fleet/frontend/components/forms/fields/Checkbox/Checkbox.tsx
Gabriel Hernandez 75212d81d4
Feat UI add end user auth to controls page (#11991)
relates to #11002

Implements the UI for mdm macos setup end user authentication page.


![image](https://github.com/fleetdm/fleet/assets/1153709/1af6c5d7-99d0-401d-9938-a78617eca817)


![image](https://github.com/fleetdm/fleet/assets/1153709/8f0ed8cc-63f5-425b-8f3a-f2f83ed018f7)



- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
- [x] Manual QA for all new/changed functionality
2023-06-02 14:14:39 +01:00

95 lines
2.3 KiB
TypeScript

import React, { ReactNode } 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?: ReactNode;
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;
isLeftLabel?: boolean;
}
const Checkbox = (props: ICheckboxProps) => {
const {
children,
className,
disabled = false,
name,
onChange = noop,
onBlur = noop,
value,
wrapperClassName,
indeterminate,
parseTarget,
tooltip,
isLeftLabel,
} = props;
const handleChange = () => {
if (parseTarget) {
// Returns both name and value
return onChange({ name, value: !value });
}
return onChange(!value);
};
const checkBoxClass = classnames(
{ inverse: isLeftLabel },
className,
baseClass
);
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={`${baseClass}__input`}
disabled={disabled}
id={name}
name={name}
onChange={handleChange}
onBlur={onBlur}
type="checkbox"
/>
<span className={checkBoxTickClass} />
{tooltip ? (
<span className={`${baseClass}__label-tooltip tooltip`}>
<TooltipWrapper tipContent={tooltip}>
{children as string}
</TooltipWrapper>
</span>
) : (
<span className={`${baseClass}__label`}>{children} </span>
)}
</label>
</FormField>
);
};
export default Checkbox;