react-toolbox/components/checkbox/Checkbox.js

106 lines
2.8 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';
2017-01-26 20:05:32 +03:00
import styleShape from 'react-style-proptype';
2016-05-21 19:42:22 +03:00
import { themr } from 'react-css-themr';
2017-01-26 20:05:32 +03:00
import { CHECKBOX } from '../identifiers';
import rippleFactory from '../ripple/Ripple';
import checkFactory from './Check';
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,
2017-01-26 20:05:32 +03:00
PropTypes.node,
2016-06-19 22:37:46 +03:00
]),
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,
2017-01-26 20:05:32 +03:00
style: styleShape,
2016-05-29 13:38:20 +03:00
theme: PropTypes.shape({
disabled: PropTypes.string,
field: PropTypes.string,
input: PropTypes.string,
2017-01-26 20:05:32 +03:00
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: '',
2017-01-26 20:05:32 +03:00
disabled: false,
2016-05-29 13:38:20 +03:00
};
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);
}
};
2017-01-26 20:05:32 +03:00
blur() {
if (this.inputNode) {
this.inputNode.blur();
}
2016-05-29 13:38:20 +03:00
}
2017-01-26 20:05:32 +03:00
focus() {
if (this.inputNode) {
this.inputNode.focus();
}
2016-05-29 13:38:20 +03:00
}
2017-01-26 20:05:32 +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, {
2017-01-26 20:05:32 +03:00
[theme.disabled]: this.props.disabled,
2016-05-29 13:38:20 +03:00
}, 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
2017-01-26 20:05:32 +03:00
data-react-toolbox="checkbox"
htmlFor={name}
2016-11-30 19:04:00 +03:00
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}
2017-01-26 20:05:32 +03:00
ref={(node) => { this.inputNode = node; }}
type="checkbox"
2016-05-29 13:38:20 +03:00
/>
<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
/>
2017-01-26 20:05:32 +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;
};
2017-01-26 20:05:32 +03:00
const Check = checkFactory(rippleFactory({ centered: true, spread: 2.6 }));
2016-05-29 13:38:20 +03:00
const Checkbox = factory(Check);
export default themr(CHECKBOX)(Checkbox);
export { factory as checkboxFactory };
export { Checkbox };