BooleanCheckView.js 3.43 KB
import React, {Component} from 'react';
import {Image, Text, TouchableOpacity, View,} from 'react-native';
// 选中的图片
const checkedImg = require("../img/switch_on.png");
// 未选中的图片
const unCheckedImg = require("../img/switch_off.png");

/*
是否组件(type:'BOOLEAN')
 */
export default class BooleanCheckView extends Component {
    // 构造方法
    constructor(props) {
        super(props);
        // 是否选中
        this.state = {
            checked: true
        }
        // 传入参数
        this.attrData = this.props.attrData;
        // json解析后的传入参数
        this.extendData = (this.attrData.data && this.attrData.data.length > 0) ? JSON.parse(this.attrData.data) : {};// null=无|true=是|false=否
        // 用于带出是否选中的callback回调
        this.callback = this.props.callback;
    }

    // 生命周期-组件将要展示
    componentWillMount() {
        // 设置默认值
        let checkedVaule = true;
        if (this.attrData.value) {
            checkedVaule = this.attrData.value || this.attrData.value === 'true';
        } else {
            checkedVaule = this.extendData.booleanDefault || true;
        }
        this.setState({
            checked: checkedVaule
        });
        this.attrData.value = checkedVaule;
    }

    // 绘制UI
    render() {
        /*
        name:标题
        code:代码编号
        type:控件类型(LONG_TEXT)
        description:描述
        isRequired:是否必须
        isPreview:是否预览
        */
        let {name, code, type, description, isRequired, isPreview} = this.attrData;

        return (
            // 最外层的样式区域
            <View style={{
                flexDirection: 'row',
                width: '100%',
                height: 46,
                backgroundColor: 'white',
                alignItems: 'center',
                paddingLeft: 10,
                paddingRight: 15
            }}>
                {/*标题显示区域*/}
                <View style={{flex: 1, flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center'}}>
                    {/*是否必须UI设定*/}
                    {isRequired && <Text style={{color: "#FF3030"}}>* </Text>}
                    {!isRequired && <Text style={{color: "#fff"}}>* </Text>}
                    {/*标题设定*/}
                    <Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)'}}>{name || ""}</Text>
                </View>
                {/*用于触发选择*/}
                <TouchableOpacity
                    style={{justifyContent: 'flex-end', alignItems: 'center'}}
                    activeOpacity={0.8}
                    onPress={() => {
                        let _checked = !this.state.checked;
                        this.setState({
                            checked: _checked
                        });
                        this.attrData.value = _checked;
                        if (this.callback) {
                            this.callback(_checked);
                        }
                    }}
                >
                    <Image
                        style={{
                            width: 46,
                            height: 26,
                        }}
                        source={this.state.checked ? checkedImg : unCheckedImg}
                        resizeMode={'contain'}
                    />
                </TouchableOpacity>
            </View>
        );
    }
}