import { Fragment, ReactNode, useEffect, useRef, useState } from "react"; import { useTheme } from "next-themes"; import { usePopper } from "react-popper"; import { Check, ChevronDown, Search } from "lucide-react"; import { Combobox } from "@headlessui/react"; import { TIssuePriorities } from "@plane/types"; // hooks import { PriorityIcon, Tooltip } from "@plane/ui"; import { ISSUE_PRIORITIES } from "@/constants/issue"; import { cn } from "@/helpers/common.helper"; import { useDropdownKeyDown } from "@/hooks/use-dropdown-key-down"; import useOutsideClickDetector from "@/hooks/use-outside-click-detector"; import { usePlatformOS } from "@/hooks/use-platform-os"; // icons // helpers // types import { BACKGROUND_BUTTON_VARIANTS, BORDER_BUTTON_VARIANTS, BUTTON_VARIANTS_WITHOUT_TEXT } from "./constants"; import { TDropdownProps } from "./types"; // constants type Props = TDropdownProps & { button?: ReactNode; dropdownArrow?: boolean; dropdownArrowClassName?: string; highlightUrgent?: boolean; onChange: (val: TIssuePriorities) => void; onClose?: () => void; value: TIssuePriorities; }; type ButtonProps = { className?: string; dropdownArrow: boolean; dropdownArrowClassName: string; hideIcon?: boolean; hideText?: boolean; isActive?: boolean; highlightUrgent: boolean; priority: TIssuePriorities; showTooltip: boolean; }; const BorderButton = (props: ButtonProps) => { const { className, dropdownArrow, dropdownArrowClassName, hideIcon = false, hideText = false, highlightUrgent, priority, showTooltip, } = props; const priorityDetails = ISSUE_PRIORITIES.find((p) => p.key === priority); const priorityClasses = { urgent: "bg-red-500/20 text-red-950 border-red-500", high: "bg-orange-500/20 text-orange-950 border-orange-500", medium: "bg-yellow-500/20 text-yellow-950 border-yellow-500", low: "bg-custom-primary-100/20 text-custom-primary-950 border-custom-primary-100", none: "hover:bg-custom-background-80 border-custom-border-300", }; const { isMobile } = usePlatformOS(); return ( ); }; const BackgroundButton = (props: ButtonProps) => { const { className, dropdownArrow, dropdownArrowClassName, hideIcon = false, hideText = false, highlightUrgent, priority, showTooltip, } = props; const priorityDetails = ISSUE_PRIORITIES.find((p) => p.key === priority); const priorityClasses = { urgent: "bg-red-500/20 text-red-950", high: "bg-orange-500/20 text-orange-950", medium: "bg-yellow-500/20 text-yellow-950", low: "bg-blue-500/20 text-blue-950", none: "bg-custom-background-80", }; const { isMobile } = usePlatformOS(); return ( ); }; const TransparentButton = (props: ButtonProps) => { const { className, dropdownArrow, dropdownArrowClassName, hideIcon = false, hideText = false, isActive = false, highlightUrgent, priority, showTooltip, } = props; const priorityDetails = ISSUE_PRIORITIES.find((p) => p.key === priority); const priorityClasses = { urgent: "text-red-950", high: "text-orange-950", medium: "text-yellow-950", low: "text-blue-950", none: "hover:text-custom-text-300", }; const { isMobile } = usePlatformOS(); return ( ); }; export const PriorityDropdown: React.FC = (props) => { const { button, buttonClassName, buttonContainerClassName, buttonVariant, className = "", disabled = false, dropdownArrow = false, dropdownArrowClassName = "", hideIcon = false, highlightUrgent = true, onChange, onClose, placement, showTooltip = false, tabIndex, value, } = props; // states const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = 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, }, }, ], }); // next-themes // TODO: remove this after new theming implementation const { resolvedTheme } = useTheme(); const options = ISSUE_PRIORITIES.map((priority) => ({ value: priority.key, query: priority.key, content: (
{priority.title}
), })); const filteredOptions = query === "" ? options : options.filter((o) => o.query.toLowerCase().includes(query.toLowerCase())); const handleClose = () => { if (!isOpen) return; setIsOpen(false); onClose && onClose(); }; const toggleDropdown = () => { setIsOpen((prevIsOpen) => !prevIsOpen); if (isOpen) onClose && onClose(); }; const dropdownOnChange = (val: TIssuePriorities) => { onChange(val); handleClose(); }; const handleKeyDown = useDropdownKeyDown(toggleDropdown, handleClose); const handleOnClick = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); toggleDropdown(); }; const searchInputKeyDown = (e: React.KeyboardEvent) => { if (query !== "" && e.key === "Escape") { e.stopPropagation(); setQuery(""); } }; useOutsideClickDetector(dropdownRef, handleClose); const ButtonToRender = BORDER_BUTTON_VARIANTS.includes(buttonVariant) ? BorderButton : BACKGROUND_BUTTON_VARIANTS.includes(buttonVariant) ? BackgroundButton : TransparentButton; useEffect(() => { if (isOpen && inputRef.current) { inputRef.current.focus(); } }, [isOpen]); return ( {button ? ( ) : ( )} {isOpen && (
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} onKeyDown={searchInputKeyDown} />
{filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `w-full truncate flex items-center justify-between gap-2 rounded px-1 py-1.5 cursor-pointer select-none ${ active ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

)}
)}
); };