2016-05-29 13:38:20 +03:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-05-21 19:42:22 +03:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { themr } from 'react-css-themr';
|
2016-05-29 13:38:20 +03:00
|
|
|
import { CHECKBOX } from '../identifiers.js';
|
|
|
|
import rippleFactory from '../ripple/Ripple.js';
|
|
|
|
import checkFactory from './Check.js';
|
2015-10-05 10:12:16 +03:00
|
|
|
|
2016-05-29 13:38:20 +03:00
|
|
|
const factory = (Check) => {
|
|
|
|
class Checkbox extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
checked: PropTypes.bool,
|
|
|
|
className: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
2016-06-19 22:37:46 +03:00
|
|
|
label: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.node
|
|
|
|
]),
|
2016-06-09 20:21:22 +03:00
|
|
|
name: PropTypes.string,
|
2016-05-29 13:38:20 +03:00
|
|
|
onChange: PropTypes.func,
|
2016-10-06 21:29:36 +03:00
|
|
|
style: PropTypes.object,
|
2016-05-29 13:38:20 +03:00
|
|
|
theme: PropTypes.shape({
|
2016-06-04 00:44:33 +03:00
|
|
|
disabled: PropTypes.string,
|
|
|
|
field: PropTypes.string,
|
|
|
|
input: PropTypes.string,
|
|
|
|
ripple: PropTypes.string
|
2016-05-29 13:38:20 +03:00
|
|
|
})
|
|
|
|
};
|
2015-10-05 10:12:16 +03:00
|
|
|
|
2016-05-29 13:38:20 +03:00
|
|
|
static defaultProps = {
|
|
|
|
checked: false,
|
|
|
|
className: '',
|
|
|
|
disabled: false
|
|
|
|
};
|
2015-10-05 10:12:16 +03:00
|
|
|
|
2016-05-29 13:38:20 +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-11-07 05:36:51 +03:00
|
|
|
|
2016-05-29 13:38:20 +03:00
|
|
|
blur () {
|
|
|
|
this.refs.input.blur();
|
|
|
|
}
|
2015-11-22 21:20:05 +03:00
|
|
|
|
2016-05-29 13:38:20 +03:00
|
|
|
focus () {
|
|
|
|
this.refs.input.focus();
|
|
|
|
}
|
2015-11-22 21:20:05 +03:00
|
|
|
|
2016-05-29 13:38:20 +03:00
|
|
|
render () {
|
2016-09-27 16:28:26 +03:00
|
|
|
const { onChange, theme, style, ...others } = this.props; //eslint-disable-line no-unused-vars
|
2016-05-29 13:38:20 +03:00
|
|
|
const className = classnames(theme.field, {
|
|
|
|
[theme.disabled]: this.props.disabled
|
|
|
|
}, this.props.className);
|
2015-11-28 15:42:17 +03:00
|
|
|
|
2016-05-29 13:38:20 +03:00
|
|
|
return (
|
|
|
|
<label data-react-toolbox='checkbox' className={className}>
|
|
|
|
<input
|
|
|
|
{...others}
|
|
|
|
className={theme.input}
|
|
|
|
onClick={this.handleToggle}
|
|
|
|
readOnly
|
|
|
|
ref='input'
|
|
|
|
type='checkbox'
|
|
|
|
/>
|
|
|
|
<Check
|
|
|
|
checked={this.props.checked}
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
rippleClassName={theme.ripple}
|
2016-09-27 16:28:26 +03:00
|
|
|
style={style}
|
2016-05-29 13:38:20 +03:00
|
|
|
theme={this.props.theme}
|
|
|
|
/>
|
|
|
|
{this.props.label ? <span data-react-toolbox='label' className={theme.text}>{this.props.label}</span> : null}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
2016-05-29 13:38:20 +03:00
|
|
|
return Checkbox;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Check = checkFactory(rippleFactory({ centered: true, spread: 2.6}));
|
|
|
|
const Checkbox = factory(Check);
|
|
|
|
export default themr(CHECKBOX)(Checkbox);
|
|
|
|
export { factory as checkboxFactory };
|
|
|
|
export { Checkbox };
|