Skip to content

Commit e679cc3

Browse files
joevilchescortinico
authored andcommitted
Remove useEditTextStockAndroidBehavior and resolve old android blur issue (facebook#51338)
Summary: Pull Request resolved: facebook#51338 There was some problems removing this feature flag earlier in that, on older android versions, we would try to focus the top most text input whenever any other text input would try to blur. This was ultimately and issue with how Android implements `clearFocus`. To fix this, lets block the focusability of all views while we clear the focus, then re-enable. Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D74760594 fbshipit-source-id: 2811c08ad6ed0855da0a4d7fca89fb08f84905c2
1 parent 1cc4601 commit e679cc3

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6565,7 +6565,6 @@ public class com/facebook/react/views/textinput/ReactEditText : androidx/appcomp
65656565
public fun addTextChangedListener (Landroid/text/TextWatcher;)V
65666566
protected final fun applyTextAttributes ()V
65676567
public final fun canUpdateWithEventCount (I)Z
6568-
public fun clearFocus ()V
65696568
protected final fun finalize ()V
65706569
public final fun getBorderColor (I)I
65716570
protected final fun getContainsImages ()Z

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import android.view.Menu
3535
import android.view.MenuItem
3636
import android.view.MotionEvent
3737
import android.view.View
38+
import android.view.ViewGroup
3839
import android.view.accessibility.AccessibilityNodeInfo
3940
import android.view.inputmethod.EditorInfo
4041
import android.view.inputmethod.InputConnection
@@ -359,11 +360,26 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
359360
super.onTextContextMenuItem(
360361
if (id == android.R.id.paste) android.R.id.pasteAsPlainText else id)
361362

362-
override fun clearFocus() {
363-
super.clearFocus()
363+
internal fun clearFocusAndMaybeRefocus() {
364+
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P || !isInTouchMode) {
365+
super.clearFocus()
366+
} else {
367+
// Avoid refocusing to a new view on old versions of Android by default
368+
// by preventing `requestFocus()` on the rootView from moving focus to any child.
369+
// https://cs.android.com/android/_/android/platform/frameworks/base/+/bdc66cb5a0ef513f4306edf9156cc978b08e06e4
370+
val rootViewGroup = rootView as ViewGroup
371+
val oldDescendantFocusability = rootViewGroup.descendantFocusability
372+
rootViewGroup.descendantFocusability = ViewGroup.FOCUS_BLOCK_DESCENDANTS
373+
super.clearFocus()
374+
rootViewGroup.descendantFocusability = oldDescendantFocusability
375+
}
364376
hideSoftKeyboard()
365377
}
366378

379+
internal fun clearFocusFromJS() {
380+
clearFocusAndMaybeRefocus()
381+
}
382+
367383
// For cases like autoFocus, or ref.focus() where we request focus programmatically and not
368384
// through
369385
// interacting with the EditText directly (like clicking on it). We cannot use stock
@@ -598,10 +614,6 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
598614
requestFocusProgrammatically()
599615
}
600616

601-
internal fun clearFocusFromJS() {
602-
clearFocus()
603-
}
604-
605617
public fun incrementAndGetEventCounter(): Int = ++nativeEventCount
606618

607619
public fun maybeSetTextFromJS(reactTextUpdate: ReactTextUpdate) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ public open class ReactTextInputManager public constructor() :
926926
}
927927

928928
if (shouldBlur) {
929-
editText.clearFocus()
929+
editText.clearFocusAndMaybeRefocus()
930930
}
931931

932932
// Prevent default behavior except when we want it to insert a newline.

0 commit comments

Comments
 (0)