Skip to content
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