2016-12-01 18:57:19 +00:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
|
import classnames from 'classnames';
|
2016-12-16 15:54:49 +00:00
|
|
|
import { noop, pick } from 'lodash';
|
|
|
|
|
|
|
|
|
|
import FormField from 'components/forms/FormField';
|
2016-12-01 18:57:19 +00:00
|
|
|
|
|
|
|
|
const baseClass = 'kolide-checkbox';
|
|
|
|
|
|
2016-12-16 15:54:49 +00:00
|
|
|
class Checkbox extends Component {
|
2016-12-01 18:57:19 +00:00
|
|
|
static propTypes = {
|
|
|
|
|
children: PropTypes.node,
|
|
|
|
|
className: PropTypes.string,
|
2016-12-21 17:25:54 +00:00
|
|
|
disabled: PropTypes.bool,
|
2016-12-01 18:57:19 +00:00
|
|
|
name: PropTypes.string,
|
|
|
|
|
onChange: PropTypes.func,
|
2016-12-16 15:54:49 +00:00
|
|
|
value: PropTypes.bool,
|
2017-01-03 20:56:50 +00:00
|
|
|
wrapperClassName: PropTypes.string,
|
2016-12-01 18:57:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static defaultProps = {
|
2016-12-21 17:25:54 +00:00
|
|
|
disabled: false,
|
2016-12-01 18:57:19 +00:00
|
|
|
onChange: noop,
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-16 15:54:49 +00:00
|
|
|
handleChange = () => {
|
|
|
|
|
const { onChange, value } = this.props;
|
|
|
|
|
|
|
|
|
|
return onChange(!value);
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-01 18:57:19 +00:00
|
|
|
render () {
|
2016-12-16 15:54:49 +00:00
|
|
|
const { handleChange } = this;
|
2017-01-03 20:56:50 +00:00
|
|
|
const { children, className, disabled, name, value, wrapperClassName } = this.props;
|
2016-12-01 18:57:19 +00:00
|
|
|
const checkBoxClass = classnames(baseClass, className);
|
|
|
|
|
|
2016-12-16 15:54:49 +00:00
|
|
|
const formFieldProps = pick(this.props, ['hint', 'label', 'error', 'name']);
|
|
|
|
|
|
2016-12-21 17:25:54 +00:00
|
|
|
const checkBoxTickClass = classnames(`${checkBoxClass}__tick`, {
|
|
|
|
|
[`${checkBoxClass}__tick--disabled`]: disabled,
|
|
|
|
|
});
|
|
|
|
|
|
2016-12-01 18:57:19 +00:00
|
|
|
return (
|
2017-01-03 20:56:50 +00:00
|
|
|
<FormField {...formFieldProps} className={wrapperClassName} type="checkbox">
|
2016-12-16 15:54:49 +00:00
|
|
|
<label htmlFor={name} className={checkBoxClass}>
|
2016-12-21 17:25:54 +00:00
|
|
|
<input
|
|
|
|
|
checked={value}
|
|
|
|
|
className={`${checkBoxClass}__input`}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
id={name}
|
|
|
|
|
name={name}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
type="checkbox"
|
|
|
|
|
/>
|
|
|
|
|
<span className={checkBoxTickClass} />
|
2016-12-27 15:32:30 +00:00
|
|
|
<span className={`${checkBoxClass}__label`}>{children}</span>
|
2016-12-16 15:54:49 +00:00
|
|
|
</label>
|
|
|
|
|
</FormField>
|
2016-12-01 18:57:19 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 15:54:49 +00:00
|
|
|
export default Checkbox;
|