import React, { useState } from "react"; // react-popper import { usePopper } from "react-popper"; // headless ui import { Combobox } from "@headlessui/react"; // icons import { Check, ChevronDown, Search } from "lucide-react"; // types import { DropdownProps } from "./types"; export type CustomSearchSelectProps = DropdownProps & { footerOption?: JSX.Element; onChange: any; options: | { value: any; query: string; content: React.ReactNode; }[] | undefined; } & ( | { multiple?: false; value: any } // if multiple is false, value can be anything | { multiple?: true; value: any[] | null; // if multiple is true, value should be an array } ); export const CustomSearchSelect = ({ customButtonClassName = "", buttonClassName = "", className = "", customButton, placement, disabled = false, footerOption, input = false, label, maxHeight = "md", multiple = false, noChevron = false, onChange, options, onOpen, optionsClassName = "", value, width = "auto", }: CustomSearchSelectProps) => { const [query, setQuery] = useState(""); const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: placement ?? "bottom-start", }); const filteredOptions = query === "" ? options : options?.filter((option) => option.query.toLowerCase().includes(query.toLowerCase())); const props: any = { value, onChange, disabled, }; if (multiple) props.multiple = true; return ( {({ open }: { open: boolean }) => { if (open && onOpen) onOpen(); return ( <> {customButton ? ( ) : ( )}
setQuery(e.target.value)} placeholder="Type to search..." displayValue={(assigned: any) => assigned?.name} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `flex items-center justify-between gap-2 cursor-pointer select-none truncate rounded px-1 py-1.5 ${ active || selected ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ active, selected }) => ( <> {option.content} {multiple ? (
) : ( )} )}
)) ) : (

No matching results

) ) : (

Loading...

)}
{footerOption}
); }}
); };