import React, { useEffect, useId, useRef, useState } from "react"; import { format, isValid, parse } from "date-fns"; import { DayPicker } from "react-day-picker"; export function Dialog() { const dialogRef = useRef(null); const dialogId = useId(); const headerId = 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(""); // Hold the dialog visibility in state const [isDialogOpen, setIsDialogOpen] = useState(false); // Function to toggle the dialog visibility const toggleDialog = () => setIsDialogOpen(!isDialogOpen); // Hook to handle the body scroll behavior and focus trapping. You may want to // use your own trapping library as the body.style overflow will break the // scroll position. useEffect(() => { if (!dialogRef.current) return; if (isDialogOpen) { dialogRef.current.showModal(); } else { dialogRef.current.close(); } }, [isDialogOpen]); /** * 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); setInputValue(format(date, "MM/dd/yyyy")); } dialogRef.current?.close(); }; /** * 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 (
{" "}

{selectedDate !== undefined ? selectedDate.toDateString() : "Please type or pick a date"}

setIsDialogOpen(false)} > {isDialogOpen && ( Selected: {selectedDate.toDateString()} ) } /> )}
); }