2015-10-11 20:07:09 +03:00
|
|
|
import React from 'react';
|
2016-05-21 19:42:22 +03:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { themr } from 'react-css-themr';
|
2015-12-06 16:35:10 +03:00
|
|
|
import Check from './Check';
|
2015-10-05 10:12:16 +03:00
|
|
|
|
2015-10-22 02:31:17 +03:00
|
|
|
class Checkbox extends React.Component {
|
2015-10-20 08:40:51 +03:00
|
|
|
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-05-21 19:42:22 +03:00
|
|
|
onChange: React.PropTypes.func,
|
|
|
|
theme: React.PropTypes.shape({
|
|
|
|
disabled: React.PropTypes.string.isRequired,
|
|
|
|
field: React.PropTypes.string.isRequired,
|
|
|
|
input: React.PropTypes.string.isRequired,
|
|
|
|
ripple: React.PropTypes.string.isRequired
|
|
|
|
})
|
2015-10-20 08:40:51 +03:00
|
|
|
};
|
2015-10-05 10:12:16 +03:00
|
|
|
|
2015-10-20 08:40:51 +03:00
|
|
|
static defaultProps = {
|
|
|
|
checked: false,
|
|
|
|
className: '',
|
|
|
|
disabled: false
|
|
|
|
};
|
2015-10-05 10:12:16 +03:00
|
|
|
|
2015-12-06 16:35:10 +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) {
|
2015-12-06 16:35:10 +03:00
|
|
|
this.props.onChange(!this.props.checked, event);
|
2015-11-12 20:58:06 +03:00
|
|
|
}
|
2015-11-07 05:36:51 +03:00
|
|
|
};
|
|
|
|
|
2015-11-22 21:20:05 +03:00
|
|
|
blur () {
|
|
|
|
this.refs.input.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
focus () {
|
|
|
|
this.refs.input.focus();
|
|
|
|
}
|
|
|
|
|
2015-10-05 10:12:16 +03:00
|
|
|
render () {
|
2016-05-21 19:42:22 +03:00
|
|
|
const { onChange, theme, ...others } = this.props; //eslint-disable-line no-unused-vars
|
|
|
|
const className = classnames(theme.field, {
|
|
|
|
[theme.disabled]: this.props.disabled
|
2015-11-28 15:42:17 +03:00
|
|
|
}, this.props.className);
|
|
|
|
|
2015-10-05 10:12:16 +03:00
|
|
|
return (
|
2015-12-06 16:35:10 +03:00
|
|
|
<label data-react-toolbox='checkbox' className={className}>
|
2015-10-05 10:12:16 +03:00
|
|
|
<input
|
2015-12-06 16:35:10 +03:00
|
|
|
{...others}
|
2016-05-21 19:42:22 +03:00
|
|
|
className={theme.input}
|
2015-12-06 16:35:10 +03:00
|
|
|
onClick={this.handleToggle}
|
|
|
|
readOnly
|
|
|
|
ref='input'
|
2015-11-07 04:59:50 +03:00
|
|
|
type='checkbox'
|
2015-10-05 10:12:16 +03:00
|
|
|
/>
|
2016-05-21 19:42:22 +03:00
|
|
|
<Check rippleClassName={theme.ripple} checked={this.props.checked} disabled={this.props.disabled}/>
|
|
|
|
{this.props.label ? <span data-react-toolbox='label' className={theme.text}>{this.props.label}</span> : null}
|
2015-10-05 10:12:16 +03:00
|
|
|
</label>
|
|
|
|
);
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-10-21 13:25:07 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
2016-05-21 19:42:22 +03:00
|
|
|
export default themr('ToolboxCheckbox')(Checkbox);
|