From d21c401620aea17ad2ab0fd7d71dadaa37b60143 Mon Sep 17 00:00:00 2001 From: Markus Kobisch Date: Mon, 8 Nov 2021 15:38:05 +0100 Subject: [PATCH] [fix] Avoid edge case where focus jumps between TextInputs Close #2163 Fix #2162 --- .../react-native-web/src/exports/TextInput/index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/react-native-web/src/exports/TextInput/index.js b/packages/react-native-web/src/exports/TextInput/index.js index a37dd8ca..2145ecd4 100644 --- a/packages/react-native-web/src/exports/TextInput/index.js +++ b/packages/react-native-web/src/exports/TextInput/index.js @@ -83,6 +83,8 @@ function isEventComposing(nativeEvent) { return nativeEvent.isComposing || nativeEvent.keyCode === 229; } +let focusTimeout: ?TimeoutID = null; + const TextInput: React.AbstractComponent< TextInputProps, HTMLElement & PlatformMethods @@ -243,8 +245,13 @@ const TextInput: React.AbstractComponent< } if (selectTextOnFocus) { // Safari requires selection to occur in a setTimeout - setTimeout(() => { - hostNode.select(); + if (focusTimeout != null) { + clearTimeout(focusTimeout); + } + focusTimeout = setTimeout(() => { + if (hostNode != null) { + hostNode.select(); + } }, 0); } }