Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: add separate state for calendar selected date #4207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from

Conversation

alfaro-v
Copy link

@alfaro-v alfaro-v commented Mar 26, 2025

Fix DatePicker calendar selection persistence when changing months

Description

This PR addresses a UX issue in the DatePicker component where the selected date is lost when navigating between months. Currently, when a user selects a date and then navigates to a different month, the calendar treats the navigation date as the selected date, which is inconsistent with user expectations.

I've investigated several popular date picker implementations including MUI, Angular Material, Ant Design, and jQuery UI Datepicker - none of these exhibit this behavior. In all these implementations, the selected date remains distinct from the navigation date when browsing through months.

Proposed Solution

The core issue is that our calendar doesn't properly distinguish between:

  1. The date used for navigation/display (calendarDate)
  2. The date that was actually selected by the user (selectedDate)

This PR introduces a separate state for tracking the selected date by:

  1. Adding a selectedDate property to the Calendar context
  2. Extending the useCalendarDate hook to maintain separate states
  3. Updating the GridRow component to properly render selected dates
    based on both date equality and month context

Implementation Details

  • Modified CalendarProvider.ts to include selectedDate in the context
  • Enhanced useCalendarDate.ts to track both calendarDate and selectedDate separately
  • Updated the handleCalendarSelect method in DatePicker.tsx to update both states
  • Passed the appropriate selectedDate to the CalendarContainer
  • Adjusted selection logic in GridRow.tsx to handle both states correctly

Note

I'm submitting this PR as an illustration of the problem and a possible solution approach. This implementation may not be complete as there might be edge cases I haven't addressed. I welcome feedback and suggestions on how to improve this solution or alternative approaches to solve the issue.

Testing

  • Tested manually by selecting a date and navigating between months
  • Verified that the originally selected date retains its "selected" state when returning to its month

This PR introduces a separate state for tracking the selected date by:

1. Adding a `selectedDate` property to the Calendar context
2. Extending the `useCalendarDate` hook to maintain separate states
3. Updating the GridRow component to properly render selected dates
based on both date equality and month context
Copy link

codesandbox bot commented Mar 26, 2025

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

vercel bot commented Mar 26, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rsuite-nextjs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 26, 2025 2:33pm
rsuite-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 26, 2025 2:33pm

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

@Copilot Copilot AI left a comment

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 fixes an issue in the DatePicker component where the selected date was lost when navigating between months by introducing a separate selected date state.

  • Updated DatePicker.tsx to use the new selectedDate state
  • Modified useCalendarDate.ts to manage both calendarDate and selectedDate
  • Adjusted GridRow.tsx, CalendarProvider.ts, and CalendarContainer.tsx to work with the new selectedDate

Reviewed Changes

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

Show a summary per file
File Description
src/DatePicker/DatePicker.tsx Updated state handling and selection logic for the DatePicker
src/Calendar/hooks/useCalendarDate.ts Added selectedDate state and setSelectedDate callback with a potential dependency issue
src/Calendar/Grid/GridRow.tsx Updated selection logic to use selectedDate and added month checking
src/Calendar/CalendarProvider.ts Added selectedDate field in Calendar context
src/Calendar/CalendarContainer.tsx Passed selectedDate through context to CalendarContainer

@@ -15,6 +16,14 @@ export const useCalendarDate = (value: Date | null | undefined, defaultDate: Dat
},
[calendarDate]
);
const setSelectedDate = useCallback(
(date: React.SetStateAction<Date> | undefined) => {
if (date && date?.valueOf() !== selectedDate?.valueOf()) {
Copy link
Preview

Copilot AI Mar 27, 2025

Choose a reason for hiding this comment

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

The dependency array for the setSelectedDate callback only includes 'calendarDate'. It should also include 'selectedDate' to ensure the callback captures the latest selectedDate value.

Copilot uses AI. Check for mistakes.

Positive FeedbackNegative Feedback
@@ -469,7 +467,8 @@ const DatePicker: RsRefForwardingComponent<'div', DatePickerProps> = React.forwa
* The callback triggered after clicking the OK button.
*/
const handleOK = useEventCallback((event: React.SyntheticEvent) => {
updateValue(event);
console.log('selectedDate', selectedDate);
Copy link
Preview

Copilot AI Mar 27, 2025

Choose a reason for hiding this comment

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

A debugging statement is present in production code; consider removing it to clean up the output.

Suggested change
console.log('selectedDate', selectedDate);

Copilot uses AI. Check for mistakes.

Positive FeedbackNegative Feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.