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] Memoize <PickersActionBar /> #16071

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { fireEvent, screen } from '@mui/internal-test-utils';
import { DesktopDatePicker } from '@mui/x-date-pickers/DesktopDatePicker';
import { createPickerRenderer, adapterToUse, openPicker } from 'test/utils/pickers';
import { describeSkipIf, testSkipIf, isJSDOM } from 'test/utils/skipIf';
import { PickersActionBar, PickersActionBarAction } from '@mui/x-date-pickers/PickersActionBar';

describe('<DesktopDatePicker />', () => {
const { render, clock } = createPickerRenderer({ clock: 'fake' });
Expand Down Expand Up @@ -349,4 +350,46 @@ describe('<DesktopDatePicker />', () => {
openPicker({ type: 'date', variant: 'desktop' });
}).toWarnDev('MUI X: `openTo="month"` is not a valid prop.');
});

describe('performance', () => {
it('should not re-render the `PickersActionBar` on date change', () => {
const RenderCount = spy((props) => <PickersActionBar {...props} />);

render(
<DesktopDatePicker
slots={{ actionBar: React.memo(RenderCount) }}
closeOnSelect={false}
open
/>,
);

const renderCountBeforeChange = RenderCount.callCount;
fireEvent.click(screen.getByRole('gridcell', { name: '2' }));
fireEvent.click(screen.getByRole('gridcell', { name: '3' }));
expect(RenderCount.callCount - renderCountBeforeChange).to.equal(0); // no re-renders after selecting new values
});

it('should not re-render the `PickersActionBar` on date change with custom callback actions with root component updates', () => {
const RenderCount = spy((props) => <PickersActionBar {...props} />);
const actions: PickersActionBarAction[] = ['clear', 'today'];

const { setProps } = render(
<DesktopDatePicker
defaultValue={adapterToUse.date('2018-01-01')}
slots={{ actionBar: React.memo(RenderCount) }}
slotProps={{ actionBar: () => ({ actions }) }}
closeOnSelect={false}
open
/>,
);

const renderCountBeforeChange = RenderCount.callCount;

setProps({ defaultValue: adapterToUse.date('2018-01-04') });

fireEvent.click(screen.getByRole('gridcell', { name: '2' }));
fireEvent.click(screen.getByRole('gridcell', { name: '3' }));
expect(RenderCount.callCount - renderCountBeforeChange).to.equal(0); // no re-renders after selecting new values and causing a root component re-render
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const PickersActionBarRoot = styled(DialogActions, {
*
* - [PickersActionBar API](https://mui.com/x/api/date-pickers/pickers-action-bar/)
*/
function PickersActionBar(props: PickersActionBarProps) {
function PickersActionBarComponent(props: PickersActionBarProps) {
const { actions, ...other } = props;

const translations = usePickerTranslations();
Expand Down Expand Up @@ -85,7 +85,7 @@ function PickersActionBar(props: PickersActionBarProps) {
return <PickersActionBarRoot {...other}>{buttons}</PickersActionBarRoot>;
}

PickersActionBar.propTypes = {
PickersActionBarComponent.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "pnpm proptypes" |
Expand Down Expand Up @@ -113,4 +113,6 @@ PickersActionBar.propTypes = {
]),
} as any;

const PickersActionBar = React.memo(PickersActionBarComponent);

export { PickersActionBar };
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ const usePickerLayout = <TValue extends PickerValidValue>(

// Action bar
const ActionBar = slots?.actionBar ?? PickersActionBar;
const actionBarProps = useSlotProps({
const {
// PickersActionBar does not use it and providing it breaks memoization
Copy link
Member

Choose a reason for hiding this comment

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

great find! 👍🏼

Copy link
Member

Choose a reason for hiding this comment

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

Strictly speaking it was forwarded to PickersActionBarRoot and so accessible through style overrides
But not documented so I think we are good.

Copy link
Member Author

Choose a reason for hiding this comment

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

Great clarification. 👍
True, but exactly, it was not even typed like it was being passed. 🙈

ownerState: destructuredOwnerState,
...actionBarProps
} = useSlotProps({
elementType: ActionBar,
externalSlotProps: slotProps?.actionBar,
additionalProps: {
Expand Down
2 changes: 1 addition & 1 deletion scripts/x-date-pickers-pro.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
{ "name": "PickerManager", "kind": "Interface" },
{ "name": "PickerOwnerState", "kind": "Interface" },
{ "name": "PickerRangeFieldSlotProps", "kind": "TypeAlias" },
{ "name": "PickersActionBar", "kind": "Function" },
{ "name": "PickersActionBar", "kind": "Variable" },
{ "name": "PickersActionBarAction", "kind": "TypeAlias" },
{ "name": "PickersActionBarProps", "kind": "Interface" },
{ "name": "PickersCalendarHeader", "kind": "Variable" },
Expand Down
2 changes: 1 addition & 1 deletion scripts/x-date-pickers.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
{ "name": "PickerLayoutOwnerState", "kind": "Interface" },
{ "name": "PickerManager", "kind": "Interface" },
{ "name": "PickerOwnerState", "kind": "Interface" },
{ "name": "PickersActionBar", "kind": "Function" },
{ "name": "PickersActionBar", "kind": "Variable" },
{ "name": "PickersActionBarAction", "kind": "TypeAlias" },
{ "name": "PickersActionBarProps", "kind": "Interface" },
{ "name": "PickersCalendarHeader", "kind": "Variable" },
Expand Down
Loading