import classnames from 'classnames'; import { type ReactNode, useState } from 'react'; import { ComboboxContainer } from './ComboboxContainer'; import { ComboboxContent } from './ComboboxContent'; import type { ComboboxOptionType, ComboboxPanelProps, ComboboxSelectionProps, } from './ComboboxType'; import { KEYBOARD_KEY_CODES } from './constants'; import { ComboboxPanel } from './Panel/ComboboxPanel'; import { ComboboxClearButton } from './Selection/ComboboxClearButton'; import { ComboboxSelection } from './Selection/ComboboxSelection'; import { between, forceInt } from '../../../utils/number'; import { Label } from '../Label/Label'; import D from '../../../i18n'; import { slottableComponent } from '../HOC/slottableComponent'; import type { LunaticError } from '../../../use-lunatic/type'; import { Declarations } from '../Declarations/Declarations'; import type { LunaticBaseProps } from '../../type'; const EMPTY_SEARCH = ''; type Props = ComboboxSelectionProps & ComboboxPanelProps & { className?: string; classNamePrefix?: string; classStyle?: string; value: string | null; messageError?: string; getOptionValue?: (o: ComboboxOptionType) => string; label?: ReactNode; description?: ReactNode; declarations?: LunaticBaseProps['declarations']; errors?: LunaticError[]; onChange?: (s: string | null) => void; onSelect: (s: string | null) => void; onBlur?: () => void; onFocus?: () => void; options: ComboboxOptionType[]; readOnly?: boolean; isLoading?: boolean; }; function LunaticComboBox({ className, classNamePrefix, classStyle = 'default-style', placeholder = D.PLACEHOLDER, editable = false, disabled, readOnly, id, optionRenderer, labelRenderer, onChange, onSelect, value, options, shouldDisplayOptionsId = true, messageError, search = EMPTY_SEARCH, getOptionValue = getDefaultOptionValue, label, description, declarations, errors, onBlur, onFocus, isLoading, required, }: Props) { const [expanded, setExpanded] = useState(false); const [focused, setFocused] = useState(false); const selectedIndex = getIndexFromOptions(options, value, getOptionValue); const labelId = `label-${id}`; const handleFocus = () => { if (disabled || readOnly) { return; } onFocus?.(); setExpanded(true); setFocused(true); }; const handleBlur = () => { if (disabled || readOnly) { return; } setExpanded(false); setFocused(false); onBlur?.(); }; const handleSelect = (index: string | number, close = true) => { const indexNumber = between(forceInt(index), 0, options.length); const option = options[indexNumber]; if (close) { setExpanded(false); onBlur?.(); } onSelect(getOptionValue(option)); }; const handleChange = (s: string | null) => { onChange?.(s); }; const handleClear = () => { setExpanded(false); onChange?.(EMPTY_SEARCH); onSelect(null); }; const onKeyDown = (key: string) => { const length = options.length; switch (key) { case KEYBOARD_KEY_CODES.Tab: case KEYBOARD_KEY_CODES.Escape: setExpanded(false); return; case KEYBOARD_KEY_CODES.ArrowDown: handleSelect((selectedIndex ?? -1) + 1, false); return; case KEYBOARD_KEY_CODES.ArrowUp: handleSelect((selectedIndex ?? length) - 1, false); return; case KEYBOARD_KEY_CODES.Home: handleSelect(0, false); return; case KEYBOARD_KEY_CODES.End: handleSelect(length - 1, false); return; case KEYBOARD_KEY_CODES.Enter: setExpanded((v) => !v); return; } }; const showClearButton = !disabled || !readOnly; if (messageError) { return (