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

80 lines
2 KiB
React
Raw Normal View History

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
import FormField from "components/forms/FormField";
2016-12-01 18:57:19 +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,
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,
indeterminate: PropTypes.bool,
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);
};
render() {
2016-12-16 15:54:49 +00:00
const { handleChange } = this;
const {
children,
className,
disabled,
name,
value,
wrapperClassName,
indeterminate,
} = this.props;
2016-12-01 18:57:19 +00:00
const checkBoxClass = classnames(baseClass, className);
const formFieldProps = pick(this.props, ["hint", "label", "error", "name"]);
2016-12-16 15:54:49 +00:00
const checkBoxTickClass = classnames(`${checkBoxClass}__tick`, {
[`${checkBoxClass}__tick--disabled`]: disabled,
[`${checkBoxClass}__tick--indeterminate`]: indeterminate,
});
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"
ref={(element) => {
element && (element.indeterminate = indeterminate);
}}
/>
<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;