Skip to content

Commit

Permalink
Merge pull request #5214 from OlegDev1/main
Browse files Browse the repository at this point in the history
fix: "previous month" button appears when selecting a date
  • Loading branch information
martijnrusschen authored Nov 9, 2024
2 parents ce8c371 + 0021d7d commit bc84ae7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
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

0 comments on commit bc84ae7

Please sign in to comment.