react-toolbox/components/checkbox/Checkbox.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

import React from 'react';
2015-11-28 15:42:17 +03:00
import ClassNames from 'classnames';
import Check from './Check';
2015-10-22 02:31:17 +03:00
import style from './style';
2015-10-05 10:12:16 +03:00
2015-10-22 02:31:17 +03:00
class Checkbox extends React.Component {
static propTypes = {
2015-10-05 10:12:16 +03:00
checked: React.PropTypes.bool,
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
2015-10-19 03:38:25 +03:00
label: React.PropTypes.any,
2016-01-22 16:40:37 +03:00
onChange: React.PropTypes.func
};
2015-10-05 10:12:16 +03:00
static defaultProps = {
checked: false,
className: '',
disabled: false
};
2015-10-05 10:12:16 +03:00
handleToggle = (event) => {
if (event.pageX !== 0 && event.pageY !== 0) this.blur();
2015-11-12 03:24:31 +03:00
if (!this.props.disabled && this.props.onChange) {
this.props.onChange(!this.props.checked, event);
2015-11-12 20:58:06 +03:00
}
};
blur () {
this.refs.input.blur();
}
focus () {
this.refs.input.focus();
}
2015-10-05 10:12:16 +03:00
render () {
2016-04-09 21:34:34 +03:00
const { onChange, ...others } = this.props; //eslint-disable-line no-unused-vars
const className = ClassNames(style.field, {
2015-11-28 15:42:17 +03:00
[style.disabled]: this.props.disabled
}, this.props.className);
2015-10-05 10:12:16 +03:00
return (
<label data-react-toolbox='checkbox' className={className}>
2015-10-05 10:12:16 +03:00
<input
{...others}
className={style.input}
onClick={this.handleToggle}
readOnly
ref='input'
2015-11-07 04:59:50 +03:00
type='checkbox'
2015-10-05 10:12:16 +03:00
/>
<Check checked={this.props.checked} disabled={this.props.disabled}/>
{this.props.label ? <span data-react-toolbox='label' className={style.text}>{this.props.label}</span> : null}
2015-10-05 10:12:16 +03:00
</label>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Checkbox;