import { Fragment, ReactNode, 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"; // types import { TIssuePriorities } from "@plane/types"; // ui import { PriorityIcon, Tooltip } from "@plane/ui"; // constants import { ISSUE_PRIORITIES } from "@/constants/issue"; // helpers import { cn } from "@/helpers/common.helper"; // hooks import { useDropdown } from "@/hooks/use-dropdown"; import { usePlatformOS } from "@/hooks/use-platform-os"; // constants import { BACKGROUND_BUTTON_VARIANTS, BORDER_BUTTON_VARIANTS, BUTTON_VARIANTS_WITHOUT_TEXT } from "./constants"; // types import { TDropdownProps } from "./types"; 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 dropdownOnChange = (val: TIssuePriorities) => { onChange(val); handleClose(); }; const { handleClose, handleKeyDown, handleOnClick, searchInputKeyDown } = useDropdown({ dropdownRef, inputRef, isOpen, onClose, query, setIsOpen, setQuery, }); const ButtonToRender = BORDER_BUTTON_VARIANTS.includes(buttonVariant) ? BorderButton : BACKGROUND_BUTTON_VARIANTS.includes(buttonVariant) ? BackgroundButton : TransparentButton; 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

)}
)}
); };