import React, { useRef, useState } from "react";
import { Popover } 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";
// helpers
import { renderFormattedDate } from "helpers/date-time.helper";
import { cn } from "helpers/common.helper";
// types
import { TDropdownProps } from "./types";
type Props = TDropdownProps & {
icon?: React.ReactNode;
isClearable?: boolean;
minDate?: Date;
maxDate?: Date;
onChange: (val: Date | null) => void;
placeholder: string;
value: Date | string | null;
closeOnSelect?: boolean;
};
type ButtonProps = {
className?: string;
date: string | Date | null;
icon: React.ReactNode;
isClearable: boolean;
hideText?: boolean;
onClear: () => void;
placeholder: string;
};
const BorderButton = (props: ButtonProps) => {
const { className, date, icon, isClearable, hideText = false, onClear, placeholder } = props;
return (
{icon}
{!hideText && {date ? renderFormattedDate(date) : placeholder}}
{isClearable && (
{
e.stopPropagation();
onClear();
}}
/>
)}
);
};
const BackgroundButton = (props: ButtonProps) => {
const { className, date, icon, isClearable, hideText = false, onClear, placeholder } = props;
return (
{icon}
{!hideText && {date ? renderFormattedDate(date) : placeholder}}
{isClearable && (
{
e.stopPropagation();
onClear();
}}
/>
)}
);
};
const TransparentButton = (props: ButtonProps) => {
const { className, date, icon, isClearable, hideText = false, onClear, placeholder } = props;
return (
{icon}
{!hideText && {date ? renderFormattedDate(date) : placeholder}}
{isClearable && (
{
e.stopPropagation();
onClear();
}}
/>
)}
);
};
export const DateDropdown: React.FC = (props) => {
const {
buttonClassName = "",
buttonContainerClassName,
buttonVariant,
className = "",
disabled = false,
icon = ,
isClearable = true,
minDate,
maxDate,
onChange,
placeholder,
placement,
value,
closeOnSelect = true,
tabIndex,
} = 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
/>
)}
);
};