1 | import type {
|
2 | DateLib,
|
3 | DayPickerProps,
|
4 | MoveFocusBy,
|
5 | MoveFocusDir
|
6 | } from "../types/index.js";
|
7 |
|
8 |
|
9 | export function getFocusableDate(
|
10 | moveBy: MoveFocusBy,
|
11 | moveDir: MoveFocusDir,
|
12 | refDate: Date,
|
13 | navStart: Date | undefined,
|
14 | navEnd: Date | undefined,
|
15 | props: Pick<DayPickerProps, "locale" | "ISOWeek" | "weekStartsOn">,
|
16 | dateLib: DateLib
|
17 | ): Date {
|
18 | const { weekStartsOn, locale, ISOWeek } = props;
|
19 | const {
|
20 | addDays,
|
21 | addMonths,
|
22 | addYears,
|
23 | addWeeks,
|
24 | startOfISOWeek,
|
25 | endOfISOWeek,
|
26 | startOfWeek,
|
27 | endOfWeek,
|
28 | max,
|
29 | min
|
30 | } = dateLib;
|
31 | const moveFns = {
|
32 | day: addDays,
|
33 | week: addWeeks,
|
34 | month: addMonths,
|
35 | year: addYears,
|
36 | startOfWeek: (date: Date) =>
|
37 | ISOWeek
|
38 | ? startOfISOWeek(date)
|
39 | : startOfWeek(date, { locale, weekStartsOn }),
|
40 | endOfWeek: (date: Date) =>
|
41 | ISOWeek ? endOfISOWeek(date) : endOfWeek(date, { locale, weekStartsOn })
|
42 | };
|
43 |
|
44 | let focusableDate = moveFns[moveBy](refDate, moveDir === "after" ? 1 : -1);
|
45 | if (moveDir === "before" && navStart) {
|
46 | focusableDate = max([navStart, focusableDate]);
|
47 | } else if (moveDir === "after" && navEnd) {
|
48 | focusableDate = min([navEnd, focusableDate]);
|
49 | }
|
50 | return focusableDate;
|
51 | }
|