-
Notifications
You must be signed in to change notification settings - Fork 20
/
use-lilius.ts
307 lines (260 loc) Β· 6.85 KB
/
use-lilius.ts
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import {
addMonths,
addYears,
eachDayOfInterval,
eachMonthOfInterval,
eachWeekOfInterval,
endOfMonth,
endOfWeek,
isAfter,
isBefore,
isEqual,
set,
setMonth,
setYear,
startOfMonth,
startOfToday,
startOfWeek,
subMonths,
subYears,
} from "date-fns";
import { useCallback, useMemo, useState } from "react";
export enum Month {
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER,
}
export enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
}
export interface Options {
/**
* What day a week starts on within the calendar matrix.
*
* @default Day.SUNDAY
*/
weekStartsOn?: Day;
/**
* The initial viewing date.
*
* @default new Date()
*/
viewing?: Date;
/**
* The initial date(s) selection.
*
* @default []
*/
selected?: Date[];
/**
* The number of months in the calendar.
*
* @default 1
*/
numberOfMonths?: number;
}
export interface Returns {
/**
* Returns a copy of the given date with the time set to 00:00:00:00.
*/
clearTime: (date: Date) => Date;
/**
* Returns whether or not a date is between 2 other dates (inclusive).
*/
inRange: (date: Date, min: Date, max: Date) => boolean;
/**
* The date represented in the calendar matrix. Note that
* the month and year are the only parts used.
*/
viewing: Date;
/**
* Set the date represented in the calendar matrix. Note that
* the month and year are the only parts used.
*/
setViewing: React.Dispatch<React.SetStateAction<Date>>;
/**
* Set the viewing date to today.
*/
viewToday: () => void;
/**
* Set the viewing date to the given month.
*/
viewMonth: (month: Month) => void;
/**
* Set the viewing date to the month before the current.
*/
viewPreviousMonth: () => void;
/**
* Set the viewing date to the month after the current.
*/
viewNextMonth: () => void;
/**
* Set the viewing date to the given year.
*/
viewYear: (year: number) => void;
/**
* Set the viewing date to the year before the current.
*/
viewPreviousYear: () => void;
/**
* Set the viewing date to the year after the current.
*/
viewNextYear: () => void;
/**
* The dates currently selected.
*/
selected: Date[];
/**
* Override the currently selected dates.
*/
setSelected: React.Dispatch<React.SetStateAction<Date[]>>;
/**
* Reset the selected dates to [].
*/
clearSelected: () => void;
/**
* Determine whether or not a date has been selected.
*/
isSelected: (date: Date) => boolean;
/**
* Select one or more dates.
*/
select: (date: Date | Date[], replaceExisting?: boolean) => void;
/**
* Deselect one or more dates.
*/
deselect: (date: Date | Date[]) => void;
/**
* Toggle the selection of a date.
*/
toggle: (date: Date, replaceExisting?: boolean) => void;
/**
* Select a range of dates (inclusive).
*/
selectRange: (start: Date, end: Date, replaceExisting?: boolean) => void;
/**
* Deselect a range of dates (inclusive).
*/
deselectRange: (start: Date, end: Date) => void;
/**
* A matrix of days based on the current viewing date.
*/
calendar: Date[][][];
}
const inRange = (date: Date, min: Date, max: Date) =>
(isEqual(date, min) || isAfter(date, min)) && (isEqual(date, max) || isBefore(date, max));
const clearTime = (date: Date) => set(date, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 });
export const useLilius = ({
weekStartsOn = Day.SUNDAY,
viewing: initialViewing = new Date(),
selected: initialSelected = [],
numberOfMonths = 1,
}: Options = {}): Returns => {
const [viewing, setViewing] = useState<Date>(initialViewing);
const viewToday = useCallback(() => setViewing(startOfToday()), [setViewing]);
const viewMonth = useCallback((month: Month) => setViewing((v) => setMonth(v, month)), []);
const viewPreviousMonth = useCallback(() => setViewing((v) => subMonths(v, 1)), []);
const viewNextMonth = useCallback(() => setViewing((v) => addMonths(v, 1)), []);
const viewYear = useCallback((year: number) => setViewing((v) => setYear(v, year)), []);
const viewPreviousYear = useCallback(() => setViewing((v) => subYears(v, 1)), []);
const viewNextYear = useCallback(() => setViewing((v) => addYears(v, 1)), []);
const [selected, setSelected] = useState<Date[]>(initialSelected.map(clearTime));
const clearSelected = () => setSelected([]);
const isSelected = useCallback((date: Date) => selected.findIndex((s) => isEqual(s, date)) > -1, [selected]);
const select = useCallback((date: Date | Date[], replaceExisting?: boolean) => {
if (replaceExisting) {
setSelected(Array.isArray(date) ? date : [date]);
} else {
setSelected((selectedItems) => selectedItems.concat(Array.isArray(date) ? date : [date]));
}
}, []);
const deselect = useCallback(
(date: Date | Date[]) =>
setSelected((selectedItems) =>
Array.isArray(date)
? selectedItems.filter((s) => !date.map((d) => d.getTime()).includes(s.getTime()))
: selectedItems.filter((s) => !isEqual(s, date)),
),
[],
);
const toggle = useCallback(
(date: Date, replaceExisting?: boolean) => (isSelected(date) ? deselect(date) : select(date, replaceExisting)),
[deselect, isSelected, select],
);
const selectRange = useCallback((start: Date, end: Date, replaceExisting?: boolean) => {
if (replaceExisting) {
setSelected(eachDayOfInterval({ start, end }));
} else {
setSelected((selectedItems) => selectedItems.concat(eachDayOfInterval({ start, end })));
}
}, []);
const deselectRange = useCallback((start: Date, end: Date) => {
setSelected((selectedItems) =>
selectedItems.filter(
(s) =>
!eachDayOfInterval({ start, end })
.map((d) => d.getTime())
.includes(s.getTime()),
),
);
}, []);
const calendar = useMemo<Date[][][]>(
() =>
eachMonthOfInterval({
start: startOfMonth(viewing),
end: endOfMonth(addMonths(viewing, numberOfMonths - 1)),
}).map((month) =>
eachWeekOfInterval(
{
start: startOfMonth(month),
end: endOfMonth(month),
},
{ weekStartsOn },
).map((week) =>
eachDayOfInterval({
start: startOfWeek(week, { weekStartsOn }),
end: endOfWeek(week, { weekStartsOn }),
}),
),
),
[viewing, weekStartsOn, numberOfMonths],
);
return {
clearTime,
inRange,
viewing,
setViewing,
viewToday,
viewMonth,
viewPreviousMonth,
viewNextMonth,
viewYear,
viewPreviousYear,
viewNextYear,
selected,
setSelected,
clearSelected,
isSelected,
select,
deselect,
toggle,
selectRange,
deselectRange,
calendar,
};
};