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 = ({ display, options, onChange, value, multiple: canSelectMultiple, }) => { const [query, setQuery] = useState(""); const filteredOptions = query === "" ? options : options.filter((option) => option.name.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) => ( <> {display}
{value ? options.find((option) => option.value === value)?.name ?? "None" : `Select ${display}`} setQuery(event.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} /> {filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( classNames( active ? "bg-gray-50" : "bg-white", "relative rounded cursor-default select-none py-2 px-3" ) } value={option.value} >
{option.name}
)) ) : (
No results found
)}
)}
); }; export default SearchListbox;