/
SelectRangeContext.tsx
196 lines (180 loc) · 5.27 KB
/
SelectRangeContext.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { createContext, ReactNode, useContext } from 'react';
import {
addDays,
differenceInCalendarDays,
isSameDay,
subDays
} from 'date-fns';
import { DayPickerBase } from 'types/DayPickerBase';
import { DayPickerRangeProps, isDayPickerRange } from 'types/DayPickerRange';
import { DayClickEventHandler } from 'types/EventHandlers';
import { DateRange } from 'types/Matchers';
import { InternalModifier, Modifiers } from 'types/Modifiers';
import { addToRange } from './utils/addToRange';
/** Represent the modifiers that are changed by the range selection. */
export type SelectRangeModifiers = Pick<
Modifiers,
| InternalModifier.Disabled
| InternalModifier.RangeEnd
| InternalModifier.RangeMiddle
| InternalModifier.RangeStart
>;
/** Represents the value of a {@link SelectRangeContext}. */
export interface SelectRangeContextValue {
/** The range of days that has been selected. */
selected: DateRange | undefined;
/** The modifiers for the corresponding selection. */
modifiers: SelectRangeModifiers;
/** Event handler to attach to the day button to enable the range select. */
onDayClick?: DayClickEventHandler;
}
/**
* The SelectRange context shares details about the selected days when in
* range selection mode.
*
* Access this context from the {@link useSelectRange} hook.
*/
export const SelectRangeContext = createContext<
SelectRangeContextValue | undefined
>(undefined);
export interface SelectRangeProviderProps {
initialProps: DayPickerBase;
children?: ReactNode;
}
/** Provides the values for the {@link SelectRangeProvider}. */
export function SelectRangeProvider(
props: SelectRangeProviderProps
): JSX.Element {
if (!isDayPickerRange(props.initialProps)) {
const emptyContextValue: SelectRangeContextValue = {
selected: undefined,
modifiers: {
range_start: [],
range_end: [],
range_middle: [],
disabled: []
}
};
return (
<SelectRangeContext.Provider value={emptyContextValue}>
{props.children}
</SelectRangeContext.Provider>
);
}
return (
<SelectRangeProviderInternal
initialProps={props.initialProps}
children={props.children}
/>
);
}
/** @private */
export interface SelectRangeProviderInternalProps {
initialProps: DayPickerRangeProps;
children?: ReactNode;
}
export function SelectRangeProviderInternal({
initialProps,
children
}: SelectRangeProviderInternalProps): JSX.Element {
const { selected } = initialProps;
const { from: selectedFrom, to: selectedTo } = selected || {};
const min = initialProps.min;
const max = initialProps.max;
const onDayClick: DayClickEventHandler = (day, activeModifiers, e) => {
initialProps.onDayClick?.(day, activeModifiers, e);
const newRange = addToRange(day, selected);
initialProps.onSelect?.(newRange, day, activeModifiers, e);
};
const modifiers: SelectRangeModifiers = {
range_start: [],
range_end: [],
range_middle: [],
disabled: []
};
if (selectedFrom) {
modifiers.range_start = [selectedFrom];
if (!selectedTo) {
modifiers.range_end = [selectedFrom];
} else {
modifiers.range_end = [selectedTo];
if (!isSameDay(selectedFrom, selectedTo)) {
modifiers.range_middle = [
{
after: selectedFrom,
before: selectedTo
}
];
}
}
} else if (selectedTo) {
modifiers.range_start = [selectedTo];
modifiers.range_end = [selectedTo];
}
if (min) {
if (selectedFrom && !selectedTo) {
modifiers.disabled.push({
after: subDays(selectedFrom, min - 1),
before: addDays(selectedFrom, min - 1)
});
}
if (selectedFrom && selectedTo) {
modifiers.disabled.push({
after: selectedFrom,
before: addDays(selectedFrom, min - 1)
});
}
if (!selectedFrom && selectedTo) {
modifiers.disabled.push({
after: subDays(selectedTo, min - 1),
before: addDays(selectedTo, min - 1)
});
}
}
if (max) {
if (selectedFrom && !selectedTo) {
modifiers.disabled.push({
before: addDays(selectedFrom, -max + 1)
});
modifiers.disabled.push({
after: addDays(selectedFrom, max - 1)
});
}
if (selectedFrom && selectedTo) {
const selectedCount =
differenceInCalendarDays(selectedTo, selectedFrom) + 1;
const offset = max - selectedCount;
modifiers.disabled.push({
before: subDays(selectedFrom, offset)
});
modifiers.disabled.push({
after: addDays(selectedTo, offset)
});
}
if (!selectedFrom && selectedTo) {
modifiers.disabled.push({
before: addDays(selectedTo, -max + 1)
});
modifiers.disabled.push({
after: addDays(selectedTo, max - 1)
});
}
}
return (
<SelectRangeContext.Provider value={{ selected, onDayClick, modifiers }}>
{children}
</SelectRangeContext.Provider>
);
}
/**
* Hook to access the {@link SelectRangeContextValue}.
*
* This hook is meant to be used inside internal or custom components.
*/
export function useSelectRange(): SelectRangeContextValue {
const context = useContext(SelectRangeContext);
if (!context) {
throw new Error('useSelectRange must be used within a SelectRangeProvider');
}
return context;
}