Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,14 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
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:
Expand All @@ -543,7 +551,7 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
);
break;
default:
allPrevDaysDisabled = monthDisabledBefore(this.state.date, this.props);
allPrevDaysDisabled = monthDisabledBefore(fromMonthDate, this.props);
break;
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/calendar_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<DatePicker
inline
monthsShown={2}
minDate={new Date("2024-11-06")}
maxDate={new Date("2025-01-01")}
/>,
);

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",
Expand Down