Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Mar 7, 2025
1 parent a9ae5cd commit d7aec39
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<TError> | null = null;
const getContext = (): PickerChangeHandlerContext<TError> => {
Expand Down Expand Up @@ -187,9 +183,7 @@ export function useValueAndOpenStates<
setState((prevState) => ({
...prevState,
lastExternalValue: value,
internalValueDependencies: { timezone },
internalValue: value,
lastCommittedValue: value,
clockShallowValue: undefined,
hasBeenModifiedSinceMount: true,
}));
}
Expand Down Expand Up @@ -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],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export interface UsePickerState<TValue extends PickerValidValue> {
* Then clicking on "Accept", "Today" or "Clear" should fire `onAccept` with `defaultValue`, but clicking on "Cancel" or dismissing the picker should not.
*/
hasBeenModifiedSinceMount: boolean;
shouldFireOnAccept: boolean;
}

export interface PickerViewsRendererBaseExternalProps
Expand Down
4 changes: 2 additions & 2 deletions test/utils/pickers/describeValue/testPickerActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ export const testPickerActionBar: DescribeValueTestSuite<any, 'picker'> = (
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));
Expand Down

0 comments on commit d7aec39

Please sign in to comment.