import * as React from "react"; import { t as i18nT } from "i18next"; import { parse } from "date-fns"; import { CalendarIcon, ChevronDownIcon, XIcon } from "~/components/ui/icons/vendor"; import { Button } from "../button"; import { Popover, PopoverContent, PopoverTrigger } from "../popover"; import { cn } from "~/lib/utils"; import { Calendar } from "../calendar"; import { Input } from "../input"; import { Tabs, TabsContent } from "../tabs"; import { useLocaleFormatters } from "~/locale/format"; type Props = { name?: string; date: Date | undefined | null; setDate: (date: Date | undefined) => void; dirty?: boolean; canClear?: boolean; className?: string; closeOnSelect?: boolean; showInput?: boolean; placeholder?: string; required?: boolean; disabled?: boolean; autoFocus?: boolean; style?: React.CSSProperties; fromYear?: number; toYear?: number; }; export function InputDatePickerDeprecated({ name, date, setDate, dirty, canClear = true, className, closeOnSelect = false, showInput = false, placeholder, required, disabled, autoFocus, style, fromYear, toYear, }: Props) { const formatters = useLocaleFormatters(); const [isCalendarOpen, setIsCalendarOpen] = React.useState(false); const [inputValue, setInputValue] = React.useState(""); const [activeTab, setActiveTab] = React.useState("calendar"); const [month, setMonth] = React.useState(date || undefined); React.useEffect(() => { if (date) { try { const parsedDate = new Date(date); if (!isNaN(parsedDate.getTime())) { setInputValue(parsedDate.toISOString().split("T")[0]); } } catch (e) { console.error("Invalid date:", date); } } else { setInputValue(""); } }, [date]); const handleInputChange = (e: React.ChangeEvent) => { const value = e.target.value; setInputValue(value); if (value) { // Parse the date string in local timezone const [year, month, day] = value.split("-").map(Number); const newDate = new Date(year, month - 1, day); if (!isNaN(newDate.getTime())) { setDate(newDate); } } else { setDate(undefined); } }; const displayDate = React.useMemo(() => { if (!date) return null; try { const parsedDate = new Date(date); if (!isNaN(parsedDate.getTime())) { return formatters.date(parsedDate, { dateStyle: "long" }); } } catch (e) { console.error("Invalid date:", date); } return null; }, [date, formatters]); return ( {name && }
{canClear && date && (
)}
{ if (e.key === "Enter") { setIsCalendarOpen(false); } }} />
{ setMonth(e); }} onSelect={(e: Date | undefined) => { setDate(e); if (closeOnSelect) { setIsCalendarOpen(false); } }} required={required} disabled={disabled} fromYear={fromYear} toYear={toYear} />
{ if (e.key === "Enter") { setIsCalendarOpen(false); } }} />
); } export default function InputDatePicker(props: Props) { const formatters = useLocaleFormatters(); const [open, setOpen] = React.useState(false); const [date, setDate] = React.useState(props.date ? new Date(props.date) : undefined); return (
{ setDate(date); setOpen(false); props.setDate(date); }} fromYear={props.fromYear} toYear={props.toYear} /> {/*
*/}
{/* Today button */} {/* Clear button */}
); } // function ManualInputDatePicker({ // date, // onChange, // inputFormat = "mm/dd/yyyy", // }: { // date: Date | undefined; // onChange: (date: Date | undefined) => void; // inputFormat?: string; // }) { // const [inputValue, setInputValue] = React.useState(date ? format(date, inputFormat) : ""); // function tryParseDate(value: string) { // setInputValue(value); // try { // const parsedDate = parse(value, inputFormat, new Date()); // if (!isNaN(parsedDate.getTime())) { // onChange(parsedDate); // } // } catch (e) { // console.error("Invalid date:", value); // } // } // return tryParseDate(e.target.value)} placeholder={inputFormat} />; // }