import { Combobox, Transition } from '@headlessui/react'; import { CheckIcon, ChevronUpDownIcon } from '@heroicons/react/20/solid'; import { Fragment, useState } from 'react'; export type ItemType = { id: number | string; name: string; slug?: string; }; type SearchSelectPropType = { items: ItemType[] | undefined; selected: ItemType; onChange: (item: { id: string; name: string; slug?: string }) => void; }; const SearchSelect = ({ items, onChange, selected }: SearchSelectPropType) => { const [query, setQuery] = useState(''); const filteredItems = query === '' ? items || [] : (items || []).filter((item) => item.name.toLowerCase().replace(/\s+/g, '').includes(query.toLowerCase().replace(/\s+/g, '')) ); return (
item.name} onChange={(event) => setQuery(event.target.value)} />
setQuery('')} > {filteredItems.length === 0 && query !== '' ? (
Nothing found.
) : ( filteredItems.map((item) => ( `wmx-relative wmx-mb-0 wmx-cursor-default wmx-capitalize wmx-select-none wmx-py-1 wmx-pl-10 wmx-pr-4 ${ active ? 'wmx-bg-primary wmx-text-white' : 'wmx-text-gray-900' }` } value={item} > {({ selected: s, active }) => ( <> {item.name} {s ? ( ) : null} )} )) )}
); }; export default SearchSelect;