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] Always use props.value when it changes #15490

Merged
merged 2 commits into from
Nov 20, 2024
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
@@ -1,5 +1,6 @@
import { fireEvent } from '@mui/internal-test-utils';
import { DesktopDatePicker, DesktopDatePickerProps } from '@mui/x-date-pickers/DesktopDatePicker';
import { fireUserEvent } from 'test/utils/fireUserEvent';
import {
createPickerRenderer,
buildFieldInteractions,
Expand Down Expand Up @@ -98,6 +99,47 @@ describe('<DesktopDatePicker /> - Field', () => {
testFormat({ views: ['year', 'month', 'day'] }, 'MM/DD/YYYY');
testFormat({ views: ['year', 'day'] }, 'MM/DD/YYYY');
});

it('should allow to set the value to its previous valid value using props.value', () => {
// Test with accessible DOM structure
let view = renderWithProps(
{
enableAccessibleFieldDOMStructure: true as const,
value: adapterToUse.date('2022-10-31'),
},
{ componentFamily: 'picker' },
);

view.selectSection('month');
expectFieldValueV7(view.getSectionsContainer(), '10/31/2022');

view.pressKey(0, 'ArrowUp');
expectFieldValueV7(view.getSectionsContainer(), '11/31/2022');

view.setProps({ value: adapterToUse.date('2022-10-31') });
expectFieldValueV7(view.getSectionsContainer(), '10/31/2022');

view.unmount();

// Test with non-accessible DOM structure
view = renderWithProps(
{
enableAccessibleFieldDOMStructure: false as const,
value: adapterToUse.date('2022-10-31'),
},
{ componentFamily: 'picker' },
);

const input = getTextbox();
view.selectSection('month');
expectFieldValueV6(input, '10/31/2022');

fireUserEvent.keyPress(input, { key: 'ArrowUp' });
expectFieldValueV6(input, '11/31/2022');

view.setProps({ value: adapterToUse.date('2022-10-31') });
expectFieldValueV6(input, '10/31/2022');
});
});

describe('slots: field', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export const usePickerValue = <
draft: initialValue,
lastPublishedValue: initialValue,
lastCommittedValue: initialValue,
lastControlledValue: inValueWithTimezoneToRender,
lastControlledValue: inValueWithoutRenderTimezone,
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to store the raw props.value variable if we want to do a reference comparison

hasBeenModifiedSinceMount: false,
};
});
Expand Down Expand Up @@ -302,15 +302,7 @@ export const usePickerValue = <
}
});

if (
inValueWithTimezoneToRender !== undefined &&
(dateState.lastControlledValue === undefined ||
!valueManager.areValuesEqual(
utils,
dateState.lastControlledValue,
inValueWithTimezoneToRender,
))
) {
if (dateState.lastControlledValue !== inValueWithoutRenderTimezone) {
Copy link
Member Author

Choose a reason for hiding this comment

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

We compare the old props.value with the new props.value (without any processing done to it)
If it changed:

  1. We set the new props.value in the state
  2. If the new derived props.value with the right timezone is not equal to the current draft, we update the draft

const isUpdateComingFromPicker = valueManager.areValuesEqual(
utils,
dateState.draft,
Expand All @@ -319,7 +311,7 @@ export const usePickerValue = <

setDateState((prev) => ({
...prev,
lastControlledValue: inValueWithTimezoneToRender,
lastControlledValue: inValueWithoutRenderTimezone,
...(isUpdateComingFromPicker
? {}
: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export interface UsePickerValueState<TValue> {
*/
lastCommittedValue: TValue;
/**
* Last value passed with `props.value`.
* Last value passed to `props.value`.
* Used to update the `draft` value whenever the `value` prop changes.
*/
lastControlledValue: TValue | undefined;
Expand Down