Skip to content

Commit c1c1a9a

Browse files
committed
Pure <Switch>
1 parent d5294cc commit c1c1a9a

File tree

5 files changed

+65
-41
lines changed

5 files changed

+65
-41
lines changed

components/switch/index.jsx

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ class Switch extends React.Component {
1010
disabled: React.PropTypes.bool,
1111
label: React.PropTypes.string,
1212
name: React.PropTypes.string,
13-
onBlur: React.PropTypes.func,
14-
onChange: React.PropTypes.func,
15-
onFocus: React.PropTypes.func
13+
onChange: React.PropTypes.func
1614
};
1715

1816
static defaultProps = {
@@ -21,19 +19,9 @@ class Switch extends React.Component {
2119
disabled: false
2220
};
2321

24-
state = {
25-
checked: this.props.checked
26-
};
27-
2822
handleChange = (event) => {
29-
this.setState({checked: !this.state.checked}, () => {
30-
if (this.props.onChange) this.props.onChange(event, this);
31-
});
32-
};
33-
34-
handleClick = (event) => {
3523
events.pauseEvent(event);
36-
if (!this.props.disabled) this.handleChange(event);
24+
if (this.props.onChange && !this.props.disabled) this.props.onChange(event);
3725
};
3826

3927
handleInputClick = (event) => {
@@ -46,19 +34,19 @@ class Switch extends React.Component {
4634

4735
render () {
4836
let labelClassName = style[this.props.disabled ? 'disabled' : 'field'];
49-
const switchClassName = style[this.state.checked ? 'switch-on' : 'switch-off'];
37+
const switchClassName = style[this.props.checked ? 'on' : 'off'];
5038
if (this.props.className) labelClassName += ` ${this.props.className}`;
5139

5240
return (
5341
<label
5442
data-react-toolbox='checkbox'
5543
className={labelClassName}
56-
onClick={this.handleClick}
44+
onClick={this.handleChange}
5745
>
5846
<input
5947
{...this.props}
6048
ref='input'
61-
checked={this.state.checked}
49+
checked={this.props.checked}
6250
className={style.input}
6351
onChange={this.handleChange}
6452
onClick={this.handleInputClick}
@@ -81,14 +69,6 @@ class Switch extends React.Component {
8169
focus () {
8270
this.refs.input.focus();
8371
}
84-
85-
getValue () {
86-
return this.state.checked;
87-
}
88-
89-
setValue (value) {
90-
this.setState({checked: value});
91-
}
9272
}
9373

9474
export default Switch;

components/switch/readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,4 @@ const SwitchTest = () => (
2424
| `disabled` | `Boolean` | `false` | If true, component will be disabled.|
2525
| `label` | `String` | | The text string to use for the floating label element.|
2626
| `name` | `String` | | The text string used as name of the input.|
27-
| `onBlur` | `Function` | | Callback function that is fired when when the switch is blurred.|
2827
| `onChange` | `Function` | | Callback function that is fired when the components's value changes.|
29-
| `onFocus` | `Function` | | Callback function fire when the switch is focused.|

components/switch/style.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
transition-duration: $switch-ripple-duration;
4949
}
5050

51-
.switch-on {
51+
.on {
5252
@extend %switch;
5353
background: $switch-track-on-color;
5454
.thumb {
@@ -58,7 +58,7 @@
5858
}
5959
}
6060

61-
.switch-off {
61+
.off {
6262
@extend %switch;
6363
background: $switch-track-off-color;
6464
.thumb {
Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1-
const SwitchTest = () => (
2-
<fieldset>
3-
<Switch label="Push notifications" />
4-
<Switch checked label="Mail notifications" />
5-
<Switch disabled label="Nothing, thanks"/>
6-
</fieldset>
7-
);
1+
class SwitchTest extends React.Component {
2+
state = {
3+
switch: [true, false, false]
4+
};
5+
6+
handleChange = (index) => {
7+
const state = this.state.switch;
8+
state[index] = !state[index];
9+
this.setState({switch: state});
10+
};
11+
12+
render () {
13+
return (
14+
<section>
15+
<Switch
16+
checked={this.state.switch[0]}
17+
label="Push notifications"
18+
onChange={this.handleChange.bind(this, 0)}
19+
/>
20+
<Switch
21+
checked={this.state.switch[1]}
22+
label="Mail notifications"
23+
onChange={this.handleChange.bind(this, 1)}
24+
/>
25+
<Switch
26+
checked={this.state.switch[2]}
27+
disabled
28+
label="Nothing, thanks"
29+
onChange={this.handleChange.bind(this, 2)}
30+
/>
31+
</section>
32+
);
33+
}
34+
}
835

936
return <SwitchTest />;

spec/components/switch.jsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,37 @@ import React from 'react';
22
import Switch from '../../components/switch';
33

44
class SwitchTest extends React.Component {
5-
handleChange = (event, instance) => {
6-
console.log('[Switch] Changed', instance.getValue());
5+
state = {
6+
switch: [true, false, false]
7+
};
8+
9+
handleChange = (index) => {
10+
const state = this.state.switch;
11+
state[index] = !state[index];
12+
this.setState({switch: state});
713
};
814

915
render () {
1016
return (
1117
<section>
1218
<h5>Switches</h5>
1319
<p style={{marginBottom: '10px'}}>This is more beautiful than the old fashion checkboxes...</p>
14-
<Switch label="Push notifications" />
15-
<Switch checked label="Mail notifications" onChange={this.handleChange} />
16-
<Switch disabled label="Nothing, thanks"/>
20+
<Switch
21+
checked={this.state.switch[0]}
22+
label="Push notifications"
23+
onChange={this.handleChange.bind(this, 0)}
24+
/>
25+
<Switch
26+
checked={this.state.switch[1]}
27+
label="Mail notifications"
28+
onChange={this.handleChange.bind(this, 1)}
29+
/>
30+
<Switch
31+
checked={this.state.switch[2]}
32+
disabled
33+
label="Nothing, thanks"
34+
onChange={this.handleChange.bind(this, 2)}
35+
/>
1736
</section>
1837
);
1938
}

0 commit comments

Comments
 (0)