2021-04-12 13:32:25 +00:00
|
|
|
import React, { Component } from "react";
|
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
import classnames from "classnames";
|
|
|
|
|
import { noop, pick } from "lodash";
|
2016-12-16 15:54:49 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import FormField from "components/forms/FormField";
|
2016-12-01 18:57:19 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const baseClass = "kolide-checkbox";
|
2016-12-01 18:57:19 +00:00
|
|
|
|
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,
|
2021-05-13 14:30:42 +00:00
|
|
|
indeterminate: PropTypes.bool,
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
render() {
|
2016-12-16 15:54:49 +00:00
|
|
|
const { handleChange } = this;
|
2021-04-12 13:32:25 +00:00
|
|
|
const {
|
|
|
|
|
children,
|
|
|
|
|
className,
|
|
|
|
|
disabled,
|
|
|
|
|
name,
|
|
|
|
|
value,
|
|
|
|
|
wrapperClassName,
|
2021-05-13 14:30:42 +00:00
|
|
|
indeterminate,
|
2021-04-12 13:32:25 +00:00
|
|
|
} = this.props;
|
2016-12-01 18:57:19 +00:00
|
|
|
const checkBoxClass = classnames(baseClass, className);
|
2021-04-12 13:32:25 +00:00
|
|
|
const formFieldProps = pick(this.props, ["hint", "label", "error", "name"]);
|
2016-12-16 15:54:49 +00:00
|
|
|
|
2016-12-21 17:25:54 +00:00
|
|
|
const checkBoxTickClass = classnames(`${checkBoxClass}__tick`, {
|
|
|
|
|
[`${checkBoxClass}__tick--disabled`]: disabled,
|
2021-05-13 14:30:42 +00:00
|
|
|
[`${checkBoxClass}__tick--indeterminate`]: indeterminate,
|
2016-12-21 17:25:54 +00:00
|
|
|
});
|
|
|
|
|
|
2016-12-01 18:57:19 +00:00
|
|
|
return (
|
2021-04-12 13:32:25 +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"
|
2021-05-13 14:30:42 +00:00
|
|
|
ref={(element) => {
|
|
|
|
|
element && (element.indeterminate = indeterminate);
|
|
|
|
|
}}
|
2016-12-21 17:25:54 +00:00
|
|
|
/>
|
|
|
|
|
<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;
|