import { startTransition, useMemo } from 'react'; import { Search as SearchIcon } from 'lucide-react'; import * as RadixSelect from '@radix-ui/react-select'; import { CheckIcon, ChevronDownIcon } from '@radix-ui/react-icons'; import { Combobox, ComboboxItem, ComboboxList, ComboboxProvider, ComboboxCancel, } from '@ariakit/react'; import type { OptionWithIcon } from '~/common'; import { SelectTrigger, SelectValue, SelectScrollDownButton } from './Select'; import useCombobox from '~/hooks/Input/useCombobox'; import { cn } from '~/utils'; export default function ComboboxComponent({ selectedValue, displayValue, items, setValue, ariaLabel, searchPlaceholder, selectPlaceholder, isCollapsed, SelectIcon, }: { ariaLabel: string; displayValue?: string; selectedValue: string; searchPlaceholder?: string; selectPlaceholder?: string; items: OptionWithIcon[] | string[]; setValue: (value: string) => void; isCollapsed: boolean; SelectIcon?: React.ReactNode; }) { const options: OptionWithIcon[] = useMemo(() => { if (!items) { return []; } return items.map((option: string | OptionWithIcon) => { if (typeof option === 'string') { return { label: option, value: option }; } return option; }); }, [items]); const { open, setOpen, setSearchValue, matches } = useCombobox({ value: selectedValue, options, }); return ( { startTransition(() => { setSearchValue(value); }); }} > span]:line-clamp-1 [&>span]:flex [&>span]:w-full [&>span]:items-center [&>span]:gap-1 [&>span]:truncate [&_svg]:h-4 [&_svg]:w-4 [&_svg]:shrink-0', isCollapsed ? 'flex h-9 w-9 shrink-0 items-center justify-center p-0 [&>span]:w-auto [&>svg]:hidden' : '', 'bg-white text-black hover:bg-gray-50 dark:bg-gray-850 dark:text-white', )} >
{SelectIcon ? SelectIcon : }
{selectedValue ? displayValue ?? selectedValue : selectPlaceholder && selectPlaceholder}
{ event.preventDefault(); event.stopPropagation(); }} />
{matches.map(({ label, value, icon }) => (
{icon && icon}
{label}
))}
); }