import { Fragment, ReactNode, useEffect, useRef, useState } from "react"; import { observer } from "mobx-react"; import { usePopper } from "react-popper"; import { Check, ChevronDown, Search } from "lucide-react"; import { Combobox } from "@headlessui/react"; // ui import { Spinner, StateGroupIcon } from "@plane/ui"; // helpers import { cn } from "@/helpers/common.helper"; // hooks import { useAppRouter, useProjectState } from "@/hooks/store"; import { useDropdown } from "@/hooks/use-dropdown"; // components import { DropdownButton } from "./buttons"; // constants import { BUTTON_VARIANTS_WITH_TEXT } from "./constants"; // types import { TDropdownProps } from "./types"; type Props = TDropdownProps & { button?: ReactNode; dropdownArrow?: boolean; dropdownArrowClassName?: string; onChange: (val: string) => void; onClose?: () => void; projectId: string; value: string; }; export const StateDropdown: React.FC = observer((props) => { const { button, buttonClassName, buttonContainerClassName, buttonVariant, className = "", disabled = false, dropdownArrow = false, dropdownArrowClassName = "", hideIcon = false, onChange, onClose, placement, projectId, showTooltip = false, tabIndex, value, } = props; // states const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = useState(false); const [stateLoader, setStateLoader] = useState(false); // refs const dropdownRef = useRef(null); const inputRef = useRef(null); // popper-js refs const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); // popper-js init const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: placement ?? "bottom-start", modifiers: [ { name: "preventOverflow", options: { padding: 12, }, }, ], }); // store hooks const { workspaceSlug } = useAppRouter(); const { fetchProjectStates, getProjectStates, getStateById } = useProjectState(); const statesList = getProjectStates(projectId); const defaultStateList = statesList?.find((state) => state.default); const stateValue = value ? value : defaultStateList?.id; const options = statesList?.map((state) => ({ value: state.id, query: `${state?.name}`, content: (
{state?.name}
), })); const filteredOptions = query === "" ? options : options?.filter((o) => o.query.toLowerCase().includes(query.toLowerCase())); const selectedState = stateValue ? getStateById(stateValue) : undefined; const onOpen = async () => { if (!statesList && workspaceSlug) { setStateLoader(true); await fetchProjectStates(workspaceSlug, projectId); setStateLoader(false); } }; const { handleClose, handleKeyDown, handleOnClick, searchInputKeyDown } = useDropdown({ dropdownRef, inputRef, isOpen, onClose, onOpen, query, setIsOpen, setQuery, }); useEffect(() => { if (projectId) onOpen(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [projectId]); const dropdownOnChange = (val: string) => { onChange(val); handleClose(); }; return ( {button ? ( ) : ( )} {isOpen && (
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} onKeyDown={searchInputKeyDown} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `flex w-full cursor-pointer select-none items-center justify-between gap-2 truncate rounded px-1 py-1.5 ${ active ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matches found

) ) : (

Loading...

)}
)}
); });