import React, { useState } from "react"; import { CalendarIcon } from "lucide-react"; import dayjs from "dayjs"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Calendar, NO_BORDER } from "@/components/ui/calendar_v9"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Input } from "@/components/ui/input"; export const EARLIEST_DATE = new Date(1900, 0, 1); export const LATEST_DATE = new Date(2199, 11, 31); export const PREVENT_DEFAULT: React.EventHandler = (e) => e.preventDefault(); export const PickerInput: React.FC<{ label: string; children: React.ReactNode; }> = ({ label, children }) => ( <>
{children}
{" "} ); export const QuickOption: React.FC<{ label: string; isSelected: boolean; onSelect: () => void; }> = ({ label, isSelected, onSelect }) => { return ( ); }; export const DoneButton: React.FC<{ onClick: () => void; }> = ({ onClick }) => { return ( ); }; export type PickDateProps = { date?: Date; quickOptions?: Array<{ label: string; date: Date }>; numberOfMonths?: 1 | 2; required?: boolean; showInput?: boolean; onSelect: (date: Date | undefined) => void; }; export const PickDate: React.FC = ({ date, quickOptions, numberOfMonths, required = false, showInput = false, onSelect, }) => { const [month, setMonth] = useState(date); function initializeDate(e) { if (!date) { const today = dayjs().startOf("day").toDate(); onSelect(today); } } return ( <> {quickOptions?.length ? (
Quick Options
{quickOptions.map((option, idx) => ( { onSelect(option.date); setMonth(option.date); }} /> ))}
) : null}
{ onSelect(date); setMonth(date); }} numberOfMonths={numberOfMonths || 1} captionLayout="dropdown" startMonth={EARLIEST_DATE} endMonth={LATEST_DATE} hidden={{ before: EARLIEST_DATE, after: LATEST_DATE }} required={required} />
{showInput && (
{ const value = dayjs(e.target.value).toDate(); if (dayjs(value).isValid()) { setMonth(value); onSelect(value); } }} onMouseDown={initializeDate} onFocus={PREVENT_DEFAULT} onClick={PREVENT_DEFAULT} />
)} ); }; // Popover containing a PickDate const SelectDate: React.FC< Omit & { initialDate?: Date; align?: "center" | "start" | "end"; } > = ({ initialDate, align = "start", quickOptions, showInput, onSelect }) => { const [isOpen, setIsOpen] = useState(false); const [date, setDate] = useState(initialDate); const toggle = (value?: boolean) => { if (!isOpen) { setDate(initialDate); } setIsOpen(value === undefined ? !isOpen : value); }; return (
{ onSelect(date); toggle(false); }} />
); }; export default SelectDate;