import { useEffect } from 'react'; import type { SetFormField } from '@douglasneuroinformatics/libui-form-types'; import { match } from 'ts-pattern'; import type { Simplify } from 'type-fest'; import { SetFieldListbox } from './SetFieldListbox.tsx'; import { SetFieldSelect } from './SetFieldSelect.tsx'; import type { BaseFieldComponentProps } from '../types.ts'; export type SetFieldProps = Simplify> & SetFormField>>; export const SetField = (props: SetFieldProps) => { useEffect(() => { if (!props.value) { props.setValue(new Set([])); } }, [props.value]); const handleCheckedChange = (option: T, isChecked: boolean) => { if (isChecked) { const updatedValue = new Set(props.value); updatedValue.delete(option); props.setValue(updatedValue); } else { const updatedValue = new Set(props.value); updatedValue.add(option); props.setValue(updatedValue); } }; return match(props) .with({ variant: 'select' }, (props) => ) .with({ variant: 'listbox' }, (props) => ) .exhaustive(); };