1 | import React, { useId, useState } from "react";
|
2 |
|
3 | import { format, isValid, parse } from "date-fns";
|
4 | import { DayPicker } from "react-day-picker";
|
5 |
|
6 |
|
7 | export function Input() {
|
8 | const inputId = useId();
|
9 |
|
10 |
|
11 | const [month, setMonth] = useState(new Date());
|
12 |
|
13 |
|
14 | const [selectedDate, setSelectedDate] = useState<Date | undefined>(undefined);
|
15 |
|
16 |
|
17 | const [inputValue, setInputValue] = useState("");
|
18 |
|
19 | |
20 |
|
21 |
|
22 |
|
23 | const handleDayPickerSelect = (date: Date | undefined) => {
|
24 | if (!date) {
|
25 | setInputValue("");
|
26 | setSelectedDate(undefined);
|
27 | } else {
|
28 | setSelectedDate(date);
|
29 | setMonth(date);
|
30 | setInputValue(format(date, "MM/dd/yyyy"));
|
31 | }
|
32 | };
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 | const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
39 | setInputValue(e.target.value);
|
40 |
|
41 | const parsedDate = parse(e.target.value, "MM/dd/yyyy", new Date());
|
42 |
|
43 | if (isValid(parsedDate)) {
|
44 | setSelectedDate(parsedDate);
|
45 | setMonth(parsedDate);
|
46 | } else {
|
47 | setSelectedDate(undefined);
|
48 | }
|
49 | };
|
50 |
|
51 | return (
|
52 | <div>
|
53 | <label htmlFor={inputId}>
|
54 | <strong>Date:</strong>
|
55 | </label>
|
56 | <input
|
57 | style={{ fontSize: "inherit", padding: "0.25em 0.5em" }}
|
58 | id={inputId}
|
59 | type="text"
|
60 | value={inputValue}
|
61 | placeholder="MM/dd/yyyy"
|
62 | onChange={handleInputChange}
|
63 | />
|
64 | <div style={{ marginBlock: "1em" }}>
|
65 | <DayPicker
|
66 | month={month}
|
67 | onMonthChange={setMonth}
|
68 | mode="single"
|
69 | selected={selectedDate}
|
70 | onSelect={handleDayPickerSelect}
|
71 | footer={`Selected: ${selectedDate?.toDateString()}`}
|
72 | />
|
73 | </div>
|
74 | </div>
|
75 | );
|
76 | }
|