Skip to content

Commit 4efd464

Browse files
committed
refactor: Switch api
1 parent 3f9c17c commit 4efd464

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/Switch.js

+26-19
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ import classNames from 'classnames';
55
const propTypes = {
66
color: PropTypes.string,
77
label: PropTypes.bool,
8-
outline: PropTypes.bool,
9-
outlineAlt: PropTypes.bool,
10-
pill: PropTypes.bool,
11-
size: PropTypes.string,
8+
outline: PropTypes.oneOfType([
9+
PropTypes.bool,
10+
PropTypes.string,
11+
PropTypes.oneOf(['', 'alt'])
12+
]),
13+
size: PropTypes.oneOf(['', 'lg', 'sm']),
1214
checked: PropTypes.bool,
1315
defaultChecked: PropTypes.bool,
1416
defaultValue: PropTypes.any,
17+
value: PropTypes.string,
1518
disabled: PropTypes.bool,
1619
form: PropTypes.any,
17-
indeterminate: PropTypes.any,
1820
name: PropTypes.string,
1921
required: PropTypes.bool,
20-
type: PropTypes.string,
21-
value: PropTypes.string,
22+
onChange: PropTypes.func,
23+
type: PropTypes.oneOf(['checkbox', 'radio']),
24+
variant: PropTypes.oneOf(['', '3d', 'pill']),
2225
className: PropTypes.string,
2326
dataOn: PropTypes.string,
2427
dataOff: PropTypes.string,
@@ -28,48 +31,52 @@ const defaultProps = {
2831
color: 'secondary',
2932
label: false,
3033
outline: false,
31-
outlineAlt: false,
32-
pill: false,
3334
size: '',
3435
checked: false,
3536
defaultChecked: false,
3637
disabled: false,
3738
required: false,
3839
type: 'checkbox',
40+
variant: '',
3941
dataOn: 'On',
4042
dataOff: 'Off',
4143
};
4244

4345
class AppSwitch extends Component {
4446
constructor(props) {
4547
super(props);
46-
this.toggle = this.toggle.bind(this);
48+
this.onChange = this.onChange.bind(this);
4749
this.state = {
4850
checked: this.props.defaultChecked || this.props.checked,
51+
selected: []
4952
};
5053
}
5154

52-
toggle(event) {
55+
onChange(event) {
5356
const target = event.target;
5457
this.setState({
5558
checked: target.checked,
56-
});
59+
})
60+
61+
if (this.props.onChange) {
62+
this.props.onChange(event);
63+
}
5764
}
5865

5966
render() {
60-
const { className, checked, disabled, color, defaultChecked, name, label, outline, outlineAlt, pill, size, required, type, value, dataOn, dataOff, ...attributes } = this.props;
61-
62-
const outlined = outline || outlineAlt;
67+
const { className, disabled, color, name, label, outline, size, required, type, value, dataOn, dataOff, variant, ...attributes } = this.props;
6368

64-
const variant = `switch${outlined ? '-outline' : ''}-${color}${outlineAlt ? '-alt' : ''}`;
69+
delete attributes.checked
70+
delete attributes.defaultChecked
71+
delete attributes.onChange
6572

6673
const classes = classNames(
6774
className,
6875
'switch',
6976
label ? 'switch-label' : false,
70-
pill ? 'switch-pill' : false,
7177
size ? `switch-${size}` : false,
72-
variant,
78+
variant ? `switch-${variant}` : false,
79+
`switch${outline ? '-outline' : ''}-${color}${outline==='alt' ? '-alt' : ''}`,
7380
'form-check-label',
7481
);
7582

@@ -84,7 +91,7 @@ class AppSwitch extends Component {
8491

8592
return (
8693
<label className={classes}>
87-
<input type={type} className={inputClasses} onChange={this.toggle} checked={this.state.checked} name={name} required={required} disabled={disabled} value={value} />
94+
<input type={type} className={inputClasses} onChange={this.onChange} checked={this.state.checked} name={name} required={required} disabled={disabled} value={value} {...attributes} />
8895
<span className={sliderClasses} data-checked={dataOn} data-unchecked={dataOff}></span>
8996
</label>
9097
);

0 commit comments

Comments
 (0)