UNPKG

1.27 kBPlain TextView Raw
1import { DropdownOption } from "../components/Dropdown.js";
2import type { Locale } from "../lib/dateLib.js";
3import type { DateLib, Formatters } from "../types/index.js";
4
5/** Return the months to show in the dropdown. */
6export function getMonthOptions(
7 displayMonth: Date,
8 navStart: Date | undefined,
9 navEnd: Date | undefined,
10 formatters: Pick<Formatters, "formatMonthDropdown">,
11 locale: Locale | undefined,
12 dateLib: DateLib
13): DropdownOption[] | undefined {
14 if (!navStart) return undefined;
15 if (!navEnd) return undefined;
16
17 const { addMonths, startOfMonth, isBefore, Date } = dateLib;
18 const year = displayMonth.getFullYear();
19
20 const months: number[] = [];
21 let month = navStart;
22 while (months.length < 12 && isBefore(month, addMonths(navEnd, 1))) {
23 months.push(month.getMonth());
24 month = addMonths(month, 1);
25 }
26 const sortedMonths = months.sort((a, b) => {
27 return a - b;
28 });
29 const options = sortedMonths.map((value) => {
30 const label = formatters.formatMonthDropdown(value, locale);
31 const disabled =
32 (navStart && new Date(year, value) < startOfMonth(navStart)) ||
33 (navEnd && new Date(year, value) > startOfMonth(navEnd)) ||
34 false;
35 return { value, label, disabled };
36 });
37
38 return options;
39}