import React, { useRef, useState } from "react"; import { DayPicker, Matcher } from "react-day-picker"; import { usePopper } from "react-popper"; import { CalendarDays, X } from "lucide-react"; import { Combobox } from "@headlessui/react"; // helpers import { cn } from "@/helpers/common.helper"; import { renderFormattedDate, getDate } from "@/helpers/date-time.helper"; // hooks import { useDropdown } from "@/hooks/use-dropdown"; // components import { DropdownButton } from "./buttons"; // constants import { BUTTON_VARIANTS_WITH_TEXT } from "./constants"; // types import { TDropdownProps } from "./types"; type Props = TDropdownProps & { clearIconClassName?: string; icon?: React.ReactNode; isClearable?: boolean; minDate?: Date; maxDate?: Date; onChange: (val: Date | null) => void; onClose?: () => void; value: Date | string | null; closeOnSelect?: boolean; }; export const DateDropdown: React.FC = (props) => { const { buttonClassName = "", buttonContainerClassName, buttonVariant, className = "", clearIconClassName = "", closeOnSelect = true, disabled = false, hideIcon = false, icon = , isClearable = true, minDate, maxDate, onChange, onClose, placeholder = "Date", placement, showTooltip = false, tabIndex, value, } = props; // states 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 && value.toString().trim() !== ""; const onOpen = () => { if (referenceElement) referenceElement.focus(); }; const { handleClose, handleKeyDown, handleOnClick } = useDropdown({ dropdownRef, isOpen, onClose, onOpen, setIsOpen, }); const dropdownOnChange = (val: Date | null) => { onChange(val); if (closeOnSelect) { handleClose(); referenceElement?.blur(); } }; const disabledDays: Matcher[] = []; if (minDate) disabledDays.push({ before: minDate }); if (maxDate) disabledDays.push({ after: maxDate }); return ( { if (e.key === "Enter") { if (!isOpen) handleKeyDown(e); } else handleKeyDown(e); }} disabled={disabled} > {isOpen && (
{ dropdownOnChange(date ?? null); }} showOutsideDays initialFocus disabled={disabledDays} mode="single" />
)}
); };