-
-
Notifications
You must be signed in to change notification settings - Fork 734
/
useRange.tsx
76 lines (66 loc) · 1.82 KB
/
useRange.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React from "react";
import type {
DateLib,
DateRange,
DayPickerProps,
Modifiers,
PropsRange,
Selection
} from "../types/index.js";
import { addToRange, dateMatchModifiers } from "../utils/index.js";
import { rangeIncludesDate } from "../utils/rangeIncludesDate.js";
export function useRange<T extends DayPickerProps>(
props: T,
dateLib: DateLib
): Selection<T> {
const {
disabled,
excludeDisabled,
selected: initiallySelected,
required,
onSelect
} = props as PropsRange;
const [selected, setSelected] = React.useState<DateRange | undefined>(
initiallySelected
);
// Update the selected date if the `selected` prop changes.
React.useEffect(() => {
setSelected(initiallySelected);
}, [initiallySelected]);
const isSelected = (date: Date) =>
selected && rangeIncludesDate(selected, date, false, dateLib);
const select = (
triggerDate: Date,
modifiers: Modifiers,
e: React.MouseEvent | React.KeyboardEvent
) => {
const { min, max } = props as PropsRange;
const newRange = triggerDate
? addToRange(triggerDate, selected, min, max, required, dateLib)
: undefined;
if (newRange?.from && newRange.to) {
let newDate = newRange.from;
while (dateLib.differenceInCalendarDays(newRange.to, newDate) > 0) {
newDate = dateLib.addDays(newDate, 1);
if (
excludeDisabled &&
disabled &&
dateMatchModifiers(newDate, disabled, dateLib)
) {
// if a disabled days is found, the range is reset
newRange.from = triggerDate;
newRange.to = undefined;
break;
}
}
}
setSelected(newRange);
onSelect?.(newRange, triggerDate, modifiers, e);
return newRange;
};
return {
selected,
select,
isSelected
} as Selection<T>;
}