The "previous month" button appears when selecting a date
Describe the bug The "previous month" button appears when selecting a date
To Reproduce Steps to reproduce the behavior:
- Go to https://codesandbox.io/p/sandbox/reactdatepicker-forked-kxtkd8
- Prev button absent
- Select date in second month
- Prev button appears
Expected behavior the "previous month" button does not appear because the calendar starts in October
Additional context Add any other context about the problem here. v.6.9.0 also reproduced in 7.4.0
This seems to fix the issue. Basically the current code relies on the date selected. However if multiple calendars are selected then it doesn't know if the min month is rendered.
Borrowed some code from renderMonths to do the necessary math (monthSelectedIn is the relative calendar offset that the date is selected in -- so in your first example it's 0, and in the second example it's 1).
So the fix is likely to consider which was the 0th calendar rendered by applying the same math. (i.e. if the first month calendar is October and the selected date is in November, then we need to subtract 1 to ensure we're checking the correct date.
not creating a PR because tests (even on a clean checkout) aren't passing for me.
diff --git a/src/calendar.tsx b/src/calendar.tsx
index 461e1c81..799de807 100644
--- a/src/calendar.tsx
+++ b/src/calendar.tsx
@@ -543,7 +543,17 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
);
break;
default:
- allPrevDaysDisabled = monthDisabledBefore(this.state.date, this.props);
+ 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);
+
+ allPrevDaysDisabled = monthDisabledBefore(fromMonthDate, this.props);
+
break;
}
@pmacmillan You just have to move all the variable declarations out of default and add some tests. Can i do that and open a PR or do you want to do it yourself?
@pmacmillan You just have to move all the variable declarations out of
defaultand add some tests. Can i do that and open a PR [...]?
Please do!
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 10 days.
This issue was closed because it has been stalled for 10 days with no activity.