fleet/frontend/components/forms/fields/Checkbox/Checkbox.jsx

51 lines
1.4 KiB
React
Raw Normal View History

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,
name: PropTypes.string,
onChange: PropTypes.func,
2016-12-16 15:54:49 +00:00
error: PropTypes.string,
hint: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
label: PropTypes.string,
value: PropTypes.bool,
2016-12-01 18:57:19 +00:00
};
static defaultProps = {
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;
const { children, className, name, value } = 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-01 18:57:19 +00:00
return (
2016-12-16 15:54:49 +00:00
<FormField {...formFieldProps} type="checkbox">
<label htmlFor={name} className={checkBoxClass}>
<input type="checkbox" name={name} id={name} className={`${checkBoxClass}__input`} onChange={handleChange} checked={value} />
<span className={`${checkBoxClass}__tick`} />
<div className={`${checkBoxClass}__label`}>{children}</div>
</label>
</FormField>
2016-12-01 18:57:19 +00:00
);
}
}
2016-12-16 15:54:49 +00:00
export default Checkbox;