BooleanCheckView.js 2.61 KB
import React, {Component} from 'react';
import {
    View,
    Text,
    TouchableOpacity,
    Image,
} from 'react-native';

const checkedImg = require("../img/switch_on.png");
const unCheckedImg = require("../img/switch_off.png");

export default class BooleanCheckView extends Component {

    constructor(props) {
        super(props);
        this.state = {
            checked: true
        }
        this.attrData = this.props.attrData;
        this.extendData = (this.attrData.data && this.attrData.data.length > 0)?JSON.parse(this.attrData.data):{};// null=无|true=是|false=否
        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;
    }

    render() {
        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'}}>
                    {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>
        );
    }
}