import React, { useState } from "react"; // headless ui import { Transition, Combobox } from "@headlessui/react"; // common import { classNames } from "constants/common"; // types import type { Props } from "./types"; const SearchListbox: React.FC = ({ title, options, onChange, value, multiple: canSelectMultiple, icon, width = "sm", optionsFontsize, buttonClassName, optionsClassName, }) => { const [query, setQuery] = useState(""); const filteredOptions = query === "" ? options : options?.filter((option) => option.display.toLowerCase().includes(query.toLowerCase())); const props: any = { value, onChange, }; if (canSelectMultiple) { props.value = props.value ?? []; props.onChange = (value: string[]) => { onChange(value); }; props.multiple = true; } return ( {({ open }: any) => ( <> {title} {icon ?? null} {Array.isArray(value) ? value .map((v) => options?.find((option) => option.value === v)?.display) .join(", ") || title : options?.find((option) => option.value === value)?.display || title} setQuery(event.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `${ active ? "bg-indigo-50" : "" } cursor-pointer select-none truncate text-gray-900 p-2` } value={option.value} > {option.element ?? option.display} )) ) : (

No {title.toLowerCase()} found

) ) : (

Loading...

)}
)}
); }; export default SearchListbox;