import { Fragment, ReactNode, useRef, useState } from "react"; import { observer } from "mobx-react-lite"; import { ChevronDown } from "lucide-react"; import { Combobox } from "@headlessui/react"; // hooks import { ContrastIcon } from "@plane/ui"; import { cn } from "@/helpers/common.helper"; import { useCycle } from "@/hooks/store"; import { useDropdownKeyDown } from "@/hooks/use-dropdown-key-down"; import useOutsideClickDetector from "@/hooks/use-outside-click-detector"; // components import { DropdownButton } from "../buttons"; // icons // helpers // types import { BUTTON_VARIANTS_WITH_TEXT } from "../constants"; import { TDropdownProps } from "../types"; // constants import { CycleOptions } from "./cycle-options"; type Props = TDropdownProps & { button?: ReactNode; dropdownArrow?: boolean; dropdownArrowClassName?: string; onChange: (val: string | null) => void; onClose?: () => void; projectId: string; value: string | null; }; export const CycleDropdown: React.FC = observer((props) => { const { button, buttonClassName, buttonContainerClassName, buttonVariant, className = "", disabled = false, dropdownArrow = false, dropdownArrowClassName = "", hideIcon = false, onChange, onClose, placeholder = "", placement, projectId, showTooltip = false, tabIndex, value, } = props; // states const [isOpen, setIsOpen] = useState(false); const { getCycleNameById } = useCycle(); // refs const dropdownRef = useRef(null); // popper-js refs const [referenceElement, setReferenceElement] = useState(null); const selectedName = value ? getCycleNameById(value) : null; const handleClose = () => { if (!isOpen) return; setIsOpen(false); onClose && onClose(); }; const toggleDropdown = () => { setIsOpen((prevIsOpen) => !prevIsOpen); if (isOpen) onClose && onClose(); }; const dropdownOnChange = (val: string | null) => { onChange(val); handleClose(); }; const handleKeyDown = useDropdownKeyDown(toggleDropdown, handleClose); const handleOnClick = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); toggleDropdown(); }; useOutsideClickDetector(dropdownRef, handleClose); return ( {button ? ( ) : ( )} {isOpen && ( )} ); });