1 | import { CalendarDay } from "../classes/index.js";
|
2 | import type {
|
3 | DateLib,
|
4 | DayPickerProps,
|
5 | MoveFocusBy,
|
6 | MoveFocusDir
|
7 | } from "../types/index.js";
|
8 | import { dateMatchModifiers } from "../utils/dateMatchModifiers.js";
|
9 |
|
10 | import { getFocusableDate } from "./getFocusableDate.js";
|
11 |
|
12 | export function getNextFocus(
|
13 | moveBy: MoveFocusBy,
|
14 | moveDir: MoveFocusDir,
|
15 |
|
16 | refDay: CalendarDay,
|
17 | calendarStartMonth: Date | undefined,
|
18 | calendarEndMonth: Date | undefined,
|
19 | props: Pick<
|
20 | DayPickerProps,
|
21 | "disabled" | "hidden" | "modifiers" | "locale" | "ISOWeek" | "weekStartsOn"
|
22 | >,
|
23 | dateLib: DateLib,
|
24 | attempt: number = 0
|
25 | ): CalendarDay | undefined {
|
26 | if (attempt > 365) {
|
27 |
|
28 | return undefined;
|
29 | }
|
30 |
|
31 | const focusableDate = getFocusableDate(
|
32 | moveBy,
|
33 | moveDir,
|
34 | refDay.date,
|
35 | calendarStartMonth,
|
36 | calendarEndMonth,
|
37 | props,
|
38 | dateLib
|
39 | );
|
40 |
|
41 | const isDisabled = Boolean(
|
42 | props.disabled && dateMatchModifiers(focusableDate, props.disabled, dateLib)
|
43 | );
|
44 |
|
45 | const isHidden = Boolean(
|
46 | props.hidden && dateMatchModifiers(focusableDate, props.hidden, dateLib)
|
47 | );
|
48 |
|
49 | const targetMonth = focusableDate;
|
50 | const focusDay = new CalendarDay(focusableDate, targetMonth, dateLib);
|
51 |
|
52 | if (!isDisabled && !isHidden) {
|
53 | return focusDay;
|
54 | }
|
55 |
|
56 |
|
57 | return getNextFocus(
|
58 | moveBy,
|
59 | moveDir,
|
60 | focusDay,
|
61 | calendarStartMonth,
|
62 | calendarEndMonth,
|
63 | props,
|
64 | dateLib,
|
65 | attempt + 1
|
66 | );
|
67 | }
|