Skip to content
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

[DateRangeCalendar] Support arrow navigation with multiple months rendered #16363

Merged

Conversation

flaviendelangle
Copy link
Member

@flaviendelangle flaviendelangle commented Jan 28, 2025

Extracted from #16175

In this PR

  • Arrow navigation
  • Support clean autoFocus

Not in this PR

  • No preview when doing keyboard editing
  • Start / end date switch is weird

Work

  • Move the focused day behavior from the DayCalendar component to the useCalendarState hook to be able to support multi month navigation. DayCalendar cannot be used as standalone so there is no problem doing that.
  • Merge the changeMonth, changeFocusedDay and handleMonthChange method into a single setVisibleDate method that updates the focused day, and the current month if it changes.

@flaviendelangle flaviendelangle self-assigned this Jan 28, 2025
@flaviendelangle flaviendelangle added plan: Pro Impact at least one Pro user component: pickers This is the name of the generic UI component, not the React module! labels Jan 28, 2025
@mui-bot
Copy link

mui-bot commented Jan 28, 2025

Deploy preview: https://deploy-preview-16363--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against 8c2697f

@flaviendelangle flaviendelangle force-pushed the date-range-calendar-keyboard branch from 143a0af to 4148130 Compare January 28, 2025 08:07
@flaviendelangle flaviendelangle force-pushed the date-range-calendar-keyboard branch 2 times, most recently from 6f50b7e to e1bc77b Compare January 29, 2025 09:44
@flaviendelangle flaviendelangle force-pushed the date-range-calendar-keyboard branch from e1bc77b to a0a095c Compare January 29, 2025 10:07
@flaviendelangle flaviendelangle force-pushed the date-range-calendar-keyboard branch from 722e8f6 to 734a9e2 Compare January 29, 2025 12:30
@flaviendelangle flaviendelangle force-pushed the date-range-calendar-keyboard branch from 952977a to 66ed9f7 Compare January 29, 2025 14:47
@@ -183,8 +183,8 @@ const PickersCalendarHeader = React.forwardRef(function PickersCalendarHeader(
className: classes.switchViewIcon,
});

const selectNextMonth = () => onMonthChange(utils.addMonths(month, 1), 'left');
const selectPreviousMonth = () => onMonthChange(utils.addMonths(month, -1), 'right');
const selectNextMonth = () => onMonthChange(utils.addMonths(month, 1));
Copy link
Member Author

Choose a reason for hiding this comment

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

Do you think I should count that as a breaking change?
Strictly speaking people can keep passing the param, it's just not used anymore and instead inferred by comparing the old and new month.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure there is a need for a BC if it will not break anything on user land. 🤔

@flaviendelangle flaviendelangle force-pushed the date-range-calendar-keyboard branch from 66ed9f7 to 1c62890 Compare January 29, 2025 14:53
@flaviendelangle flaviendelangle force-pushed the date-range-calendar-keyboard branch from 72e04ef to 0aa118e Compare January 29, 2025 16:24
@flaviendelangle flaviendelangle marked this pull request as ready for review January 29, 2025 16:39
fireEvent.click(screen.getByRole('gridcell', { name: '2' }));
expect(RenderCount.callCount - renderCountBeforeChange).to.equal(4); // 2 render * 2 days
// 2 render (one to update tabIndex + autoFocus, one to update selection) * 2 days * 2 (because dev mode)
expect(RenderCount.callCount - renderCountBeforeChange).to.equal(8);
Copy link
Member Author

Choose a reason for hiding this comment

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

There is no additional render caused by this PR.
On master, we don't focus in the test and we only have 4 renders, but it's not a realistic scenario, clicking always focused and with focus with have 8 render before and after the changes.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. 👍

@flaviendelangle flaviendelangle changed the title [DateRangeCalendar] Add proper keyboard navigation [DateRangeCalendar] Add proper arrow navigation navigation Jan 30, 2025
@flaviendelangle flaviendelangle changed the title [DateRangeCalendar] Add proper arrow navigation navigation [DateRangeCalendar] Support arrow navigation with multiple months rendered Jan 30, 2025
Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

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

Great work!
Nice improvement and cleanup! 💙 💯

Here are some strange inconsistencies I noticed, but they can be left for separate issue as the behavior is currently like that as well. 🙈

  1. The Focus on Date Range Calendar jumps to the day after navigating months, while on the regular Date Calendar the focus is not moved to the day.
    I feel like the latter behavior is more correct. 🤔

P.S. For some reason the focus animation is stuck on the next button after navigation, while it is cleared on the previous button... 🤷

Screen.Recording.2025-01-31.at.15.46.41.mov

@@ -183,8 +183,8 @@ const PickersCalendarHeader = React.forwardRef(function PickersCalendarHeader(
className: classes.switchViewIcon,
});

const selectNextMonth = () => onMonthChange(utils.addMonths(month, 1), 'left');
const selectPreviousMonth = () => onMonthChange(utils.addMonths(month, -1), 'right');
const selectNextMonth = () => onMonthChange(utils.addMonths(month, 1));
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure there is a need for a BC if it will not break anything on user land. 🤔

fireEvent.click(screen.getByRole('gridcell', { name: '2' }));
expect(RenderCount.callCount - renderCountBeforeChange).to.equal(4); // 2 render * 2 days
// 2 render (one to update tabIndex + autoFocus, one to update selection) * 2 days * 2 (because dev mode)
expect(RenderCount.callCount - renderCountBeforeChange).to.equal(8);
Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. 👍

@flaviendelangle
Copy link
Member Author

The Focus on Date Range Calendar jumps to the day after navigating months, while on the regular Date Calendar the focus is not moved to the day.

I fixed it 👍
But the CI is broken (see https://mui-org.slack.com/archives/C02P87NQLJC/p1738561140312549)

@flaviendelangle flaviendelangle merged commit 6760ecf into mui:master Feb 3, 2025
18 checks passed
A-s-h-o-k pushed a commit to A-s-h-o-k/mui-x that referenced this pull request Feb 4, 2025
JCQuintas pushed a commit to JCQuintas/mui-x that referenced this pull request Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accessibility a11y component: DateRangePicker The React component. component: pickers This is the name of the generic UI component, not the React module! plan: Pro Impact at least one Pro user
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants