| 7 | |
| 8 | const factory = (Thumb) => { |
| 9 | class Switch extends Component { |
| 10 | static propTypes = { |
| 11 | checked: PropTypes.bool, |
| 12 | className: PropTypes.string, |
| 13 | disabled: PropTypes.bool, |
| 14 | label: PropTypes.string, |
| 15 | name: PropTypes.string, |
| 16 | onBlur: PropTypes.func, |
| 17 | onChange: PropTypes.func, |
| 18 | onFocus: PropTypes.func, |
| 19 | theme: PropTypes.shape({ |
| 20 | disabled: PropTypes.string, |
| 21 | field: PropTypes.string, |
| 22 | input: PropTypes.string, |
| 23 | off: PropTypes.string, |
| 24 | on: PropTypes.string, |
| 25 | ripple: PropTypes.string, |
| 26 | text: PropTypes.string, |
| 27 | thumb: PropTypes.string |
| 28 | }) |
| 29 | }; |
| 30 | |
| 31 | static defaultProps = { |
| 32 | checked: false, |
| 33 | className: '', |
| 34 | disabled: false |
| 35 | }; |
| 36 | |
| 37 | handleToggle = (event) => { |
| 38 | if (event.pageX !== 0 && event.pageY !== 0) this.blur(); |
| 39 | if (!this.props.disabled && this.props.onChange) { |
| 40 | this.props.onChange(!this.props.checked, event); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | blur () { |
| 45 | this.refs.input.blur(); |
| 46 | } |
| 47 | |
| 48 | focus () { |
| 49 | this.refs.input.focus(); |
| 50 | } |
| 51 | |
| 52 | render () { |
| 53 | const { className, checked, disabled, onChange, theme, ...others } = this.props; //eslint-disable-line no-unused-vars |
| 54 | const _className = classnames(theme[disabled ? 'disabled' : 'field'], className); |
| 55 | return ( |
| 56 | <label data-react-toolbox='switch' className={_className}> |
| 57 | <input |
| 58 | {...others} |
| 59 | checked={this.props.checked} |
| 60 | className={theme.input} |
| 61 | onClick={this.handleToggle} |
| 62 | readOnly |
| 63 | ref='input' |
| 64 | type='checkbox' |
| 65 | /> |
| 66 | <span className={theme[checked ? 'on' : 'off']}> |