import { Switch } from "@headlessui/react"; import clsx from "clsx"; import { forwardRef, ReactNode, Ref, RefObject, useEffect, useImperativeHandle, useRef, useState } from "react"; import EntityIcon from "../../layouts/icons/EntityIcon"; import HintTooltip from "../tooltips/HintTooltip"; export interface RefInputCheckbox { input: RefObject; } interface Props { name?: string; title?: string; withLabel?: boolean; value?: boolean; setValue?: React.Dispatch>; className?: string; required?: boolean; disabled?: boolean; asToggle?: boolean; readOnly?: boolean; hint?: ReactNode; help?: string; icon?: string; autoFocus?: boolean; } const InputCheckbox = ( { name, title, withLabel = true, value, setValue, className, required, disabled, asToggle, readOnly, hint, help, icon, autoFocus }: Props, ref: Ref ) => { useImperativeHandle(ref, () => ({ input })); const input = useRef(null); const [actualValue, setActualValue] = useState(value ?? false); useEffect(() => { setActualValue(value ?? false); }, [value]); useEffect(() => { if (setValue && actualValue !== value) { setValue(actualValue); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [actualValue]); return (
{withLabel && title && ( )}
{icon && (
)} {asToggle ? ( ) : ( setActualValue(e.target.checked)} disabled={disabled || readOnly} autoFocus={autoFocus} checked={actualValue} className={clsx( (disabled || readOnly) && "cursor-not-allowed bg-gray-100", "mt-1 h-6 w-6 cursor-pointer rounded border-gray-300 text-accent-800 focus:ring-accent-500", className, icon && "pl-10" )} /> )}
); }; export default forwardRef(InputCheckbox);