react-toolbox/components/checkbox/Checkbox.js

85 lines
2.3 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,
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,
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 () {
this.refs.input.blur();
}
2016-05-29 13:38:20 +03:00
focus () {
this.refs.input.focus();
}
2016-05-29 13:38:20 +03:00
render () {
const { onChange, theme, ...others } = this.props; //eslint-disable-line no-unused-vars
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}
theme={this.props.theme}
/>
{this.props.label ? <span data-react-toolbox='label' className={theme.text}>{this.props.label}</span> : null}
</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 };