forked from peng8/react-native-checkbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox.js
executable file
·109 lines (98 loc) · 2.44 KB
/
checkbox.js
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
101
102
103
104
105
106
107
108
109
'use strict';
import React from 'react';
import {
View,
Text,
StyleSheet,
Image,
TouchableHighlight
} from "react-native"
var Icon = require('react-native-vector-icons/FontAwesome');
export default class CheckBox extends React.Component{
static defaultProps = {
label: 'Label',
labelBefore: false,
checked: false
};
static propTypes={
label: React.PropTypes.string,
labelStyle: React.PropTypes.object,
checked: React.PropTypes.bool,
onChange: React.PropTypes.func
};
constructor(props){
super(props);
this.state = {
checked: props.checked,
};
}
componentWillMount(){
console.log("checkbox初始化啦");
}
componentWillReceiveProps(nextProps) {
console.log("checkbox接收到新参数啦 ");
this.setState({
checked: nextProps.checked
});
}
componentWillUnmount(){
console.log("checkbox组件被销毁了");
}
onChange() {
this.setState({checked:!this.state.checked});
}
toggle(){
console.log("checkbox被点击了");
this.setState({checked:!this.state.checked});
this.props.onChange(this.state.checked);
}
render() {
var source = "square-o";
if(this.state.checked){
source = "check-square-o";
}
var container = (
<View style={styles.container}>
<Icon name={source} size={16} style={styles.checkbox} color="#00B4F7" ></Icon>
<View style={styles.labelContainer}>
<Text style={[styles.label, this.props.labelStyle]}>{this.props.label}</Text>
</View>
</View>
);
if (this.props.labelBefore) {
container = (
<View style={styles.container}>
<View style={styles.labelContainer}>
<Text style={[styles.label, this.props.labelStyle]}>{this.props.label}</Text>
</View>
<Icon name={source} size={16} style={styles.checkbox} color="#00B4F7" ></Icon>
</View>
);
}
return (
<TouchableHighlight ref="checkbox" onPress={this.toggle.bind(this)} underlayColor='white'>
{container}
</TouchableHighlight>
)
}
};
var styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 5,
},
checkbox: {
width: 16,
height: 16
},
labelContainer: {
marginLeft: 10,
marginRight: 10
},
label: {
fontSize: 15,
lineHeight: 15,
color: 'grey',
}
});