Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fields] Prevent infinite recursion when ensuring selection #13779

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const addPositionPropertiesToSections = <TSection extends FieldSection>(
export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
const isRtl = useRtl();
const focusTimeoutRef = React.useRef<ReturnType<typeof setTimeout>>();
const selectionSyncTimeoutRef = React.useRef<ReturnType<typeof setTimeout>>();

const {
forwardedProps: {
Expand Down Expand Up @@ -162,12 +163,16 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
inputRef.current.setSelectionRange(selectionStart, selectionEnd);
}
}
setTimeout(() => {
clearTimeout(selectionSyncTimeoutRef.current);
selectionSyncTimeoutRef.current = setTimeout(() => {
// handle case when the selection is not updated correctly
// could happen on Android
if (
inputRef.current &&
inputRef.current === getActiveElement(document) &&
// The section might loose all selection, where `selectionStart === selectionEnd`
// https://github.com/mui/mui-x/pull/13652
inputRef.current.selectionStart === inputRef.current.selectionEnd &&
(inputRef.current.selectionStart !== selectionStart ||
inputRef.current.selectionEnd !== selectionEnd)
) {
Expand Down Expand Up @@ -433,6 +438,7 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {

return () => {
clearTimeout(focusTimeoutRef.current);
clearTimeout(selectionSyncTimeoutRef.current);
};
}, []); // eslint-disable-line react-hooks/exhaustive-deps

Expand Down