import { Switch } from "@headlessui/react"; import clsx from "clsx"; import { ReactNode, useEffect, useState } from "react"; import EntityIcon from "~/components/layouts/icons/EntityIcon"; import HintTooltip from "~/components/ui/tooltips/HintTooltip"; import { Checkbox } from "~/ui/components/Checkbox"; interface Props { name?: string; title?: string; withLabel?: boolean; value?: boolean | undefined; setValue?: React.Dispatch>; className?: string; required?: boolean; disabled?: boolean; asToggle?: boolean; readOnly?: boolean; hint?: ReactNode; help?: string; icon?: string; autoFocus?: boolean; } export default function InputCheckbox({ name, title, withLabel = true, value, setValue, className, required, disabled, asToggle, readOnly, hint, help, icon, autoFocus, }: Props) { // useImperativeHandle(ref, () => ({ input })); // const input = useRef(null); // Subframe's Checkbox is always controlled (no uncontrolled/defaultChecked path), so the // shim owns the checked state and mirrors it back through setValue — preserving the // original controlled+uncontrolled public API. 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]); const accessibleName = title || name || undefined; return (
{withLabel && title && ( )}
{icon && (
)} {asToggle ? ( // asToggle renders a switch (not a checkbox) — left on the legacy headless-ui Switch; // switch migration is tracked separately (Subframe Switch). ) : ( above already labels this field; Subframe's own label slot is // hidden to avoid a duplicate. aria-label is the accessible name when no label shows. hideLabel={true} aria-label={!withLabel || !title ? accessibleName : undefined} onCheckedChange={(checked) => setActualValue(Boolean(checked))} disabled={disabled || readOnly} autoFocus={autoFocus} checked={actualValue} className={clsx((disabled || readOnly) && "cursor-not-allowed", "mt-1", icon && "pl-10")} /> )}
); }