import * as React from "react"; import { format, parse } from "date-fns"; import { Calendar as CalendarIcon } from "lucide-react"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Calendar } from "@/components/ui/calendar"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; interface DateInputProps { value: string | null; onChange: (value: string | null) => void; className?: string; disabled?: boolean; placeholder?: string; } export function DateInput({ value, onChange, className, disabled = false, placeholder = "Pick a date", }: DateInputProps) { // Convert string date to Date object for the calendar const getDateValue = () => { if (!value) return undefined; try { return parse(value, "yyyy-MM-dd", new Date()); } catch (e) { console.error("Invalid date format:", value); return undefined; } }; // Update the date value as string in yyyy-MM-dd format const handleDateChange = (date: Date | undefined) => { if (!date) { onChange(null); return; } try { const formattedDate = format(date, "yyyy-MM-dd"); onChange(formattedDate); } catch (e) { console.error("Error formatting date:", e); } }; // Format date for display const getDisplayDate = () => { if (!value) return null; try { const date = parse(value, "yyyy-MM-dd", new Date()); return format(date, "PPP"); // Human readable format } catch (e) { console.error("Error parsing date for display:", e); return value; } }; return (
handleDateChange(date)} initialFocus disabled={(date) => date > new Date('2100-01-01') || date < new Date('1900-01-01')} />
); }