@@ -12,7 +12,13 @@ var Check = _interopRequire(require("./check"));
12
12
13
13
var X = _interopRequire ( require ( "./x" ) ) ;
14
14
15
+ var addons = require ( "react/addons" ) . addons ;
16
+
17
+ var PureRenderMixin = addons . PureRenderMixin ;
18
+
15
19
module . exports = React . createClass ( {
20
+ mixins : [ PureRenderMixin ] ,
21
+
16
22
displayName : "Toggle" ,
17
23
18
24
propTypes : {
@@ -27,20 +33,36 @@ module.exports = React.createClass({
27
33
} ,
28
34
29
35
getInitialState : function getInitialState ( ) {
36
+ var checked = false ;
37
+ if ( "checked" in this . props ) {
38
+ checked = this . props . checked ;
39
+ } else if ( "defaultChecked" in this . props ) {
40
+ checked = this . props . defaultChecked ;
41
+ }
30
42
return {
43
+ checked : ! ! checked ,
31
44
hasFocus : false
32
45
} ;
33
46
} ,
34
47
48
+ componentWillReceiveProps : function componentWillReceiveProps ( nextProps ) {
49
+ if ( "checked" in nextProps ) {
50
+ this . setState ( { checked : ! ! nextProps . checked } ) ;
51
+ }
52
+ } ,
53
+
35
54
handleClick : function handleClick ( event ) {
36
- var checkbox = this . refs . input . getDOMNode ( ) ;
37
- var checkboxWasDirectlyClicked = event . target === checkbox ;
38
- if ( checkboxWasDirectlyClicked ) {
55
+ var checkbox = React . findDOMNode ( this . refs . input ) ;
56
+ if ( event . target !== checkbox ) {
57
+ event . preventDefault ( ) ;
58
+ checkbox . focus ( ) ;
59
+ checkbox . click ( ) ;
39
60
return ;
40
61
}
41
- event . preventDefault ( ) ;
42
- checkbox . click ( ) ;
43
- checkbox . focus ( ) ;
62
+
63
+ if ( ! ( "checked" in this . props ) ) {
64
+ this . setState ( { checked : checkbox . checked } ) ;
65
+ }
44
66
} ,
45
67
46
68
handleFocus : function handleFocus ( ) {
@@ -51,19 +73,9 @@ module.exports = React.createClass({
51
73
this . setState ( { hasFocus : false } ) ;
52
74
} ,
53
75
54
- isChecked : function isChecked ( ) {
55
- if ( this . props . checked != null ) {
56
- return this . props . checked ;
57
- }
58
- if ( this . refs . input ) {
59
- return this . refs . input . getDOMNode ( ) . checked ;
60
- }
61
- return this . props . defaultChecked || false ;
62
- } ,
63
-
64
76
render : function render ( ) {
65
77
var classes = classNames ( "react-toggle" , {
66
- "react-toggle--checked" : this . isChecked ( ) ,
78
+ "react-toggle--checked" : this . state . checked ,
67
79
"react-toggle--focus" : this . state . hasFocus ,
68
80
"react-toggle--disabled" : this . props . disabled
69
81
} ) ;
0 commit comments