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..b595b10ea 100644 --- a/src/test/calendar_test.test.tsx +++ b/src/test/calendar_test.test.tsx @@ -448,6 +448,35 @@ 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( + , + ); + + expect( + container.querySelector(".react-datepicker__navigation--previous"), + ).toBe(null); + + const secondMonthDate = safeQuerySelectorAll( + container, + ".react-datepicker__day--009", + )[1]; + if (!secondMonthDate) { + throw new Error("second month date is not found"); + } + + fireEvent.click(secondMonthDate); + + expect( + container.querySelector(".react-datepicker__navigation--previous"), + ).toBe(null); + }); + describe("custom header", () => { const months = [ "January",