[DateRangeCalendar] Avoid unnecessary month switch when the value is already visible#22996
Conversation
Deploy previewhttps://deploy-preview-22996--material-ui-x.netlify.app/Bundle size
Check out the code infra dashboard for more information about this PR. |
There was a problem hiding this comment.
Pull request overview
This PR updates DateRangeCalendar’s auto month-switching logic so that when the controlled range value changes, the calendar window only scrolls if the newly requested date is not already visible—including when the visible window includes months rendered before the “current month” slot (currentMonthCalendarPosition > 1). It replaces month-index arithmetic with a date-window check aligned with the visible-month window logic used elsewhere in the component.
Changes:
- Replace month-number comparison with a visible-window range check using
adapter.isWithinRange. - Compute the first/last visible month bounds based on
currentMonthCalendarPositionandcalendars. - Add a regression test covering
currentMonthCalendarPosition > 1where the requested date is visible in an earlier calendar.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.tsx | Updates the auto month-switch effect to use a visible-window date range rather than month indices. |
| packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.test.tsx | Adds a regression test ensuring no scroll occurs when the requested month is already visible in an earlier calendar. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
be46ebe to
955227c
Compare
…already visible Follow-up to mui#22987, which fixed the unnecessary auto month-switch across the year boundary for the default `currentMonthCalendarPosition`. The controlled-value-change effect tested the requested month against `[currentMonth, currentMonth + displayingMonthRange]`. That lower bound ignores the `currentMonthCalendarPosition - 1` calendars rendered before the current month, so for `currentMonthCalendarPosition > 1` a value already visible in an earlier calendar still scrolled the window. Correct the lower bound to `currentMonthIndex - (currentMonthCalendarPosition - 1)`. The comparison keeps using absolute month indices (`year * 12 + month`), which are time-insensitive and stay correct across year boundaries, so no date normalization is needed. Co-Authored-By: Claude Opus 4.8 <[email protected]>
955227c to
48b492f
Compare
|
Double-checked with GPT 5.4 review, which didn't find anything to flag. |
Follow-up to #22987, which fixed the unnecessary auto month-switch across the year boundary for the default
currentMonthCalendarPosition.What
When the range value changes, an effect decides whether to scroll the visible months so the changed date stays visible. It compared the requested month against
[currentMonth, currentMonth + displayingMonthRange]. That lower bound iscurrentMonth, but the real first visible month iscurrentMonth - (currentMonthCalendarPosition - 1)-- it ignores the calendars rendered before the current-month slot. So forcurrentMonthCalendarPosition > 1, setting the value to a date already visible in an earlier calendar still scrolled the window unnecessarily.The fix corrects the lower bound to
currentMonthIndex - (currentMonthCalendarPosition - 1). It keeps the absolute month-index comparison (year * 12 + month) introduced in #22987, which is time-insensitive (a date-range check would be sensitive to thereferenceDatetime carried bycurrentMonth) and stays correct across year boundaries, so no date normalization is needed.The effect keys on
[rangePosition, value], so it runs both when the value is set from the outside and when a day is selected in the calendar; the single fix corrects both.Why it's safe
The set of value changes that scroll is now a strict subset of before: the fix only ever removes scrolls, and every removed scroll was a case where the requested date was already visible. It never introduces a scroll that did not happen before, and the landing month for the cases that still scroll is unchanged.
Behavior-only bugfix, so this is not a breaking change: no props, types, defaults, slots, CSS, or DOM changed. The
currentMonthCalendarPositiondocs describe the slot the current month is rendered in, not an invariant re-established on every value change.Testing
Added regression tests for the
currentMonthCalendarPosition > 1case and for a value landing on the first visible day earlier in the day than the reference time (guarding the time-insensitivity of the check). The existingcurrentMonthCalendarPositiontest and both new ones pass.