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 (@flaviendelangle) #15500

Merged
merged 1 commit 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 @@ -243,7 +243,7 @@ export const usePickerValue = <
draft: initialValue,
lastPublishedValue: initialValue,
lastCommittedValue: initialValue,
lastControlledValue: inValueWithTimezoneToRender,
lastControlledValue: inValueWithoutRenderTimezone,
hasBeenModifiedSinceMount: false,
};
});
Expand Down Expand Up @@ -322,15 +322,7 @@ export const usePickerValue = <
}
});

if (
inValueWithTimezoneToRender !== undefined &&
(dateState.lastControlledValue === undefined ||
!valueManager.areValuesEqual(
utils,
dateState.lastControlledValue,
inValueWithTimezoneToRender,
))
) {
if (dateState.lastControlledValue !== inValueWithoutRenderTimezone) {
const isUpdateComingFromPicker = valueManager.areValuesEqual(
utils,
dateState.draft,
Expand All @@ -339,7 +331,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 @@ -155,7 +155,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