Skip to content

Commit fb91f14

Browse files
committed
upgraded to support React 0.13, fixed impure rendering, replaced deprecated getDOMNode() with React.findDOMNode(), added example of controlled component without onChange property
1 parent 89a0eb5 commit fb91f14

File tree

6 files changed

+65
-21
lines changed

6 files changed

+65
-21
lines changed

example/bundle.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var App = React.createClass({
1717
milkIsReady: false,
1818
eggsAreReady: false,
1919
burritoIsReady: false,
20+
toastIsReady: false,
2021
formData: {}
2122
}
2223
},
@@ -46,6 +47,10 @@ var App = React.createClass({
4647
this.setState({burritoIsReady: event.target.checked})
4748
},
4849

50+
handleToastChange(event) {
51+
this.setState({toastIsReady: event.target.checked})
52+
},
53+
4954
render() {
5055
return (
5156
<form ref="breakfastForm">
@@ -168,7 +173,7 @@ var App = React.createClass({
168173
<div className="example">
169174
<label>
170175
<Toggle
171-
defaultChecked={this.state.milkIsReady}
176+
defaultChecked={!!this.state.milkIsReady}
172177
name="milkIsReady"
173178
value="yes"
174179
onChange={this.handleMilkChange} />
@@ -213,6 +218,31 @@ var App = React.createClass({
213218
onChange={this.handleBurritoChange} />
214219
</div>
215220

221+
{/* Controlled Component without onChange */}
222+
223+
<div className="example">
224+
<label>
225+
<input
226+
type="checkbox"
227+
checked={this.state.toastIsReady}
228+
name="toastIsReady2"
229+
onChange={this.handleToastChange} />
230+
<span className="label-text"> Controlled Component without onChange</span>
231+
</label>
232+
233+
<pre>
234+
{`<Toggle
235+
checked={this.state.toastIsReady}
236+
name="toastIsReady"
237+
value="yes" />`}
238+
</pre>
239+
240+
<Toggle
241+
checked={this.state.toastIsReady}
242+
name="toastIsReady"
243+
value="yes" />
244+
</div>
245+
216246
{/* Disabled */}
217247

218248
<div className="example">

index.es6.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import React from 'react'
22
import classNames from 'classnames'
33
import Check from './check'
44
import X from './x'
5+
import {addons} from 'react/addons'
6+
7+
var PureRenderMixin = addons.PureRenderMixin;
58

69
export default React.createClass({
10+
mixins: [PureRenderMixin],
11+
712
displayName: 'Toggle',
813

914
propTypes: {
@@ -18,20 +23,37 @@ export default React.createClass({
1823
},
1924

2025
getInitialState() {
26+
var checked = false;
27+
if ('checked' in this.props) {
28+
checked = this.props.checked
29+
} else if ('defaultChecked' in this.props) {
30+
checked = this.props.defaultChecked
31+
}
2132
return {
33+
checked: !!checked,
2234
hasFocus: false
2335
}
2436
},
2537

38+
componentWillReceiveProps(nextProps) {
39+
if ('checked' in nextProps) {
40+
this.setState({checked: !!nextProps.checked})
41+
}
42+
},
43+
2644
handleClick(event) {
27-
var checkbox = this.refs.input.getDOMNode()
28-
var checkboxWasDirectlyClicked = event.target === checkbox
29-
if(checkboxWasDirectlyClicked) {
45+
var checkbox = React.findDOMNode(this.refs.input)
46+
if (event.target !== checkbox)
47+
{
48+
event.preventDefault()
49+
checkbox.focus()
50+
checkbox.click()
3051
return
3152
}
32-
event.preventDefault()
33-
checkbox.click()
34-
checkbox.focus()
53+
54+
if (!('checked' in this.props)) {
55+
this.setState({checked: checkbox.checked})
56+
}
3557
},
3658

3759
handleFocus() {
@@ -42,19 +64,9 @@ export default React.createClass({
4264
this.setState({hasFocus: false})
4365
},
4466

45-
isChecked() {
46-
if (this.props.checked != null) {
47-
return this.props.checked
48-
}
49-
if (this.refs.input) {
50-
return this.refs.input.getDOMNode().checked
51-
}
52-
return this.props.defaultChecked || false
53-
},
54-
5567
render() {
5668
var classes = classNames('react-toggle', {
57-
'react-toggle--checked': this.isChecked(),
69+
'react-toggle--checked': this.state.checked,
5870
'react-toggle--focus': this.state.hasFocus,
5971
'react-toggle--disabled': this.props.disabled
6072
})

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = React.createClass({
3333
},
3434

3535
handleClick: function handleClick(event) {
36-
var checkbox = React.findDOMNode(this.refs.input);
36+
var checkbox = this.refs.input.getDOMNode();
3737
var checkboxWasDirectlyClicked = event.target === checkbox;
3838
if (checkboxWasDirectlyClicked) {
3939
return;
@@ -56,7 +56,7 @@ module.exports = React.createClass({
5656
return this.props.checked;
5757
}
5858
if (this.refs.input) {
59-
return React.findDOMNode(this.refs.input).checked;
59+
return this.refs.input.getDOMNode().checked;
6060
}
6161
return this.props.defaultChecked || false;
6262
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"babel-loader": "^4.2.0",
3131
"css-loader": "^0.9.1",
3232
"mocha": "^2.2.1",
33-
"react": "^0.12.0",
33+
"react": "^0.13.0",
3434
"style-loader": "^0.9.0",
3535
"webpack": "^1.7.3",
3636
"webpack-dev-server": "^1.7.0"

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
entry: {
55
'example/bundle': './example/index'
66
},
7+
devtool: 'source-map',
78

89
output: {
910
path: '.',

0 commit comments

Comments
 (0)