Skip to content

Commit

Permalink
[fields] Support Backspace key on Android (#7842)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle authored Feb 10, 2023
1 parent 5e15293 commit 801f79b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -921,17 +921,21 @@ describe('<DateField /> - Editing', () => {

clickOnInput(input, sectionStart, sectionStart + 1);

// Remove the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('16', '') } });
act(() => {
// Remove the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('16', '') } });

// // Set the key pressed in the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('16', '2') } });
// // Set the key pressed in the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('16', '2') } });
});

// Remove the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('16', '') } });
act(() => {
// Remove the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('16', '') } });

// Set the key pressed in the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('16', '1') } });
// Set the key pressed in the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('16', '1') } });
});

expectInputValue(input, '05 / 21 / 2022');
});
Expand All @@ -950,17 +954,21 @@ describe('<DateField /> - Editing', () => {

clickOnInput(input, sectionStart, sectionStart + 1);

// Remove the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('May', '') } });
act(() => {
// Remove the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('May', '') } });

// // Set the key pressed in the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('May', 'J') } });
// // Set the key pressed in the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('May', 'J') } });
});

// Remove the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('May', '') } });
act(() => {
// Remove the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('May', '') } });

// Set the key pressed in the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('May', 'u') } });
// Set the key pressed in the selected section
fireEvent.change(input, { target: { value: initialValueStr.replace('May', 'u') } });
});

expectInputValue(input, 'June 2022');
});
Expand Down
11 changes: 11 additions & 0 deletions packages/x-date-pickers/src/internals/hooks/useField/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,17 @@ export const useField = <
return () => window.clearTimeout(focusTimeoutRef.current);
}, []); // eslint-disable-line react-hooks/exhaustive-deps

// If `state.tempValueStrAndroid` is still defined when running `useEffect`,
// Then `onChange` has only been called once, which means the user pressed `Backspace` to reset the section.
// This causes a small flickering on Android,
// But we can't use `useEnhancedEffect` which is always called before the second `onChange` call and then would cause false positives.
React.useEffect(() => {
if (state.tempValueStrAndroid != null && selectedSectionIndexes != null) {
resetCharacterQuery();
clearActiveSection();
}
}, [state.tempValueStrAndroid]); // eslint-disable-line react-hooks/exhaustive-deps

const valueStr = React.useMemo(
() => state.tempValueStrAndroid ?? fieldValueManager.getValueStrFromSections(state.sections),
[state.sections, fieldValueManager, state.tempValueStrAndroid],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export const useFieldState = <
return setState((prevState) => ({
...prevState,
sections: newSections,
tempValueStrAndroid: null,
...newValue,
}));
};
Expand Down

0 comments on commit 801f79b

Please sign in to comment.