Skip to content

Commit 3144e0c

Browse files
[pickers] Always use props.value when it changes (@flaviendelangle) (#15500)
Co-authored-by: Flavien DELANGLE <[email protected]>
1 parent 5f62979 commit 3144e0c

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

packages/x-date-pickers/src/DesktopDatePicker/tests/field.DesktopDatePicker.test.tsx

+42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { fireEvent } from '@mui/internal-test-utils';
22
import { DesktopDatePicker, DesktopDatePickerProps } from '@mui/x-date-pickers/DesktopDatePicker';
3+
import { fireUserEvent } from 'test/utils/fireUserEvent';
34
import {
45
createPickerRenderer,
56
buildFieldInteractions,
@@ -98,6 +99,47 @@ describe('<DesktopDatePicker /> - Field', () => {
9899
testFormat({ views: ['year', 'month', 'day'] }, 'MM/DD/YYYY');
99100
testFormat({ views: ['year', 'day'] }, 'MM/DD/YYYY');
100101
});
102+
103+
it('should allow to set the value to its previous valid value using props.value', () => {
104+
// Test with accessible DOM structure
105+
let view = renderWithProps(
106+
{
107+
enableAccessibleFieldDOMStructure: true as const,
108+
value: adapterToUse.date('2022-10-31'),
109+
},
110+
{ componentFamily: 'picker' },
111+
);
112+
113+
view.selectSection('month');
114+
expectFieldValueV7(view.getSectionsContainer(), '10/31/2022');
115+
116+
view.pressKey(0, 'ArrowUp');
117+
expectFieldValueV7(view.getSectionsContainer(), '11/31/2022');
118+
119+
view.setProps({ value: adapterToUse.date('2022-10-31') });
120+
expectFieldValueV7(view.getSectionsContainer(), '10/31/2022');
121+
122+
view.unmount();
123+
124+
// Test with non-accessible DOM structure
125+
view = renderWithProps(
126+
{
127+
enableAccessibleFieldDOMStructure: false as const,
128+
value: adapterToUse.date('2022-10-31'),
129+
},
130+
{ componentFamily: 'picker' },
131+
);
132+
133+
const input = getTextbox();
134+
view.selectSection('month');
135+
expectFieldValueV6(input, '10/31/2022');
136+
137+
fireUserEvent.keyPress(input, { key: 'ArrowUp' });
138+
expectFieldValueV6(input, '11/31/2022');
139+
140+
view.setProps({ value: adapterToUse.date('2022-10-31') });
141+
expectFieldValueV6(input, '10/31/2022');
142+
});
101143
});
102144

103145
describe('slots: field', () => {

packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export const usePickerValue = <
243243
draft: initialValue,
244244
lastPublishedValue: initialValue,
245245
lastCommittedValue: initialValue,
246-
lastControlledValue: inValueWithTimezoneToRender,
246+
lastControlledValue: inValueWithoutRenderTimezone,
247247
hasBeenModifiedSinceMount: false,
248248
};
249249
});
@@ -322,15 +322,7 @@ export const usePickerValue = <
322322
}
323323
});
324324

325-
if (
326-
inValueWithTimezoneToRender !== undefined &&
327-
(dateState.lastControlledValue === undefined ||
328-
!valueManager.areValuesEqual(
329-
utils,
330-
dateState.lastControlledValue,
331-
inValueWithTimezoneToRender,
332-
))
333-
) {
325+
if (dateState.lastControlledValue !== inValueWithoutRenderTimezone) {
334326
const isUpdateComingFromPicker = valueManager.areValuesEqual(
335327
utils,
336328
dateState.draft,
@@ -339,7 +331,7 @@ export const usePickerValue = <
339331

340332
setDateState((prev) => ({
341333
...prev,
342-
lastControlledValue: inValueWithTimezoneToRender,
334+
lastControlledValue: inValueWithoutRenderTimezone,
343335
...(isUpdateComingFromPicker
344336
? {}
345337
: {

packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface UsePickerValueState<TValue> {
155155
*/
156156
lastCommittedValue: TValue;
157157
/**
158-
* Last value passed with `props.value`.
158+
* Last value passed to `props.value`.
159159
* Used to update the `draft` value whenever the `value` prop changes.
160160
*/
161161
lastControlledValue: TValue | undefined;

0 commit comments

Comments
 (0)