import { FC, useRef, Fragment, useState } from "react"; import { Info, Check, ChevronDown } from "lucide-react"; import { Listbox, Transition } from "@headlessui/react"; import { TEstimatePointsObject } from "@plane/types"; import { Tooltip } from "@plane/ui"; // helpers import { cn } from "@/helpers/common.helper"; // hooks import useDynamicDropdownPosition from "@/hooks/use-dynamic-dropdown"; import useOutsideClickDetector from "@/hooks/use-outside-click-detector"; type TEstimatePointDropdown = { options: TEstimatePointsObject[]; error: string | undefined; callback: (estimateId: string) => void; }; export const EstimatePointDropdown: FC = (props) => { const { options, error, callback } = props; // states const [isDropdownOpen, setIsDropdownOpen] = useState(false); const [selectedOption, setSelectedOption] = useState(undefined); // ref const dropdownContainerRef = useRef(null); const buttonRef = useRef(null); const dropdownRef = useRef(null); useDynamicDropdownPosition(isDropdownOpen, () => setIsDropdownOpen(false), buttonRef, dropdownRef); useOutsideClickDetector(dropdownContainerRef, () => setIsDropdownOpen(false)); // derived values const selectedValue = selectedOption ? selectedOption === "none" ? { id: undefined, key: undefined, value: "None", } : options.find((option) => option?.id === selectedOption) : undefined; return (
{ setSelectedOption(selectedOption); callback(selectedOption); setIsDropdownOpen(false); }} className="w-full flex-shrink-0 text-left" > setIsDropdownOpen((prev) => !prev)} className={cn( "relative w-full rounded border flex items-center gap-3 p-2.5", error ? `border-red-500` : `border-custom-border-200` )} >
{selectedValue?.value || "Select an estimate point"}
{error && ( <>
)}
None
{selectedOption === "none" && }
{options.map((option) => (
{option.value}
{selectedOption === option?.id && }
))}
); };