1
- /**
1
+ /**
2
2
* react-native-check-box
3
3
* Checkbox component for react native, it works on iOS and Android
4
4
* https://github.com/crazycodeboy/react-native-check-box
@@ -20,6 +20,12 @@ import PropTypes from 'prop-types';
20
20
21
21
22
22
export default class CheckBox extends Component {
23
+ constructor ( props ) {
24
+ super ( props ) ;
25
+ this . state = {
26
+ isChecked : this . props . isChecked ,
27
+ }
28
+ }
23
29
static propTypes = {
24
30
...( ViewPropTypes || View . PropTypes ) ,
25
31
leftText : PropTypes . string ,
@@ -42,7 +48,12 @@ export default class CheckBox extends Component {
42
48
leftTextStyle : { } ,
43
49
rightTextStyle : { }
44
50
}
45
-
51
+ onClick ( ) {
52
+ this . setState ( {
53
+ isChecked : ! this . state . isChecked
54
+ } )
55
+ this . props . onClick ( ) ;
56
+ }
46
57
_renderLeft ( ) {
47
58
if ( this . props . leftTextView ) return this . props . leftTextView ;
48
59
if ( ! this . props . leftText ) return null ;
@@ -62,7 +73,7 @@ export default class CheckBox extends Component {
62
73
if ( this . props . isIndeterminate ) {
63
74
return this . props . indeterminateImage ? this . props . indeterminateImage : this . genCheckedImage ( ) ;
64
75
}
65
- if ( this . props . isChecked ) {
76
+ if ( this . state . isChecked ) {
66
77
return this . props . checkedImage ? this . props . checkedImage : this . genCheckedImage ( ) ;
67
78
} else {
68
79
return this . props . unCheckedImage ? this . props . unCheckedImage : this . genCheckedImage ( ) ;
@@ -72,10 +83,10 @@ export default class CheckBox extends Component {
72
83
genCheckedImage ( ) {
73
84
var source ;
74
85
if ( this . props . isIndeterminate ) {
75
- source = require ( './img/ic_indeterminate_check_box.png' ) ;
86
+ source = require ( './img/ic_indeterminate_check_box.png' ) ;
76
87
}
77
88
else {
78
- source = this . props . isChecked ? require ( './img/ic_check_box.png' ) : require ( './img/ic_check_box_outline_blank.png' ) ;
89
+ source = this . state . isChecked ? require ( './img/ic_check_box.png' ) : require ( './img/ic_check_box_outline_blank.png' ) ;
79
90
}
80
91
81
92
return (
@@ -87,7 +98,7 @@ export default class CheckBox extends Component {
87
98
return (
88
99
< TouchableHighlight
89
100
style = { this . props . style }
90
- onPress = { this . props . onClick }
101
+ onPress = { ( ) => this . onClick ( ) }
91
102
underlayColor = 'transparent'
92
103
disabled = { this . props . disabled }
93
104
>
0 commit comments