From 73ed8e9e9e3455eab8e5cd7d8ccae8fd5739393c Mon Sep 17 00:00:00 2001 From: Lukas Date: Fri, 28 Jun 2024 17:23:02 +0300 Subject: [PATCH] Ensure the selection is synced --- .../internals/hooks/useField/useFieldV6TextField.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useFieldV6TextField.ts b/packages/x-date-pickers/src/internals/hooks/useField/useFieldV6TextField.ts index 0b8ad1427e0c..9dd3d684e0d6 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useFieldV6TextField.ts +++ b/packages/x-date-pickers/src/internals/hooks/useField/useFieldV6TextField.ts @@ -162,6 +162,18 @@ export const useFieldV6TextField: UseFieldTextField = (params) => { inputRef.current.setSelectionRange(selectionStart, selectionEnd); } } + setTimeout(() => { + // handle case when the selection is not updated correctly + // could happen on Android + if ( + inputRef.current && + inputRef.current === getActiveElement(document) && + (inputRef.current.selectionStart !== selectionStart || + inputRef.current.selectionEnd !== selectionEnd) + ) { + interactions.syncSelectionToDOM(); + } + }); } // Even reading this variable seems to do the trick, but also setting it just to make use of it