import { useState } from "react"; import { format } from "date-fns"; import { Calendar } from "@/components/ui/calendar"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { CalendarIcon } from "lucide-react"; import { cn } from "@/lib/utils"; interface DateInputProps { label?: string; value: Date | null; onChange: (date: Date | null) => void; disableFuture?: boolean; disablePast?: boolean; } export function DateInput({ label, value, onChange, disableFuture = false, disablePast = false, }: DateInputProps) { const [inputValue, setInputValue] = useState( value ? format(value, "MM/dd/yyyy") : "" ); const handleInputChange = (e: React.ChangeEvent) => { let val = e.target.value.replace(/\D/g, ""); if (val.length >= 5) { val = `${val.slice(0, 2)}/${val.slice(2, 4)}/${val.slice(4, 8)}`; } else if (val.length >= 3) { val = `${val.slice(0, 2)}/${val.slice(2, 4)}`; } setInputValue(val); if (val.length === 10) { const [mm, dd, yyyy] = val.split("/") ?? []; if (mm && dd && yyyy) { const parsed = new Date(+yyyy, +mm - 1, +dd); if (!isNaN(parsed.getTime())) { onChange(parsed); } } } }; return (
{label && }
{ if (date) { setInputValue(format(date, "MM/dd/yyyy")); onChange(date); } }} disabled={ disableFuture ? { after: new Date() } : disablePast ? { before: new Date() } : undefined } />
); }