UNPKG

1.6 kBTypeScriptView Raw
1import { CalendarDay } from "../classes/index.js";
2import type {
3 DateLib,
4 DayPickerProps,
5 MoveFocusBy,
6 MoveFocusDir
7} from "../types/index.js";
8import { dateMatchModifiers } from "../utils/dateMatchModifiers.js";
9
10import { getFocusableDate } from "./getFocusableDate.js";
11
12export function getNextFocus(
13 moveBy: MoveFocusBy,
14 moveDir: MoveFocusDir,
15 /** The date that is currently focused. */
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 // Limit the recursion to 365 attempts
28 return undefined;
29 }
30
31 const focusableDate = getFocusableDate(
32 moveBy,
33 moveDir,
34 refDay.date, // should be refDay? or 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 // Recursively attempt to find the next focusable date
57 return getNextFocus(
58 moveBy,
59 moveDir,
60 focusDay,
61 calendarStartMonth,
62 calendarEndMonth,
63 props,
64 dateLib,
65 attempt + 1
66 );
67}