Skip to content

Commit c3d1a86

Browse files
committed
build
1 parent 3e91140 commit c3d1a86

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

example/bundle.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

+29-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ var Check = _interopRequire(require("./check"));
1212

1313
var X = _interopRequire(require("./x"));
1414

15+
var addons = require("react/addons").addons;
16+
17+
var PureRenderMixin = addons.PureRenderMixin;
18+
1519
module.exports = React.createClass({
20+
mixins: [PureRenderMixin],
21+
1622
displayName: "Toggle",
1723

1824
propTypes: {
@@ -27,20 +33,36 @@ module.exports = React.createClass({
2733
},
2834

2935
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+
}
3042
return {
43+
checked: !!checked,
3144
hasFocus: false
3245
};
3346
},
3447

48+
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
49+
if ("checked" in nextProps) {
50+
this.setState({ checked: !!nextProps.checked });
51+
}
52+
},
53+
3554
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();
3960
return;
4061
}
41-
event.preventDefault();
42-
checkbox.click();
43-
checkbox.focus();
62+
63+
if (!("checked" in this.props)) {
64+
this.setState({ checked: checkbox.checked });
65+
}
4466
},
4567

4668
handleFocus: function handleFocus() {
@@ -51,19 +73,9 @@ module.exports = React.createClass({
5173
this.setState({ hasFocus: false });
5274
},
5375

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-
6476
render: function render() {
6577
var classes = classNames("react-toggle", {
66-
"react-toggle--checked": this.isChecked(),
78+
"react-toggle--checked": this.state.checked,
6779
"react-toggle--focus": this.state.hasFocus,
6880
"react-toggle--disabled": this.props.disabled
6981
});

0 commit comments

Comments
 (0)