import { Fragment, ReactNode, useRef, useState } from "react";
import { Combobox } from "@headlessui/react";
import { usePopper } from "react-popper";
import { Check, ChevronDown, Search } from "lucide-react";
// hooks
import { useDropdownKeyDown } from "hooks/use-dropdown-key-down";
import useOutsideClickDetector from "hooks/use-outside-click-detector";
// icons
import { PriorityIcon } from "@plane/ui";
// helpers
import { cn } from "helpers/common.helper";
// types
import { TIssuePriorities } from "@plane/types";
import { TDropdownProps } from "./types";
// constants
import { ISSUE_PRIORITIES } from "constants/issue";
import { useTheme } from "next-themes";
type Props = TDropdownProps & {
button?: ReactNode;
dropdownArrow?: boolean;
highlightUrgent?: boolean;
onChange: (val: TIssuePriorities) => void;
value: TIssuePriorities;
};
type ButtonProps = {
className?: string;
dropdownArrow: boolean;
hideText?: boolean;
highlightUrgent: boolean;
priority: TIssuePriorities;
};
const BorderButton = (props: ButtonProps) => {
const { className, dropdownArrow, hideText = false, highlightUrgent, priority } = 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: "bg-custom-background-80 border-custom-border-300",
};
return (
{!hideText &&
{priorityDetails?.title}}
{dropdownArrow &&
}
);
};
const BackgroundButton = (props: ButtonProps) => {
const { className, dropdownArrow, hideText = false, highlightUrgent, priority } = 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",
};
return (
{!hideText &&
{priorityDetails?.title}}
{dropdownArrow &&
}
);
};
const TransparentButton = (props: ButtonProps) => {
const { className, dropdownArrow, hideText = false, highlightUrgent, priority } = 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: "",
};
return (
{!hideText &&
{priorityDetails?.title}}
{dropdownArrow &&
}
);
};
export const PriorityDropdown: React.FC = (props) => {
const {
button,
buttonClassName,
buttonContainerClassName,
buttonVariant,
className = "",
disabled = false,
dropdownArrow = false,
highlightUrgent = true,
onChange,
placement,
value,
tabIndex,
} = props;
// states
const [query, setQuery] = useState("");
const [isOpen, setIsOpen] = useState(false);
// refs
const dropdownRef = 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: (
),
}));
const filteredOptions =
query === "" ? options : options.filter((o) => o.query.toLowerCase().includes(query.toLowerCase()));
const openDropdown = () => {
setIsOpen(true);
if (referenceElement) referenceElement.focus();
};
const closeDropdown = () => setIsOpen(false);
const handleKeyDown = useDropdownKeyDown(openDropdown, closeDropdown, isOpen);
useOutsideClickDetector(dropdownRef, closeDropdown);
return (
{button ? (
) : (
)}
{isOpen && (
setQuery(e.target.value)}
placeholder="Search"
displayValue={(assigned: any) => assigned?.name}
/>
{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"}`
}
onClick={closeDropdown}
>
{({ selected }) => (
<>
{option.content}
{selected && }
>
)}
))
) : (
No matching results
)}
)}
);
};