import { type ReactNode } from 'react'; import { voidFunction } from '../../utils/function'; import type { LunaticError } from '../../use-lunatic/type'; import type { LunaticBaseProps, LunaticComponentProps } from '../type'; import D from '../../i18n'; import { slottableComponent } from '../shared/HOC/slottableComponent'; import { Combobox } from '../shared/Combobox/Combobox'; import type { SuggesterOptionType } from './SuggesterType'; type Props = { className?: string; classNamePrefix?: string; placeholder?: string; onSelect: (option: SuggesterOptionType | null) => void; onBlur: () => void; onFocus: () => void; value: [SuggesterOptionType] | []; /** * @deprecated use createCustomizableField with ComboboxLabelRenderer as name. */ labelRenderer: LunaticComponentProps<'Suggester'>['labelRenderer']; /** * @deprecated use createCustomizableField with ComboboxOptionRenderer as name. */ optionRenderer: LunaticComponentProps<'Suggester'>['optionRenderer']; disabled?: boolean; readOnly?: boolean; id?: string; label?: ReactNode; declarations?: LunaticBaseProps['declarations']; description?: ReactNode; errors?: LunaticError[]; options: SuggesterOptionType[]; onSearch: (s: string) => void; onClear: () => void; search: string; state: 'loading' | 'error' | 'success'; required?: boolean; }; export const CustomSuggester = slottableComponent( 'Suggester', ({ className, classNamePrefix = 'lunatic', placeholder = D.SUGGESTER_PLACEHOLDER, onSelect = voidFunction, labelRenderer, optionRenderer, value, disabled, readOnly, id, search, label, declarations, description, errors, options, onSearch, onBlur, onClear, state, onFocus, required, }) => { const handleSelect = (id: string | null) => { if (id === null) { onClear(); } else { onSelect(options.find((o) => o.id === id) ?? null); } }; return ( onSearch(v ?? '')} disabled={disabled} readOnly={readOnly} options={options.map((o) => ({ value: o.id, ...o }))} shouldDisplayOptionsId={true} editable={true} onBlur={onBlur} onFocus={onFocus} onSelect={handleSelect} value={value[0]?.id ?? ''} search={search} optionRenderer={optionRenderer} labelRenderer={labelRenderer} placeholder={placeholder} label={label} declarations={declarations} description={description} errors={errors} required={required} /> ); } );