Skip to content

Commit f1023dd

Browse files
authored
fix: initial month when numberOfMonths and endMonth are set (#2784)
* fix: single month even when numberOfMonths Fixes #2782 * fix: improve initial month calculation considering numberOfMonths for navEnd * Add MultipleWithEndMonth example component and test * fix: update test to verify display of three months in MultipleWithEndMonth component * Revert "fix: single month even when numberOfMonths" This reverts commit 0167b1e. Signed-off-by: gpbl <io@gpbl.dev> * fix: remove startMonth prop from MultipleWithEndMonth example * fix: update test to verify display of only three months in MultipleWithEndMonth component * Update logic Signed-off-by: gpbl <io@gpbl.dev> * Update example name to MultipleMonthsWithEndMonth Signed-off-by: gpbl <io@gpbl.dev> * Remove unnecessary comments Signed-off-by: gpbl <io@gpbl.dev> --------- Signed-off-by: gpbl <io@gpbl.dev>
1 parent 46ff759 commit f1023dd

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from "react";
2+
3+
import { grid } from "@/test/elements";
4+
import { render } from "@/test/render";
5+
6+
import { MultipleMonthsWithEndMonth } from "./MultipleMonthsWithEndMonth";
7+
8+
const today = new Date(2025, 6, 1);
9+
10+
beforeAll(() => jest.setSystemTime(today));
11+
afterAll(() => jest.useRealTimers());
12+
13+
beforeEach(() => {
14+
render(<MultipleMonthsWithEndMonth />);
15+
});
16+
17+
test("should display three months", () => {
18+
expect(grid("May 2025")).toBeInTheDocument();
19+
expect(grid("June 2025")).toBeInTheDocument();
20+
expect(grid("July 2025")).toBeInTheDocument();
21+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react";
2+
3+
import { DayPicker } from "react-day-picker";
4+
5+
export function MultipleMonthsWithEndMonth() {
6+
return (
7+
<DayPicker
8+
numberOfMonths={3}
9+
endMonth={new Date()}
10+
pagedNavigation={false}
11+
/>
12+
);
13+
}

examples/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export * from "./MultipleMinMax";
5252
export * from "./MultipleRequired";
5353
export * from "./MultipleMonths";
5454
export * from "./MultipleMonthsPaged";
55+
export * from "./MultipleMonthsWithEndMonth";
5556
export * from "./NavLayout";
5657
export * from "./NavLayoutAfter";
5758
export * from "./Numerals";

src/helpers/getInitialMonth.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe("when endMonth is given", () => {
7373
const month = new Date(2010, 11, 12);
7474
const endMonth = addMonths(month, -2);
7575
describe("when the number of month is 1", () => {
76-
it("return the endMonth", () => {
76+
it("returns the endMonth as the initial month so the last displayed month does not exceed endMonth", () => {
7777
const initialMonth = getInitialMonth(
7878
{ month },
7979
undefined,
@@ -84,14 +84,15 @@ describe("when endMonth is given", () => {
8484
});
8585
});
8686
describe("when the number of month is 3", () => {
87-
it("return the endMonth plus the number of months", () => {
87+
it("returns the initial month so that initialMonth + 2 months = endMonth (last displayed month is endMonth)", () => {
8888
const initialMonth = getInitialMonth(
8989
{ month, numberOfMonths: 3 },
9090
undefined,
9191
endMonth,
9292
defaultDateLib
9393
);
94-
const expectedMonth = addMonths(endMonth, -1 * (3 - 1));
94+
// The last displayed month should be endMonth, so initialMonth = endMonth - 2 months
95+
const expectedMonth = addMonths(endMonth, -2);
9596
expect(isSameMonth(initialMonth, expectedMonth)).toBe(true);
9697
});
9798
});

src/helpers/getInitialMonth.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ export function getInitialMonth(
3636
let initialMonth = month || defaultMonth || today;
3737
const { differenceInCalendarMonths, addMonths, startOfMonth } = dateLib;
3838

39-
// Adjust the initial month if it is after the navEnd
40-
if (navEnd && differenceInCalendarMonths(navEnd, initialMonth) < 0) {
39+
if (
40+
navEnd &&
41+
differenceInCalendarMonths(navEnd, initialMonth) < numberOfMonths - 1
42+
) {
4143
const offset = -1 * (numberOfMonths - 1);
4244
initialMonth = addMonths(navEnd, offset);
4345
}
44-
// Adjust the initial month if it is before the navStart
46+
4547
if (navStart && differenceInCalendarMonths(initialMonth, navStart) < 0) {
4648
initialMonth = navStart;
4749
}

0 commit comments

Comments
 (0)