1.63 kBPlain TextView Raw
1import type { DateLib, DayPickerProps } from "../types/index.js";
2
3/** Return the start and end months for the calendar navigation. */
4export function getNavMonths(
5 props: Pick<
6 DayPickerProps,
7 | "captionLayout"
8 | "endMonth"
9 | "startMonth"
10 | "today"
11 // Deprecated:
12 | "fromMonth"
13 | "fromYear"
14 | "toMonth"
15 | "toYear"
16 >,
17 dateLib: DateLib
18): [start: Date | undefined, end: Date | undefined] {
19 let { startMonth, endMonth } = props;
20
21 const {
22 startOfYear,
23 startOfDay,
24 startOfMonth,
25 endOfMonth,
26 addYears,
27 endOfYear,
28 Date
29 } = dateLib;
30
31 // Handle deprecated code
32 const { fromYear, toYear, fromMonth, toMonth } = props;
33 if (!startMonth && fromMonth) {
34 startMonth = fromMonth;
35 }
36 if (!startMonth && fromYear) {
37 startMonth = new Date(fromYear, 0, 1);
38 }
39 if (!endMonth && toMonth) {
40 endMonth = toMonth;
41 }
42 if (!endMonth && toYear) {
43 endMonth = new Date(toYear, 11, 31);
44 }
45
46 const hasDropdowns = props.captionLayout?.startsWith("dropdown");
47 if (startMonth) {
48 startMonth = startOfMonth(startMonth);
49 } else if (fromYear) {
50 startMonth = new Date(fromYear, 0, 1);
51 } else if (!startMonth && hasDropdowns) {
52 startMonth = startOfYear(addYears(props.today ?? new Date(), -100));
53 }
54 if (endMonth) {
55 endMonth = endOfMonth(endMonth);
56 } else if (toYear) {
57 endMonth = new Date(toYear, 11, 31);
58 } else if (!endMonth && hasDropdowns) {
59 endMonth = endOfYear(props.today ?? new Date());
60 }
61 return [
62 startMonth ? startOfDay(startMonth) : startMonth,
63 endMonth ? startOfDay(endMonth) : endMonth
64 ];
65}