2017-04-17 17:14:17 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-05-25 01:54:56 +03:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { themr } from 'react-css-themr';
|
2017-01-26 20:05:32 +03:00
|
|
|
import { SWITCH } from '../identifiers';
|
|
|
|
import rippleFactory from '../ripple/Ripple';
|
|
|
|
import thumbFactory from './Thumb';
|
2015-10-06 20:40:47 +03:00
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
const factory = (Thumb) => {
|
|
|
|
class Switch extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
checked: PropTypes.bool,
|
|
|
|
className: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
label: PropTypes.string,
|
|
|
|
name: PropTypes.string,
|
|
|
|
onBlur: PropTypes.func,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onFocus: PropTypes.func,
|
2017-01-21 14:47:21 +03:00
|
|
|
ripple: PropTypes.bool,
|
2016-05-30 07:59:44 +03:00
|
|
|
theme: PropTypes.shape({
|
|
|
|
disabled: PropTypes.string,
|
|
|
|
field: PropTypes.string,
|
|
|
|
input: PropTypes.string,
|
|
|
|
off: PropTypes.string,
|
|
|
|
on: PropTypes.string,
|
|
|
|
ripple: PropTypes.string,
|
|
|
|
text: PropTypes.string,
|
2017-01-26 20:05:32 +03:00
|
|
|
thumb: PropTypes.string,
|
|
|
|
}),
|
2016-05-30 07:59:44 +03:00
|
|
|
};
|
2015-10-06 20:40:47 +03:00
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
static defaultProps = {
|
|
|
|
checked: false,
|
|
|
|
className: '',
|
2017-01-26 20:05:32 +03:00
|
|
|
disabled: false,
|
2016-05-30 07:59:44 +03:00
|
|
|
};
|
2015-10-06 20:40:47 +03:00
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
handleToggle = (event) => {
|
|
|
|
if (event.pageX !== 0 && event.pageY !== 0) this.blur();
|
|
|
|
if (!this.props.disabled && this.props.onChange) {
|
|
|
|
this.props.onChange(!this.props.checked, event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
blur() {
|
|
|
|
this.inputNode.blur();
|
2015-11-25 13:31:59 +03:00
|
|
|
}
|
2015-10-06 20:40:47 +03:00
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
focus() {
|
|
|
|
this.inputNode.focus();
|
2016-05-30 07:59:44 +03:00
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
checked,
|
|
|
|
className,
|
|
|
|
disabled,
|
|
|
|
onChange, // eslint-disable-line no-unused-vars
|
|
|
|
ripple,
|
2019-05-07 16:50:56 +03:00
|
|
|
style, // eslint-disable-line
|
2017-01-26 20:05:32 +03:00
|
|
|
theme,
|
|
|
|
...others
|
|
|
|
} = this.props;
|
2016-05-30 07:59:44 +03:00
|
|
|
const _className = classnames(theme[disabled ? 'disabled' : 'field'], className);
|
|
|
|
return (
|
2019-05-07 16:50:56 +03:00
|
|
|
<label data-react-toolbox="switch" className={_className} style={style}>
|
2016-05-30 07:59:44 +03:00
|
|
|
<input
|
|
|
|
{...others}
|
|
|
|
checked={this.props.checked}
|
|
|
|
className={theme.input}
|
|
|
|
onClick={this.handleToggle}
|
|
|
|
readOnly
|
2017-01-26 20:05:32 +03:00
|
|
|
ref={(node) => { this.inputNode = node; }}
|
|
|
|
type="checkbox"
|
2016-05-30 07:59:44 +03:00
|
|
|
/>
|
|
|
|
<span className={theme[checked ? 'on' : 'off']}>
|
2017-01-21 14:47:21 +03:00
|
|
|
<Thumb disabled={this.props.disabled} theme={theme} ripple={ripple} />
|
2016-05-30 07:59:44 +03:00
|
|
|
</span>
|
|
|
|
{this.props.label ? <span className={theme.text}>{this.props.label}</span> : null}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
return Switch;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Thumb = thumbFactory(rippleFactory({ centered: true, spread: 2.6 }));
|
|
|
|
const Switch = factory(Thumb);
|
2015-10-22 02:31:17 +03:00
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
export default themr(SWITCH)(Switch);
|
|
|
|
export { factory as switchFactory };
|
|
|
|
export { Switch };
|