react-toolbox/components/checkbox/Checkbox.js

100 lines
2.7 KiB
JavaScript
Raw Normal View History

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,
2016-11-23 11:59:51 +03:00
children: PropTypes.node,
2016-05-29 13:38:20 +03:00
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-11-30 19:04:00 +03:00
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func,
2016-10-06 21:29:36 +03:00
style: PropTypes.object,
2016-05-29 13:38:20 +03:00
theme: PropTypes.shape({
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);
}
};
2016-05-29 13:38:20 +03:00
blur () {
2016-11-20 13:39:03 +03:00
this.inputNode && this.inputNode.blur();
2016-05-29 13:38:20 +03:00
}
2016-05-29 13:38:20 +03:00
focus () {
2016-11-20 13:39:03 +03:00
this.inputNode && this.inputNode.focus();
2016-05-29 13:38:20 +03:00
}
2016-05-29 13:38:20 +03:00
render () {
2016-11-30 19:04:00 +03:00
const { checked, children, disabled, label, name, style, onChange, // eslint-disable-line
onMouseEnter, onMouseLeave, theme, ...others } = this.props;
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 (
2016-11-30 19:04:00 +03:00
<label
data-react-toolbox='checkbox'
className={className}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
2016-05-29 13:38:20 +03:00
<input
2016-11-30 19:04:00 +03:00
{...others}
2016-11-20 13:39:03 +03:00
checked={checked}
2016-05-29 13:38:20 +03:00
className={theme.input}
2016-11-20 13:39:03 +03:00
disabled={disabled}
2016-11-30 19:04:00 +03:00
name={name}
2016-11-20 13:39:03 +03:00
onChange={() => {}}
2016-05-29 13:38:20 +03:00
onClick={this.handleToggle}
2016-11-20 13:39:03 +03:00
ref={node => { this.inputNode = node; }}
2016-05-29 13:38:20 +03:00
type='checkbox'
/>
<Check
2016-11-20 13:39:03 +03:00
checked={checked}
disabled={disabled}
2016-05-29 13:38:20 +03:00
rippleClassName={theme.ripple}
style={style}
2016-11-20 13:39:03 +03:00
theme={theme}
2016-05-29 13:38:20 +03:00
/>
2016-11-20 13:39:03 +03:00
{label ? <span data-react-toolbox='label' className={theme.text}>{label}</span> : null}
2016-11-23 11:59:51 +03:00
{children}
2016-05-29 13:38:20 +03:00
</label>
);
}
}
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 };