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

[pickers] Support onMonthChange / onYearChange in day view #6954

Closed
wants to merge 4 commits into from

Conversation

flaviendelangle
Copy link
Member

@flaviendelangle flaviendelangle commented Nov 22, 2022

Fixes #6953

@flaviendelangle flaviendelangle added bug 🐛 Something doesn't work component: pickers This is the name of the generic UI component, not the React module! labels Nov 22, 2022
@mui-bot
Copy link

mui-bot commented Nov 22, 2022

Messages
📖 Netlify deploy preview: https://deploy-preview-6954--material-ui-x.netlify.app/

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 615.8 1,000.1 629.7 814.82 160.163
Sort 100k rows ms 624.6 1,149.2 1,149.2 951.56 202.224
Select 100k rows ms 216 301 260.6 262.36 34.297
Deselect 100k rows ms 176.6 298.6 214.3 231.3 41.861

Generated by 🚫 dangerJS against a2b32d6

@@ -473,7 +476,7 @@ export const DateCalendar = React.forwardRef(function DateCalendar<TDate>(
openView={openView}
currentMonth={calendarState.currentMonth}
onViewChange={setOpenView}
onMonthChange={(newMonth, direction) => handleChangeMonth({ newMonth, direction })}
onMonthChange={slideToMonth}
Copy link
Member Author

Choose a reason for hiding this comment

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

I now compute the direction inside useCalendarState to have a single month change method exposed

@@ -71,18 +72,27 @@ export const createCalendarStateReducer =
!disableSwitchToMonthOnDayFocus &&
!utils.isSameMonth(state.currentMonth, action.focusedDay);

let slideDirection: SlideDirection;
if (!needMonthSwitch) {
slideDirection = state.slideDirection;
Copy link
Member Author

Choose a reason for hiding this comment

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

Before, the slide to the new month was caused by the useEffect tracking the external value.
So the focus of the clicked day was already done
But now it is the click itself which caused the slide (like when you click on the array left / right).

Without this check, the focus was resetting the slide direction, causing it to go on the wrong direction in some scenario.

type: 'changeMonth',
...payload,
});
const slideToMonth = useEventCallback((newDate: TDate) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed to make clear that we are not updating any value, just "meta data" used to handle the visually displayed month

Copy link
Member

Choose a reason for hiding this comment

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

If we are going this route, then maybe onYearChange should also be called when year changes when navigating months (not only when choosing a date in a different month—which is also in a different year)? 🤔

setValue(newValue);
onChange?.(newValue, selectionState);

if (newValue != null) {
Copy link
Member Author

@flaviendelangle flaviendelangle Nov 22, 2022

Choose a reason for hiding this comment

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

We now do the checks here to make sure that any value change causes a call to onMonthChange /onYearChange if needed

@flaviendelangle flaviendelangle changed the title [pickers] Call onMonthChange when clicking on a day from another month [pickers] Call onMonthChange when clicking on a day from another month Nov 22, 2022
@flaviendelangle flaviendelangle self-assigned this Nov 22, 2022
@flaviendelangle flaviendelangle changed the title [pickers] Call onMonthChange when clicking on a day from another month [pickers] Support onMonthChange / onYearChange in day view Nov 23, 2022
@flaviendelangle flaviendelangle marked this pull request as ready for review November 23, 2022 07:07
Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

The implementation sounds good to be a fix on master. For next I'm wondering if we should rename onMonthChange.

When I see onMonthChange I expect it to get called when the selected value moves from one month to an other. But it's also called when I press Next/Previous month buttons.

So maybe onVisibleMonthChange would be more appropriate

@flaviendelangle
Copy link
Member Author

When I see onMonthChange I expect it to get called when the selected value moves from one month to an other. But it's also called when I press Next/Previous month buttons.

Good point
@LukasTy if you have an opinion
I don't really know to be honest

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.

IMO—this implementation seems incorrect.
Even the documentation states that onYearChange and onMonthChange are callbacks called when the year or month changes, but now onMonthChange is called when navigating months—which contradicts the docs.

I'm with Alex on this one.
If we want to emit year or month navigation changes—they ought to be separate callbacks in order to avoid possible head-aches.

Comment on lines 220 to +222
const selectNextMonth = React.useCallback(() => {
changeMonth(utils.getNextMonth(currentMonth));
}, [changeMonth, currentMonth, utils]);
slideToMonth(utils.getNextMonth(currentMonth));
}, [slideToMonth, currentMonth, utils]);
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: Should we maybe replace these with useEventCallback? 🤔

type: 'changeMonth',
...payload,
});
const slideToMonth = useEventCallback((newDate: TDate) => {
Copy link
Member

Choose a reason for hiding this comment

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

If we are going this route, then maybe onYearChange should also be called when year changes when navigating months (not only when choosing a date in a different month—which is also in a different year)? 🤔

@github-actions
Copy link

github-actions bot commented Dec 2, 2022

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: pickers This is the name of the generic UI component, not the React module! PR: out-of-date The pull request has merge conflicts and can't be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pickers] onMonthChange doesn't call after click to days outside the current month
4 participants