UNPKG

963 BTypeScriptView Raw
1import React, { useState } from "react";
2
3import { isSameDay } from "date-fns";
4import { DayEventHandler, DayPicker } from "react-day-picker";
5
6export function CustomMultiple() {
7 const [value, setValue] = useState<Date[]>([]);
8
9 const handleDayClick: DayEventHandler<React.MouseEvent> = (
10 day,
11 modifiers
12 ) => {
13 const newValue = [...value];
14 if (modifiers.selected) {
15 const index = value.findIndex((d) => isSameDay(day, d));
16 newValue.splice(index, 1);
17 } else {
18 newValue.push(day);
19 }
20 setValue(newValue);
21 };
22
23 const handleResetClick = () => setValue([]);
24
25 let footer = <>Please pick one or more days.</>;
26
27 if (value.length > 0)
28 footer = (
29 <>
30 You selected {value.length} days.{" "}
31 <button onClick={handleResetClick}>Reset</button>
32 </>
33 );
34
35 return (
36 <DayPicker
37 onDayClick={handleDayClick}
38 modifiers={{ selected: value }}
39 footer={footer}
40 />
41 );
42}