1 | import React, { useState } from "react";
|
2 |
|
3 | import { addDays, format, startOfMonth } from "date-fns";
|
4 | import { DateRange, DayPicker } from "react-day-picker";
|
5 |
|
6 | export function RangeRequired() {
|
7 | const [range, setRange] = useState<DateRange>({
|
8 | from: startOfMonth(new Date()),
|
9 | to: addDays(startOfMonth(new Date()), 4)
|
10 | });
|
11 |
|
12 | let footer = `Please pick the first day.`;
|
13 | if (range?.from) {
|
14 | if (!range.to) {
|
15 | footer = `${format(range.from, "PPP")}—`;
|
16 | } else if (range.to) {
|
17 | footer = `${format(range.from, "PPP")}—${format(range.to, "PPP")}`;
|
18 | }
|
19 | }
|
20 |
|
21 | return (
|
22 | <DayPicker
|
23 | mode="range"
|
24 | required
|
25 | selected={range}
|
26 | onSelect={setRange}
|
27 | footer={footer}
|
28 | />
|
29 | );
|
30 | }
|