fix: clamp the displayed month when the navigation range changes - #3006
Open
nahuelsoria wants to merge 1 commit into
Open
fix: clamp the displayed month when the navigation range changes#3006nahuelsoria wants to merge 1 commit into
nahuelsoria wants to merge 1 commit into
Conversation
When `startMonth` or `endMonth` changed after the calendar had been rendered, the stored month was left outside the navigation range: `getDisplayMonths` then returned no months, so the month grid and the dropdowns disappeared. The month is now clamped to the navigation range on every render, which is what already happens when `month` is controlled. Closes gpbl#2912
🦋 Changeset detectedLatest commit: 5ef25b1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 7 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2912.
The problem
When
endMonth(orstartMonth) changes to a range that no longer contains the month being displayed, the month grid and the dropdowns disappear.getDisplayMonthsbreaks out of its loop as soon as the first month is pastcalendarEndMonth, so it returns an empty array and nothing is rendered:The displayed month is clamped to the navigation range by
getInitialMonth, but that only happens when the calendar mounts (and when the time zone changes). The month stored byuseControlledValueis then left out of range.Worth noting: this does not reproduce when
monthis controlled, because in that caseinitialMonthis recomputed on every render and the clamping is applied again. The uncontrolled case was the one drifting out of range.The fix
Clamp the displayed month to the navigation range on every render, reusing the existing
getInitialMonthlogic so the rule lives in one place:displayMonththen feedsgetDisplayMonths,getPreviousMonthandgetNextMonth, so the grid, the dropdowns and the navigation buttons all stay consistent with the new range. This matches the behaviour suggested in the issue: the calendar switches to the closest month it can display. No state is written, so no extra render and noonMonthChangeis fired.Tests
Three tests in
DayPicker.test.tsxcoveringendMonthnarrowing,startMonthmoving forward, andnumberOfMonths={2}. They fail onmainand pass with the fix.Verification
pnpm test: 221 suites, 1087 tests passingpnpm test:tz: 3 passingpnpm typecheckandbiome check: cleanmainbefore the change, so no regressions