From 77141399fd5664f041a6494a9a9d3fa891a495e9 Mon Sep 17 00:00:00 2001 From: flavien Date: Fri, 7 Mar 2025 14:57:29 +0100 Subject: [PATCH] Work --- .../usePicker/hooks/useValueAndOpenStates.ts | 30 +++++++++---------- .../describeValue/testPickerActionBar.tsx | 4 +-- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/x-date-pickers/src/internals/hooks/usePicker/hooks/useValueAndOpenStates.ts b/packages/x-date-pickers/src/internals/hooks/usePicker/hooks/useValueAndOpenStates.ts index bc69dcbb04238..09a847e5075b6 100644 --- a/packages/x-date-pickers/src/internals/hooks/usePicker/hooks/useValueAndOpenStates.ts +++ b/packages/x-date-pickers/src/internals/hooks/usePicker/hooks/useValueAndOpenStates.ts @@ -127,8 +127,6 @@ export function useValueAndOpenStates< shouldClose = changeImportance === 'accept', } = options ?? {}; - const isEqualToCurrentValue = valueManager.areValuesEqual(utils, newValue, value); - let shouldFireOnChange: boolean; let shouldFireOnAccept: boolean; if (!skipPublicationIfPristine && !isValueControlled && !state.hasBeenModifiedSinceMount) { @@ -137,21 +135,19 @@ export function useValueAndOpenStates< shouldFireOnChange = true; shouldFireOnAccept = changeImportance === 'accept'; } else { - shouldFireOnChange = !isEqualToCurrentValue; + shouldFireOnChange = !valueManager.areValuesEqual(utils, newValue, value); shouldFireOnAccept = changeImportance === 'accept' && !valueManager.areValuesEqual(utils, newValue, state.lastCommittedValue); } - if (!isEqualToCurrentValue) { - setState((prevState) => ({ - ...prevState, - // We reset the shallow value whenever we fire onChange. - clockShallowValue: shouldFireOnChange ? undefined : prevState.clockShallowValue, - lastCommittedValue: shouldFireOnAccept ? newValue : prevState.lastCommittedValue, - hasBeenModifiedSinceMount: true, - })); - } + setState((prevState) => ({ + ...prevState, + // We reset the shallow value whenever we fire onChange. + clockShallowValue: shouldFireOnChange ? undefined : prevState.clockShallowValue, + lastCommittedValue: shouldFireOnAccept ? value : prevState.lastCommittedValue, + hasBeenModifiedSinceMount: true, + })); let cachedContext: PickerChangeHandlerContext | null = null; const getContext = (): PickerChangeHandlerContext => { @@ -187,9 +183,7 @@ export function useValueAndOpenStates< setState((prevState) => ({ ...prevState, lastExternalValue: value, - internalValueDependencies: { timezone }, - internalValue: value, - lastCommittedValue: value, + clockShallowValue: undefined, hasBeenModifiedSinceMount: true, })); } @@ -224,7 +218,11 @@ export function useValueAndOpenStates< }, [isOpenControlled, openProp]); const viewValue = React.useMemo( - () => valueManager.cleanValue(utils, state.clockShallowValue ?? value), + () => + valueManager.cleanValue( + utils, + state.clockShallowValue === undefined ? value : state.clockShallowValue, + ), [utils, valueManager, state.clockShallowValue, value], ); diff --git a/test/utils/pickers/describeValue/testPickerActionBar.tsx b/test/utils/pickers/describeValue/testPickerActionBar.tsx index 7497f806866c1..c358ba5161edd 100644 --- a/test/utils/pickers/describeValue/testPickerActionBar.tsx +++ b/test/utils/pickers/describeValue/testPickerActionBar.tsx @@ -93,13 +93,13 @@ export const testPickerActionBar: DescribeValueTestSuite = ( onAccept, onClose, open: true, - value: values[0], + defaultValue: values[0], slotProps: { actionBar: { actions: ['cancel'] } }, closeOnSelect: false, }); // Change the value (already tested) - setNewValue(values[0], { isOpened: true, selectSection, pressKey }); + setNewValue(values[1], { isOpened: true, selectSection, pressKey }); // Cancel the modifications fireEvent.click(screen.getByText(/cancel/i));