fleet/frontend/components/forms/fields/Checkbox/Checkbox.tsx
Martavis Parker 33c5f0651c
New tooltips! (#4326)
* Allow sort by more than one key

* created custom tooltip component

* remove unused code

* fixed style for more layouts

* added tooltip to query side panel

* tooltips added to setting form

* finished settings form

* added tooltip for perf impact table headers

* tooltip for pack table and user form

* tooltip on manage policies page

* tooltip for manage schedules

* tooltip for automations; spacing for form input

* tooltip for automations modal

* user form; fixed input with icon component

* more user form tooltips

* tooltip for homepage; style fixes

* replaced many more tooltips with new version

* added story for tooltip

* added position prop

* fixed tests

* re-work how we click react-select dropdowns

* forcing the update button click

* trying a blur

* fixed typo

* trying blur on another element

* temp check-in

* replaced tooltip from host details software

* more consolidation of tooltip use for software

* fixed settings flow test

Co-authored-by: Tomas Touceda <chiiph@gmail.com>
2022-02-28 13:25:06 -08:00

92 lines
2.3 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";
import TooltipWrapper from "components/TooltipWrapper";
const baseClass = "fleet-checkbox";
export 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
onBlur?: any;
value?: boolean;
wrapperClassName?: string;
indeterminate?: boolean;
parseTarget?: boolean;
tooltip?: string;
}
const Checkbox = (props: ICheckboxProps) => {
const {
children,
className,
disabled = false,
name,
onChange = noop,
onBlur = noop,
value,
wrapperClassName,
indeterminate,
parseTarget,
tooltip,
} = props;
const handleChange = () => {
if (parseTarget) {
// Returns both name and value
return onChange({ name, value: !value });
}
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}
onBlur={onBlur}
type="checkbox"
ref={(element) => {
element && indeterminate && (element.indeterminate = indeterminate);
}}
/>
<span className={checkBoxTickClass} />
<span className={`${checkBoxClass}__label`}>
{tooltip ? (
<TooltipWrapper tipContent={tooltip}>
{children as string}
</TooltipWrapper>
) : (
<>{children}</>
)}
</span>
</label>
</FormField>
);
};
export default Checkbox;