fleet/frontend/components/forms/fields/Checkbox/Checkbox.tsx
Martavis Parker 3af64748ab
Pressing Enter on setup's Confirmation page (#1141)
* #917 fixed enter key for last page; TS overhaul

* #917 clean up

* Update frontend/components/forms/FormField/FormField.tsx

Co-authored-by: Zach Wasserman <zach@fleetdm.com>

* #917 fixed tests and linted

Co-authored-by: Zach Wasserman <zach@fleetdm.com>
2021-06-18 13:33:45 -07:00

71 lines
1.8 KiB
TypeScript

import React from "react";
import classnames from "classnames";
import { noop, pick } from "lodash";
import FormField from "components/forms/FormField";
import { IFormFieldProps } from "components/forms/FormField/FormField";
const baseClass = "kolide-checkbox";
interface ICheckboxProps {
children?: JSX.Element | Array<JSX.Element> | string;
className?: string;
disabled?: boolean;
name?: string;
onChange?: any; // TODO: meant to be an event; figure out type for this
value?: boolean;
wrapperClassName?: string;
indeterminate?: boolean;
}
const Checkbox = (props: ICheckboxProps) => {
const {
children,
className,
disabled = false,
name,
onChange = noop,
value,
wrapperClassName,
indeterminate,
} = props;
const handleChange = () => {
return onChange(!value);
};
const checkBoxClass = classnames(baseClass, className);
const formFieldProps = {
...pick(props, ["hint", "label", "error", "name"]),
className: wrapperClassName,
type: "checkbox",
} as IFormFieldProps;
const checkBoxTickClass = classnames(`${checkBoxClass}__tick`, {
[`${checkBoxClass}__tick--disabled`]: disabled,
[`${checkBoxClass}__tick--indeterminate`]: indeterminate,
});
return (
<FormField {...formFieldProps}>
<label htmlFor={name} className={checkBoxClass}>
<input
checked={value}
className={`${checkBoxClass}__input`}
disabled={disabled}
id={name}
name={name}
onChange={handleChange}
type="checkbox"
ref={(element) => {
element && indeterminate && (element.indeterminate = indeterminate);
}}
/>
<span className={checkBoxTickClass} />
<span className={`${checkBoxClass}__label`}>{children}</span>
</label>
</FormField>
);
};
export default Checkbox;