UNPKG

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