2024-06-10 09:43:10 +00:00
|
|
|
"use client";
|
|
|
|
|
2024-02-28 09:48:11 +00:00
|
|
|
import { Fragment, ReactNode, useRef, useState } from "react";
|
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { ChevronDown } from "lucide-react";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { Combobox } from "@headlessui/react";
|
2024-05-08 17:31:20 +00:00
|
|
|
// ui
|
2024-03-06 13:09:14 +00:00
|
|
|
import { ContrastIcon } from "@plane/ui";
|
2024-05-08 17:31:20 +00:00
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { cn } from "@/helpers/common.helper";
|
2024-05-08 17:31:20 +00:00
|
|
|
// hooks
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useCycle } from "@/hooks/store";
|
2024-05-22 07:15:51 +00:00
|
|
|
import { useDropdown } from "@/hooks/use-dropdown";
|
2024-05-08 17:31:20 +00:00
|
|
|
// local components and constants
|
2024-02-28 09:48:11 +00:00
|
|
|
import { DropdownButton } from "../buttons";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { BUTTON_VARIANTS_WITH_TEXT } from "../constants";
|
2024-02-28 09:48:11 +00:00
|
|
|
import { TDropdownProps } from "../types";
|
|
|
|
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<Props> = observer((props) => {
|
|
|
|
const {
|
|
|
|
button,
|
|
|
|
buttonClassName,
|
|
|
|
buttonContainerClassName,
|
|
|
|
buttonVariant,
|
|
|
|
className = "",
|
|
|
|
disabled = false,
|
|
|
|
dropdownArrow = false,
|
|
|
|
dropdownArrowClassName = "",
|
|
|
|
hideIcon = false,
|
|
|
|
onChange,
|
|
|
|
onClose,
|
2024-03-22 13:09:06 +00:00
|
|
|
placeholder = "",
|
2024-02-28 09:48:11 +00:00
|
|
|
placement,
|
|
|
|
projectId,
|
|
|
|
showTooltip = false,
|
|
|
|
tabIndex,
|
|
|
|
value,
|
|
|
|
} = props;
|
|
|
|
// states
|
|
|
|
|
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
const { getCycleNameById } = useCycle();
|
|
|
|
// refs
|
|
|
|
const dropdownRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
// popper-js refs
|
|
|
|
const [referenceElement, setReferenceElement] = useState<HTMLButtonElement | null>(null);
|
|
|
|
|
|
|
|
const selectedName = value ? getCycleNameById(value) : null;
|
|
|
|
|
2024-05-22 07:15:51 +00:00
|
|
|
const { handleClose, handleKeyDown, handleOnClick } = useDropdown({
|
|
|
|
dropdownRef,
|
|
|
|
isOpen,
|
|
|
|
onClose,
|
|
|
|
setIsOpen,
|
|
|
|
});
|
2024-02-28 09:48:11 +00:00
|
|
|
|
|
|
|
const dropdownOnChange = (val: string | null) => {
|
|
|
|
onChange(val);
|
|
|
|
handleClose();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Combobox
|
|
|
|
as="div"
|
|
|
|
ref={dropdownRef}
|
|
|
|
tabIndex={tabIndex}
|
|
|
|
className={cn("h-full", className)}
|
|
|
|
value={value}
|
|
|
|
onChange={dropdownOnChange}
|
|
|
|
disabled={disabled}
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
>
|
|
|
|
<Combobox.Button as={Fragment}>
|
|
|
|
{button ? (
|
|
|
|
<button
|
|
|
|
ref={setReferenceElement}
|
|
|
|
type="button"
|
|
|
|
className={cn(
|
|
|
|
"clickable block h-full w-full outline-none hover:bg-custom-background-80",
|
|
|
|
buttonContainerClassName
|
|
|
|
)}
|
|
|
|
onClick={handleOnClick}
|
|
|
|
>
|
|
|
|
{button}
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
ref={setReferenceElement}
|
|
|
|
type="button"
|
|
|
|
className={cn(
|
|
|
|
"clickable block h-full max-w-full outline-none hover:bg-custom-background-80",
|
|
|
|
{
|
|
|
|
"cursor-not-allowed text-custom-text-200": disabled,
|
|
|
|
"cursor-pointer": !disabled,
|
|
|
|
},
|
|
|
|
buttonContainerClassName
|
|
|
|
)}
|
|
|
|
onClick={handleOnClick}
|
|
|
|
>
|
|
|
|
<DropdownButton
|
|
|
|
className={buttonClassName}
|
|
|
|
isActive={isOpen}
|
|
|
|
tooltipHeading="Cycle"
|
|
|
|
tooltipContent={selectedName ?? placeholder}
|
|
|
|
showTooltip={showTooltip}
|
|
|
|
variant={buttonVariant}
|
|
|
|
>
|
|
|
|
{!hideIcon && <ContrastIcon className="h-3 w-3 flex-shrink-0" />}
|
2024-03-22 13:09:06 +00:00
|
|
|
{BUTTON_VARIANTS_WITH_TEXT.includes(buttonVariant) && (!!selectedName || !!placeholder) && (
|
2024-05-08 17:31:20 +00:00
|
|
|
<span className="max-w-40 flex-grow truncate">{selectedName ?? placeholder}</span>
|
2024-02-28 09:48:11 +00:00
|
|
|
)}
|
|
|
|
{dropdownArrow && (
|
|
|
|
<ChevronDown className={cn("h-2.5 w-2.5 flex-shrink-0", dropdownArrowClassName)} aria-hidden="true" />
|
|
|
|
)}
|
|
|
|
</DropdownButton>
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Combobox.Button>
|
|
|
|
{isOpen && (
|
|
|
|
<CycleOptions isOpen={isOpen} projectId={projectId} placement={placement} referenceElement={referenceElement} />
|
|
|
|
)}
|
|
|
|
</Combobox>
|
|
|
|
);
|
|
|
|
});
|