1 | import { DateLib, DayPickerProps } from "../index.js";
|
2 |
|
3 |
|
4 | const NrOfDaysWithFixedWeeks = 42;
|
5 |
|
6 |
|
7 | export function getDates(
|
8 | displayMonths: Date[],
|
9 | maxDate: Date | undefined,
|
10 | props: Pick<
|
11 | DayPickerProps,
|
12 | "ISOWeek" | "fixedWeeks" | "locale" | "weekStartsOn"
|
13 | >,
|
14 | dateLib: DateLib
|
15 | ): Date[] {
|
16 | const firstMonth = displayMonths[0];
|
17 | const lastMonth = displayMonths[displayMonths.length - 1];
|
18 |
|
19 | const { ISOWeek, fixedWeeks, locale, weekStartsOn } = props ?? {};
|
20 | const {
|
21 | startOfWeek,
|
22 | endOfWeek,
|
23 | startOfISOWeek,
|
24 | endOfISOWeek,
|
25 | addDays,
|
26 | differenceInCalendarDays,
|
27 | differenceInCalendarMonths,
|
28 | isAfter,
|
29 | endOfMonth,
|
30 | Date
|
31 | } = dateLib;
|
32 |
|
33 | const startWeekFirstDate = ISOWeek
|
34 | ? startOfISOWeek(firstMonth)
|
35 | : startOfWeek(firstMonth, {
|
36 | weekStartsOn,
|
37 | locale
|
38 | });
|
39 |
|
40 | const endWeekLastDate = ISOWeek
|
41 | ? endOfISOWeek(endOfMonth(lastMonth))
|
42 | : endOfWeek(endOfMonth(lastMonth), {
|
43 | weekStartsOn,
|
44 | locale
|
45 | });
|
46 |
|
47 | const nOfDays = differenceInCalendarDays(endWeekLastDate, startWeekFirstDate);
|
48 | const nOfMonths = differenceInCalendarMonths(lastMonth, firstMonth) + 1;
|
49 |
|
50 | const dates: Date[] = [];
|
51 | for (let i = 0; i <= nOfDays; i++) {
|
52 | const date = addDays(startWeekFirstDate, i);
|
53 | if (maxDate && isAfter(date, maxDate)) {
|
54 | break;
|
55 | }
|
56 | dates.push(new Date(date));
|
57 | }
|
58 |
|
59 |
|
60 | const extraDates = NrOfDaysWithFixedWeeks * nOfMonths;
|
61 | if (fixedWeeks && dates.length < extraDates) {
|
62 | for (let i = 0; i < 7; i++) {
|
63 | const date = addDays(dates[dates.length - 1], 1);
|
64 | dates.push(new Date(date));
|
65 | }
|
66 | }
|
67 | return dates;
|
68 | }
|