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

63 lines
1.7 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,
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,
wrapperClassName: PropTypes.string,
2016-12-01 18:57:19 +00:00
};
static defaultProps = {
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;
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']);
const checkBoxTickClass = classnames(`${checkBoxClass}__tick`, {
[`${checkBoxClass}__tick--disabled`]: disabled,
});
2016-12-01 18:57:19 +00:00
return (
<FormField {...formFieldProps} className={wrapperClassName} type="checkbox">
2016-12-16 15:54:49 +00:00
<label htmlFor={name} className={checkBoxClass}>
<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;