forked from crazycodeboy/react-native-check-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·116 lines (108 loc) · 3.38 KB
/
index.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
110
111
112
113
114
115
116
/**
* 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 {
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,
containerStyle: PropTypes.object,
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: {}
}
_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.props.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.props.isChecked ? require('./img/ic_check_box.png') : require('./img/ic_check_box_outline_blank.png');
}
return (
<Image source={source} style={{tintColor: this.props.checkBoxColor}} />
);
}
render() {
return (
<TouchableHighlight
style={this.props.style}
onPress={this.props.onClick}
underlayColor='transparent'
disabled={this.props.disabled}
>
<View style={[styles.container,this.props.containerStyle]}>
{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
}
});