From 70694855e2db0968faff8b646e3145bb12903541 Mon Sep 17 00:00:00 2001 From: Oleg <67059482+OlegDev1@users.noreply.github.com> Date: Thu, 7 Nov 2024 13:47:04 +0000 Subject: [PATCH 1/2] fix: "previous month" button appears when selecting a date --- src/calendar.tsx | 10 +++++++++- src/test/calendar_test.test.tsx | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/calendar.tsx b/src/calendar.tsx index 461e1c81e..b598b0fd4 100644 --- a/src/calendar.tsx +++ b/src/calendar.tsx @@ -528,6 +528,14 @@ export default class Calendar extends Component { return; } + const monthsShown = + this.props.monthsShown ?? Calendar.defaultProps.monthsShown; + const monthsToSubtract = this.props.showPreviousMonths + ? monthsShown - 1 + : 0; + const monthSelectedIn = this.props.monthSelectedIn ?? monthsToSubtract; + const fromMonthDate = subMonths(this.state.date, monthSelectedIn); + let allPrevDaysDisabled; switch (true) { case this.props.showMonthYearPicker: @@ -543,7 +551,7 @@ export default class Calendar extends Component { ); break; default: - allPrevDaysDisabled = monthDisabledBefore(this.state.date, this.props); + allPrevDaysDisabled = monthDisabledBefore(fromMonthDate, this.props); break; } diff --git a/src/test/calendar_test.test.tsx b/src/test/calendar_test.test.tsx index e3c2cae9b..8f39d1daa 100644 --- a/src/test/calendar_test.test.tsx +++ b/src/test/calendar_test.test.tsx @@ -448,6 +448,27 @@ describe("Calendar", () => { expect(nextButtonAriaLabel).toBe(nextYearAriaLabel); }); + it("should not have previous month button when selecting a date in the second month, when min date is specified", () => { + const { container } = render( + , + ); + + const secondMonthDate = safeQuerySelectorAll( + container, + ".react-datepicker__day--009", + )[1]; + fireEvent.click(secondMonthDate!); + + expect( + container.querySelector(".react-datepicker__navigation--previous"), + ).toBe(null); + }); + describe("custom header", () => { const months = [ "January", From 0021d7d4c0e0683d55dfaa7c7913f385a70f12ce Mon Sep 17 00:00:00 2001 From: Oleg <67059482+OlegDev1@users.noreply.github.com> Date: Fri, 8 Nov 2024 14:37:47 +0000 Subject: [PATCH 2/2] test: add additional expect and check for undefined --- src/test/calendar_test.test.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/test/calendar_test.test.tsx b/src/test/calendar_test.test.tsx index 8f39d1daa..b595b10ea 100644 --- a/src/test/calendar_test.test.tsx +++ b/src/test/calendar_test.test.tsx @@ -458,11 +458,19 @@ describe("Calendar", () => { />, ); + expect( + container.querySelector(".react-datepicker__navigation--previous"), + ).toBe(null); + const secondMonthDate = safeQuerySelectorAll( container, ".react-datepicker__day--009", )[1]; - fireEvent.click(secondMonthDate!); + if (!secondMonthDate) { + throw new Error("second month date is not found"); + } + + fireEvent.click(secondMonthDate); expect( container.querySelector(".react-datepicker__navigation--previous"),