import { Listbox, ListboxButton, ListboxOption, ListboxOptions, Transition } from "@headlessui/react"; import { MagnifyingGlassIcon } from "@heroicons/react/24/outline"; import { CheckCircleIcon, ChevronDownIcon } from "@heroicons/react/24/solid"; import { Fragment, memo, type ReactNode, useState } from "react"; import { Input } from "@/components/Shared/UI"; import cn from "@/helpers/cn"; interface SelectProps { className?: string; defaultValue?: string; iconClassName?: string; onChange: (value: any) => any; options?: { disabled?: boolean; helper?: string; icon?: string; label: string; htmlLabel?: ReactNode; selected?: boolean; value: number | string; }[]; showSearch?: boolean; } const Select = ({ className, defaultValue, iconClassName, onChange, options, showSearch = false }: SelectProps) => { const [searchValue, setSearchValue] = useState(""); const selected = options?.find((option) => option.selected) || options?.[0]; return (
{selected?.icon && ( {selected?.label} )} {selected?.htmlLabel || selected?.label} {showSearch ? (
} onChange={(event) => { setSearchValue(event.target.value); }} placeholder="Search" type="text" value={searchValue} />
) : null} {options ?.filter((option) => option.label.toLowerCase().includes(searchValue.toLowerCase()) ) .map((option, id) => ( cn( { "dropdown-active": focus }, "m-2 cursor-pointer rounded-lg" ) } disabled={option.disabled} key={id} value={option.value} > {({ selected }) => (
{option.icon && ( {option.label} )} {option.htmlLabel || option.label} {selected ? ( ) : null} {option.helper ? ( {option.helper} ) : null}
)}
))}
); }; export default memo(Select);