react-toolbox-build4server
Version:
Builds react-toolbox in such a way that it's components can be required and used in node - most likely for server-side rendered webapps - without having to depend on webpack to build your entire server-side project
65 lines (56 loc) • 1.7 kB
JavaScript
import React from 'react';
import Thumb from './Thumb';
import style from './style';
class Switch extends React.Component {
static propTypes = {
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,
onFocus: React.PropTypes.func
};
static defaultProps = {
checked: false,
className: '',
disabled: false
};
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);
}
};
blur () {
this.refs.input.blur();
}
focus () {
this.refs.input.focus();
}
render () {
let className = style[this.props.disabled ? 'disabled' : 'field'];
const switchClassName = style[this.props.checked ? 'on' : 'off'];
const { onChange, ...others } = this.props; //eslint-disable-line no-unused-vars
if (this.props.className) className += ` ${this.props.className}`;
return (
<label data-react-toolbox='switch' className={className}>
<input
{...others}
checked={this.props.checked}
className={style.input}
onClick={this.handleToggle}
readOnly
ref='input'
type='checkbox'
/>
<span className={switchClassName}>
<Thumb disabled={this.props.disabled} />
</span>
{this.props.label ? <span className={style.text}>{this.props.label}</span> : null}
</label>
);
}
}
export default Switch;