FIX: Calendar view (month/year) is not reset to selected input date after close/reopen#725
Open
MikeP707 wants to merge 2 commits into
Open
FIX: Calendar view (month/year) is not reset to selected input date after close/reopen#725MikeP707 wants to merge 2 commits into
MikeP707 wants to merge 2 commits into
Conversation
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.
Calendar Reopen View Reset in DatePicker
Problem
When the date picker calendar is opened and the user browses to another month or year without selecting a date, closing and reopening the calendar can keep the last browsed view.
That behavior is confusing because the selected input value was never changed, so the calendar should reopen on the current selected date instead.
Current fix implementation
The fix in
packages/react-date-picker/src/DatePicker.tsxnow controls React-Calendar's view explicitly using theactiveStartDateprop.DatePicker keeps its own
activeStartDatestate and resets it when the calendar is opened. TheCalendarcomponent receives that date and updates it throughonActiveStartDateChangewhile the user navigates.What changed in
DatePicker.tsxactiveStartDatestate was added.activeStartDateis initialized from the currentvalue.activeStartDateis reset from the selected input date beforeisOpenbecomestrue.Calendaris rendered with:activeStartDate={activeStartDate ?? undefined}onActiveStartDateChange={({ activeStartDate }) => setActiveStartDate(activeStartDate)}Why this works
activeStartDateto determine which month/year to display.activeStartDatefrom the current selected value, so the calendar always starts on the selected date.Calendarcomponent across open/close cycles.Implementation details
The current code flow is:
activeStartDatefrom the selectedvalue.useState.activeStartDatewhen opening the calendar, so the first visible month/year matches the selected date.activeStartDatesynced while the calendar is open usingonActiveStartDateChange.isOpenprop updates with a small effect that resynchronizes the view whenisOpenbecomestruefrom outside.Example from current implementation
Trade-offs
Pros
Calendarcomponent on every open.key.Cons
activeStartDatestays in sync withvaluewhen the calendar is reopened.Recommendation
This controlled
activeStartDateapproach is the preferred solution for this bugfix.