import { type KeyboardEventHandler, type ReactNode } from 'react'; import type { LunaticBaseProps } from '../../type'; import { slottableComponent } from '../HOC/slottableComponent'; import { Label } from '../Label/Label'; import { useKeyboardKey } from '../../../hooks/useKeyboardKey'; import { CustomInput } from '../../Input/Input'; type CheckboxOptionProps = { disabled?: boolean; readOnly?: boolean; checked?: boolean; id?: string; onCheck: (b: boolean) => void; label?: ReactNode; description?: LunaticBaseProps['description']; codeModality?: string; shortcut?: boolean; invalid?: boolean; detailAlwaysDisplayed?: boolean; detailLabel?: ReactNode; detailMaxLength?: number; detailValue?: string | null; onDetailChange?: (value: string) => void; shouldBeFiltered?: boolean; }; function LunaticCheckboxOption({ disabled, readOnly, checked, id, onCheck, label, description, detailAlwaysDisplayed, detailLabel, detailMaxLength, detailValue, onDetailChange, codeModality, shortcut, invalid, }: Readonly) { const isEnabled = !readOnly && !disabled; const hasDetail = !!onDetailChange; const hasKeyboardShortcut = Boolean(shortcut && codeModality && isEnabled); const onClickOption = () => { if (isEnabled) { onCheck(!checked); } }; const handleKeyDown: KeyboardEventHandler = (e) => { if (e.code === 'Space') { onClickOption(); } }; const labelId = `label-${id}`; useKeyboardKey( codeModality ? [codeModality] : [], (e) => { e.preventDefault(); onClickOption(); }, hasKeyboardShortcut, false // disable shortcut when editing a field ); return ( <> {hasDetail && (checked || detailAlwaysDisplayed) && ( )} ); } export const CheckboxOption = slottableComponent( 'CheckboxOption', LunaticCheckboxOption );