Skip to content

Commit

Permalink
[fields] Support Backspace key on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Feb 6, 2023
1 parent a25e098 commit 44032c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 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 @@ -372,6 +372,16 @@ 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 the two `onChange` calls and would cause false positives.
React.useEffect(() => {
if (state.tempValueStrAndroid != null && selectedSectionIndexes != null) {
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 44032c6

Please sign in to comment.