BooleanCheckView.js
3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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>
);
}
}