import React, { useId, useState } from "react"; import { format, isValid, parse } from "date-fns"; import { DayPicker } from "react-day-picker"; /** Render an input field bound to a DayPicker calendar. */ export function Input() { const inputId = useId(); // Hold the month in state to control the calendar when the input changes const [month, setMonth] = useState(new Date()); // Hold the selected date in state const [selectedDate, setSelectedDate] = useState(undefined); // Hold the input value in state const [inputValue, setInputValue] = useState(""); /** * Function to handle the DayPicker select event: update the input value and * the selected date, and set the month. */ const handleDayPickerSelect = (date: Date | undefined) => { if (!date) { setInputValue(""); setSelectedDate(undefined); } else { setSelectedDate(date); setMonth(date); setInputValue(format(date, "MM/dd/yyyy")); } }; /** * Handle the input change event: parse the input value to a date, update the * selected date and set the month. */ const handleInputChange = (e: React.ChangeEvent) => { setInputValue(e.target.value); // keep the input value in sync const parsedDate = parse(e.target.value, "MM/dd/yyyy", new Date()); if (isValid(parsedDate)) { setSelectedDate(parsedDate); setMonth(parsedDate); } else { setSelectedDate(undefined); } }; return (
); }