-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·143 lines (134 loc) · 4.02 KB
/
index.js
File metadata and controls
executable file
·143 lines (134 loc) · 4.02 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* react-native-check-box
* Checkbox component for react native, it works on iOS and Android
* https://github.com/crazycodeboy/react-native-check-box
* Email:crazycodeboy@gmail.com
* Blog:http://www.devio.org
* @flow
*/
import React, {Component} from 'react';
import {
StyleSheet,
View,
ViewPropTypes,
Image,
Text,
TouchableHighlight
} from 'react-native';
import PropTypes from 'prop-types';
export default class CheckBox extends Component {
constructor(props) {
super(props);
this.state = {
isChecked: this.props.isChecked,
}
}
static propTypes = {
...(ViewPropTypes || View.PropTypes),
leftText: PropTypes.string,
leftTextView: PropTypes.element,
rightText: PropTypes.string,
leftTextStyle: PropTypes.object,
rightTextView: PropTypes.element,
rightTextStyle: PropTypes.object,
checkedImage: PropTypes.element,
unCheckedImage: PropTypes.element,
onClick: PropTypes.func.isRequired,
isChecked: PropTypes.bool.isRequired,
isIndeterminate: PropTypes.bool.isRequired,
checkBoxColor: PropTypes.string,
disabled: PropTypes.bool,
}
static defaultProps = {
isChecked: false,
isIndeterminate: false,
leftTextStyle: {},
rightTextStyle: {}
}
static getDerivedStateFromProps(nextProps, prevState) {
if (prevState.isChecked !== nextProps.isChecked) {
return {
isChecked: nextProps.isChecked
};
}
return null;
}
onClick() {
this.setState({
isChecked: !this.state.isChecked
})
this.props.onClick();
}
_renderLeft() {
if (this.props.leftTextView)return this.props.leftTextView;
if (!this.props.leftText)return null;
return (
<Text style={[styles.leftText, this.props.leftTextStyle]}>{this.props.leftText}</Text>
);
}
_renderRight() {
if (this.props.rightTextView)return this.props.rightTextView;
if (!this.props.rightText)return null;
return (
<Text style={[styles.rightText, this.props.rightTextStyle]}>{this.props.rightText}</Text>
);
}
_renderImage() {
if (this.props.isIndeterminate){
return this.props.indeterminateImage ? this.props.indeterminateImage : this.genCheckedImage();
}
if (this.state.isChecked) {
return this.props.checkedImage ? this.props.checkedImage : this.genCheckedImage();
} else {
return this.props.unCheckedImage ? this.props.unCheckedImage : this.genCheckedImage();
}
}
genCheckedImage() {
var source;
if (this.props.isIndeterminate) {
source = require('./img/ic_indeterminate_check_box.png');
}
else {
source = this.state.isChecked ? require('./img/ic_check_box.png') : require('./img/ic_check_box_outline_blank.png');
}
return (
<Image source={source} style={{tintColor: this.props.checkBoxColor}} />
);
}
componentWillReceiveProps( newProps ){
if (newProps.isChecked != this.state.isChecked){
this.setState({
isChecked: !this.state.isChecked
})
}
}
render() {
return (
<TouchableHighlight
style={this.props.style}
onPress={()=>this.onClick()}
underlayColor='transparent'
disabled={this.props.disabled}
>
<View style={styles.container}>
{this._renderLeft()}
{this._renderImage()}
{this._renderRight()}
</View>
</TouchableHighlight>
);
}
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center'
},
leftText: {
flex: 1,
},
rightText: {
flex: 1,
marginLeft: 10
}
});