1- /*
2- ReactNativeCircleCheckbox 0.1.0
1+ /*
2+ ReactNativeCircleCheckbox 0.1.1
33 https://github.com/ParamoshkinAndrew/ReactNativeCircleCheckbox
44 (c) 2016 Andrew Paramoshkin <paramoshkin.andrew@gmail.com>
55 ReactNativeCircleCheckbox may be freely distributed under the MIT license.
6- */
6+ */
77
88'use strict'
99
@@ -17,37 +17,54 @@ var {
1717
1818class CircleCheckBox extends Component {
1919
20+ static propTypes = {
21+ checked : React . PropTypes . bool ,
22+ outerSize : React . PropTypes . number ,
23+ filterSize : React . PropTypes . number ,
24+ innerSize : React . PropTypes . number ,
25+ outerColor : React . PropTypes . string ,
26+ filterColor : React . PropTypes . string ,
27+ innerColor : React . PropTypes . string ,
28+ onToggle : React . PropTypes . func . isRequired
29+ } ;
30+
31+ static defaultProps = {
32+ checked : false ,
33+ outerSize : 26 ,
34+ filterSize : 23 ,
35+ innerSize : 18 ,
36+ outerColor : '#FC9527' ,
37+ filterColor : '#FFF' ,
38+ innerColor : '#FC9527'
39+ } ;
40+
2041 constructor ( props ) {
2142 super ( props ) ;
22- var outerSize = ( props . outerSize ) ? props . outerSize : 26 ;
23- outerSize = ( parseInt ( outerSize ) < 10 || isNaN ( parseInt ( outerSize ) ) ) ? 10 : parseInt ( outerSize ) ;
24- var outerColor = ( props . outerColor ) ? props . outerColor : '#FC9527' ;
43+ var outerSize = ( parseInt ( props . outerSize ) < 10 || isNaN ( parseInt ( props . outerSize ) ) ) ? 10 : parseInt ( props . outerSize ) ;
2544 var outerStyle = {
2645 width : outerSize ,
2746 height : outerSize ,
2847 borderRadius : outerSize / 2 ,
29- backgroundColor : outerColor
48+ backgroundColor : props . outerColor
3049 } ;
3150 var filterSize = ( parseInt ( props . filterSize ) > outerStyle . width + 3 || isNaN ( parseInt ( props . filterSize ) ) ) ? outerStyle . width - 3 : parseInt ( props . filterSize ) ;
32- var filterColor = ( props . filterColor ) ? props . filterColor : '#FFF' ;
3351 var filterStyle = {
3452 width : filterSize ,
3553 height : filterSize ,
3654 borderRadius : filterSize / 2 ,
37- backgroundColor : filterColor
55+ backgroundColor : props . filterColor
3856 } ;
3957 var innerSize = ( parseInt ( props . innerSize ) > filterStyle . width + 5 || isNaN ( parseInt ( props . innerSize ) ) ) ? filterStyle . width - 5 : parseInt ( props . innerSize ) ;
40- var innerColor = ( props . innerColor ) ? props . innerColor : '#FC9527' ;
4158 var innerStyle = {
4259 width : innerSize ,
4360 height : innerSize ,
4461 borderRadius : innerSize / 2 ,
45- backgroundColor : innerColor
62+ backgroundColor : props . innerColor
4663 } ;
4764
4865 this . state = {
49- checked : props . checked ? props . checked : false ,
50- onPress : props . onPress ,
66+ checked : props . checked ,
67+ onToggle : props . onToggle ,
5168 _circleOuterStyle : outerStyle ,
5269 _circleFilterStyle : filterStyle ,
5370 _circleInnerStyle : innerStyle
@@ -56,27 +73,22 @@ class CircleCheckBox extends Component {
5673
5774 render ( ) {
5875 return (
59- < TouchableHighlight style = { [ styles . alignStyle , this . state . _circleOuterStyle ] }
60- onPress = { ( ) =>
61- ( this . state . onPress ) ?
62- this . state . onPress ( this . state . checked ) :
63- this . _onPress ( this . state . checked ) }
64- >
76+ < TouchableHighlight style = { [ styles . alignStyle , this . state . _circleOuterStyle ] } onPress = { ( ) => this . _onToggle ( ) } >
6577 < View style = { [ styles . alignStyle , this . state . _circleFilterStyle ] } >
6678 { this . _renderInner ( ) }
6779 </ View >
6880 </ TouchableHighlight >
6981 ) ;
7082 }
7183
72- _onPress ( checked ) {
73- this . setState ( {
74- checked : ! checked
75- } ) ;
84+ _onToggle ( ) {
85+ if ( this . props . onToggle ) {
86+ this . props . onToggle ( ! this . props . checked ) ;
87+ }
7688 }
7789
7890 _renderInner ( ) {
79- return this . state . checked ? ( < View style = { this . state . _circleInnerStyle } /> ) : ( < View /> ) ;
91+ return this . props . checked ? ( < View style = { this . state . _circleInnerStyle } /> ) : ( < View /> ) ;
8092 }
8193}
8294
0 commit comments