react-toolbox/components/switch/Switch.js

73 lines
2.0 KiB
JavaScript
Raw Normal View History

import React from 'react';
2016-05-25 01:54:56 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2015-12-07 04:14:10 +03:00
import Thumb from './Thumb';
2015-10-06 20:40:47 +03:00
class Switch extends React.Component {
static propTypes = {
2015-10-06 20:40:47 +03:00
checked: React.PropTypes.bool,
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
label: React.PropTypes.string,
name: React.PropTypes.string,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
2016-05-25 01:54:56 +03:00
onFocus: React.PropTypes.func,
theme: React.PropTypes.shape({
disabled: React.PropTypes.string,
field: React.PropTypes.string,
input: React.PropTypes.string,
off: React.PropTypes.string,
on: React.PropTypes.string,
ripple: React.PropTypes.string,
text: React.PropTypes.string,
thumb: React.PropTypes.string
})
};
2015-10-06 20:40:47 +03:00
static defaultProps = {
checked: false,
className: '',
disabled: false
};
2015-10-06 20:40:47 +03:00
2015-12-07 04:14:10 +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);
}
2015-10-22 02:31:17 +03:00
};
2015-10-06 20:40:47 +03:00
blur () {
this.refs.input.blur();
}
focus () {
this.refs.input.focus();
}
2015-10-06 20:40:47 +03:00
render () {
2016-05-25 01:54:56 +03:00
const { className, checked, disabled, onChange, theme, ...others } = this.props; //eslint-disable-line no-unused-vars
const _className = classnames(theme[disabled ? 'disabled' : 'field'], className);
2015-10-06 20:40:47 +03:00
return (
2016-05-25 01:54:56 +03:00
<label data-react-toolbox='switch' className={_className}>
2015-10-06 20:40:47 +03:00
<input
2015-12-07 04:14:10 +03:00
{...others}
checked={this.props.checked}
2016-05-25 01:54:56 +03:00
className={theme.input}
2015-12-07 04:14:10 +03:00
onClick={this.handleToggle}
readOnly
ref='input'
type='checkbox'
2015-10-06 20:40:47 +03:00
/>
2016-05-25 01:54:56 +03:00
<span className={theme[checked ? 'on' : 'off']}>
<Thumb disabled={this.props.disabled} theme={theme} />
2015-10-06 20:40:47 +03:00
</span>
2016-05-25 01:54:56 +03:00
{this.props.label ? <span className={theme.text}>{this.props.label}</span> : null}
2015-10-06 20:40:47 +03:00
</label>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
2016-05-25 01:54:56 +03:00
export default themr('ToolboxSwitch')(Switch);