-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathSwitch.js
104 lines (91 loc) · 2.58 KB
/
Switch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
const propTypes = {
color: PropTypes.string,
label: PropTypes.bool,
outline: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
PropTypes.oneOf(['', 'alt'])
]),
size: PropTypes.oneOf(['', 'lg', 'sm']),
checked: PropTypes.bool,
defaultChecked: PropTypes.bool,
defaultValue: PropTypes.any,
value: PropTypes.string,
disabled: PropTypes.bool,
form: PropTypes.any,
name: PropTypes.string,
required: PropTypes.bool,
onChange: PropTypes.func,
type: PropTypes.oneOf(['checkbox', 'radio']),
variant: PropTypes.oneOf(['', '3d', 'pill']),
className: PropTypes.string,
dataOn: PropTypes.string,
dataOff: PropTypes.string,
};
const defaultProps = {
color: 'secondary',
label: false,
outline: false,
size: '',
checked: false,
defaultChecked: false,
disabled: false,
required: false,
type: 'checkbox',
variant: '',
dataOn: 'On',
dataOff: 'Off',
};
class AppSwitch extends Component {
constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);
this.state = {
checked: this.props.defaultChecked || this.props.checked,
selected: []
};
}
onChange(event) {
const target = event.target;
this.setState({
checked: target.checked,
})
if (this.props.onChange) {
this.props.onChange(event);
}
}
render() {
const { className, disabled, color, name, label, outline, size, required, type, value, dataOn, dataOff, variant, ...attributes } = this.props;
delete attributes.checked
delete attributes.defaultChecked
delete attributes.onChange
const classes = classNames(
className,
'switch',
label ? 'switch-label' : false,
size ? `switch-${size}` : false,
variant ? `switch-${variant}` : false,
`switch${outline ? '-outline' : ''}-${color}${outline==='alt' ? '-alt' : ''}`,
'form-check-label',
);
const inputClasses = classNames(
'switch-input',
'form-check-input',
);
const sliderClasses = classNames(
'switch-slider',
);
return (
<label className={classes}>
<input type={type} className={inputClasses} onChange={this.onChange} checked={this.state.checked} name={name} required={required} disabled={disabled} value={value} {...attributes} />
<span className={sliderClasses} data-checked={dataOn} data-unchecked={dataOff}></span>
</label>
);
}
}
AppSwitch.propTypes = propTypes;
AppSwitch.defaultProps = defaultProps;
export default AppSwitch;