import React, { useRef, useState } from "react"; import { DayPicker, Matcher } from "react-day-picker"; import { usePopper } from "react-popper"; import { Combobox } from "@headlessui/react"; import { CalendarDays, X } from "lucide-react"; // hooks import { cn } from "helpers/common.helper"; import { renderFormattedDate } from "helpers/date-time.helper"; import { useDropdownKeyDown } from "hooks/use-dropdown-key-down"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; // components import { DropdownButton } from "./buttons"; // helpers // types import { BUTTON_VARIANTS_WITH_TEXT } from "./constants"; import { TDropdownProps } from "./types"; // constants 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 = () => { if (!isOpen) return; setIsOpen(false); if (referenceElement) referenceElement.blur(); onClose && onClose(); }; const toggleDropdown = () => { if (!isOpen) onOpen(); setIsOpen((prevIsOpen) => !prevIsOpen); if (isOpen) onClose && onClose(); }; const dropdownOnChange = (val: Date | null) => { onChange(val); if (closeOnSelect) handleClose(); }; const handleKeyDown = useDropdownKeyDown(toggleDropdown, handleClose); const handleOnClick = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); toggleDropdown(); }; useOutsideClickDetector(dropdownRef, handleClose); 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" />
)}
); };