UNPKG

1.25 kBPlain TextView Raw
1import type { DateLib, DayPickerProps } from "../types/index.js";
2
3/**
4 * Return the next month the user can navigate to according to the given
5 * options.
6 *
7 * Please note that the next month is not always the next calendar month:
8 *
9 * - If after the `calendarEndMonth` range, is `undefined`;
10 * - If the navigation is paged , is the number of months displayed ahead.
11 */
12export function getNextMonth(
13 firstDisplayedMonth: Date,
14 calendarEndMonth: Date | undefined,
15 options: Pick<
16 DayPickerProps,
17 "numberOfMonths" | "pagedNavigation" | "disableNavigation"
18 >,
19 dateLib: DateLib
20): Date | undefined {
21 if (options.disableNavigation) {
22 return undefined;
23 }
24 const { pagedNavigation, numberOfMonths = 1 } = options;
25 const { startOfMonth, addMonths, differenceInCalendarMonths } = dateLib;
26 const offset = pagedNavigation ? numberOfMonths : 1;
27 const month = startOfMonth(firstDisplayedMonth);
28
29 if (!calendarEndMonth) {
30 return addMonths(month, offset);
31 }
32
33 const monthsDiff = differenceInCalendarMonths(
34 calendarEndMonth,
35 firstDisplayedMonth
36 );
37
38 if (monthsDiff < numberOfMonths) {
39 return undefined;
40 }
41
42 // Jump forward as the number of months when paged navigation
43 return addMonths(month, offset);
44}