import React, { useRef, useState } from "react"; import { Combobox } from "@headlessui/react"; import DatePicker from "react-datepicker"; import { usePopper } from "react-popper"; import { CalendarDays, X } from "lucide-react"; // hooks import { useDropdownKeyDown } from "hooks/use-dropdown-key-down"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; // ui import { Tooltip } from "@plane/ui"; // helpers import { renderFormattedDate } from "helpers/date-time.helper"; import { cn } from "helpers/common.helper"; // types import { TDropdownProps } from "./types"; type Props = TDropdownProps & { clearIconClassName?: string; icon?: React.ReactNode; isClearable?: boolean; minDate?: Date; maxDate?: Date; onChange: (val: Date | null) => void; value: Date | string | null; closeOnSelect?: boolean; }; type ButtonProps = { className?: string; clearIconClassName: string; date: string | Date | null; icon: React.ReactNode; isClearable: boolean; hideIcon?: boolean; hideText?: boolean; onClear: () => void; placeholder: string; tooltip: boolean; }; const BorderButton = (props: ButtonProps) => { const { className, clearIconClassName, date, icon, isClearable, hideIcon = false, hideText = false, onClear, placeholder, tooltip, } = props; return (
{!hideIcon && icon} {!hideText && {date ? renderFormattedDate(date) : placeholder}} {isClearable && ( { e.stopPropagation(); onClear(); }} /> )}
); }; const BackgroundButton = (props: ButtonProps) => { const { className, clearIconClassName, date, icon, isClearable, hideIcon = false, hideText = false, onClear, placeholder, tooltip, } = props; return (
{!hideIcon && icon} {!hideText && {date ? renderFormattedDate(date) : placeholder}} {isClearable && ( { e.stopPropagation(); onClear(); }} /> )}
); }; const TransparentButton = (props: ButtonProps) => { const { className, clearIconClassName, date, icon, isClearable, hideIcon = false, hideText = false, onClear, placeholder, tooltip, } = props; return (
{!hideIcon && icon} {!hideText && {date ? renderFormattedDate(date) : placeholder}} {isClearable && ( { e.stopPropagation(); onClear(); }} /> )}
); }; export const DateDropdown: React.FC = (props) => { const { buttonClassName = "", buttonContainerClassName, buttonVariant, className = "", clearIconClassName = "", closeOnSelect = true, disabled = false, hideIcon = false, icon = , isClearable = true, minDate, maxDate, onChange, placeholder = "Date", placement, tabIndex, tooltip = false, value, } = props; 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, }, }, ], }); const isDateSelected = value !== null && value !== undefined && value.toString().trim() !== ""; const openDropdown = () => { setIsOpen(true); if (referenceElement) referenceElement.focus(); }; const closeDropdown = () => setIsOpen(false); const handleKeyDown = useDropdownKeyDown(openDropdown, closeDropdown, isOpen); useOutsideClickDetector(dropdownRef, closeDropdown); return ( {isOpen && (
{ onChange(val); if (closeOnSelect) closeDropdown(); }} dateFormat="dd-MM-yyyy" minDate={minDate} maxDate={maxDate} calendarClassName="shadow-custom-shadow-rg rounded" inline />
)}
); };