Skip to content

[DateRangeCalendar] Avoid unnecessary month switch when the value is already visible#22996

Merged
LukasTy merged 2 commits into
mui:masterfrom
LukasTy:claude/sad-thompson-98c96d
Jul 9, 2026
Merged

[DateRangeCalendar] Avoid unnecessary month switch when the value is already visible#22996
LukasTy merged 2 commits into
mui:masterfrom
LukasTy:claude/sad-thompson-98c96d

Conversation

@LukasTy

@LukasTy LukasTy commented Jun 29, 2026

Copy link
Copy Markdown
Member

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 is currentMonth, but the real first visible month is currentMonth - (currentMonthCalendarPosition - 1) -- it ignores the calendars rendered before the current-month slot. So for currentMonthCalendarPosition > 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 the referenceDate time carried by currentMonth) 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 currentMonthCalendarPosition docs 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 > 1 case 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 existing currentMonthCalendarPosition test and both new ones pass.

@code-infra-dashboard

code-infra-dashboard Bot commented Jun 29, 2026

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-22996--material-ui-x.netlify.app/
QR code for https://deploy-preview-22996--material-ui-x.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/x-data-grid 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-pro 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-premium 0B(0.00%) 0B(0.00%)
@mui/x-charts 0B(0.00%) 0B(0.00%)
@mui/x-charts-pro 0B(0.00%) 0B(0.00%)
@mui/x-charts-premium 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 🔺+14B(+0.01%) 🔺+9B(+0.01%)
@mui/x-tree-view 0B(0.00%) 0B(0.00%)
@mui/x-tree-view-pro 0B(0.00%) 0B(0.00%)
@mui/x-scheduler 0B(0.00%) 0B(0.00%)
@mui/x-scheduler-premium 0B(0.00%) 0B(0.00%)
@mui/x-chat 0B(0.00%) 0B(0.00%)
@mui/x-license 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@LukasTy LukasTy added type: bug It doesn't behave as expected. plan: Pro Impact at least one Pro user. scope: pickers Changes related to the date/time pickers. component: DateRangePicker The React component labels Jun 29, 2026
@LukasTy LukasTy self-assigned this Jul 1, 2026
@LukasTy
LukasTy marked this pull request as ready for review July 1, 2026 12:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 currentMonthCalendarPosition and calendars.
  • Add a regression test covering currentMonthCalendarPosition > 1 where 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.

Comment thread packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.tsx Outdated
@LukasTy
LukasTy force-pushed the claude/sad-thompson-98c96d branch from be46ebe to 955227c Compare July 1, 2026 12:35
…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]>
@LukasTy
LukasTy force-pushed the claude/sad-thompson-98c96d branch from 955227c to 48b492f Compare July 1, 2026 12:51
@LukasTy
LukasTy requested a review from Copilot July 1, 2026 12:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@LukasTy

LukasTy commented Jul 9, 2026

Copy link
Copy Markdown
Member Author

Double-checked with GPT 5.4 review, which didn't find anything to flag.

@LukasTy
LukasTy merged commit 131e961 into mui:master Jul 9, 2026
21 checks passed
@LukasTy
LukasTy deleted the claude/sad-thompson-98c96d branch July 9, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: DateRangePicker The React component plan: Pro Impact at least one Pro user. scope: pickers Changes related to the date/time pickers. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants