Skip to content

Commit 6fb7cb0

Browse files
authored
[fields] Prevent infinite recursion when ensuring selection (#13779)
1 parent ed833b5 commit 6fb7cb0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

packages/x-date-pickers/src/internals/hooks/useField/useFieldV6TextField.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const addPositionPropertiesToSections = <TSection extends FieldSection>(
7777
export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
7878
const isRtl = useRtl();
7979
const focusTimeoutRef = React.useRef<ReturnType<typeof setTimeout>>();
80+
const selectionSyncTimeoutRef = React.useRef<ReturnType<typeof setTimeout>>();
8081

8182
const {
8283
forwardedProps: {
@@ -162,12 +163,16 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
162163
inputRef.current.setSelectionRange(selectionStart, selectionEnd);
163164
}
164165
}
165-
setTimeout(() => {
166+
clearTimeout(selectionSyncTimeoutRef.current);
167+
selectionSyncTimeoutRef.current = setTimeout(() => {
166168
// handle case when the selection is not updated correctly
167169
// could happen on Android
168170
if (
169171
inputRef.current &&
170172
inputRef.current === getActiveElement(document) &&
173+
// The section might loose all selection, where `selectionStart === selectionEnd`
174+
// https://github.com/mui/mui-x/pull/13652
175+
inputRef.current.selectionStart === inputRef.current.selectionEnd &&
171176
(inputRef.current.selectionStart !== selectionStart ||
172177
inputRef.current.selectionEnd !== selectionEnd)
173178
) {
@@ -428,6 +433,7 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
428433

429434
return () => {
430435
clearTimeout(focusTimeoutRef.current);
436+
clearTimeout(selectionSyncTimeoutRef.current);
431437
};
432438
}, []); // eslint-disable-line react-hooks/exhaustive-deps
433439

0 commit comments

Comments
 (0)