From b83dabce12baf82d101807a41d2ee66ea85c07be Mon Sep 17 00:00:00 2001 From: Yevhenii Date: Wed, 11 Nov 2026 15:35:48 +0200 Subject: [PATCH] Keep a read-only Android TextInput focusable editable={false} disabled the EditText, so a hardware keyboard's Tab skipped it, TalkBack announced it as disabled, and its text could not be selected. Refuse input the way TextView does instead: the input drops its key listener while it is read-only, which leaves no input method, hardware key or cut and paste able to edit it and keeps it focusable. TextView reports TYPE_NULL for such a view and marks every EditText editable for accessibility, so the multiline, secure-text and input-type reads use the configured type, and the accessibility delegate drops the editable flag and ACTION_SET_TEXT while the input is read-only. --- .../react/views/textinput/ReactEditText.kt | 58 ++++++++++--- .../views/textinput/ReactTextInputManager.kt | 2 +- .../textinput/ReactTextInputPropertyTest.kt | 86 ++++++++++++++++++- 3 files changed, 131 insertions(+), 15 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt index bb5540b37d24..9ed0a880b746 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt @@ -53,6 +53,8 @@ import androidx.appcompat.widget.AppCompatEditText import androidx.core.graphics.withTranslation import androidx.core.util.Predicate import androidx.core.view.ViewCompat +import androidx.core.view.accessibility.AccessibilityNodeInfoCompat +import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat import com.facebook.common.logging.FLog import com.facebook.react.bridge.ReactSoftExceptionLogger.logSoftException import com.facebook.react.common.ReactConstants @@ -177,8 +179,25 @@ public open class ReactEditText public constructor(context: Context) : AppCompat return field } + /** + * Whether the text takes edits. A field that does not keeps its focus and its selection: it + * carries no key listener, which is how [android.widget.TextView] refuses typing, an input method + * and cut and paste while staying enabled. + */ + internal var isEditable: Boolean = true + set(value) { + if (field != value) { + field = value + applyKeyListener() + } + } + + // The input type the props configure. A field with no key listener reports TYPE_NULL from + // getInputType(), so the configuration is read from here. + private var configuredInputType: Int + internal val isMultiline: Boolean - get() = (inputType and InputType.TYPE_TEXT_FLAG_MULTI_LINE) != 0 + get() = (configuredInputType and InputType.TYPE_TEXT_FLAG_MULTI_LINE) != 0 // Mirrors TextView.shouldAdvanceFocusOnEnter, which advances focus on Enter for both email // address and subject even when multiline (newlines don't make sense there). @@ -192,7 +211,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat private val isSecureText: Boolean get() = - ((inputType and + ((configuredInputType and (InputType.TYPE_NUMBER_VARIATION_PASSWORD or InputType.TYPE_TEXT_VARIATION_PASSWORD)) != 0) @@ -238,6 +257,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat disableFullscreen = false listeners = null stagedInputType = inputType + configuredInputType = inputType if (keyListener == null) { keyListener = InternalKeyListener() } @@ -263,7 +283,23 @@ public open class ReactEditText public constructor(context: Context) : AppCompat this@ReactEditText.isFocusable, this@ReactEditText.importantForAccessibility, ) { + // TextView reports every EditText as editable and offers to replace its text, whatever + // its key listener says. + override fun onInitializeAccessibilityNodeInfo( + host: View, + info: AccessibilityNodeInfoCompat, + ) { + super.onInitializeAccessibilityNodeInfo(host, info) + if (!this@ReactEditText.isEditable) { + info.isEditable = false + info.removeAction(AccessibilityActionCompat.ACTION_SET_TEXT) + } + } + override fun performAccessibilityAction(host: View, action: Int, args: Bundle?): Boolean { + if (action == AccessibilityNodeInfo.ACTION_SET_TEXT && !this@ReactEditText.isEditable) { + return false + } if (action == AccessibilityNodeInfo.ACTION_CLICK) { val length = checkNotNull(text).length if (length > 0) { @@ -466,9 +502,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat // that. This method will eventually replace requestFocusInternal() private fun requestFocusProgrammatically(): Boolean { val focused = super.requestFocus(FOCUS_DOWN, null) - if (isInTouchMode && showSoftInputOnFocus) { - showSoftKeyboard() - } + showSoftKeyboardIfEditable() return focused } @@ -603,7 +637,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat } internal fun commitStagedInputType() { - if (inputType != stagedInputType) { + if (configuredInputType != stagedInputType) { val selectionStart = selectionStart val selectionEnd = selectionEnd inputType = stagedInputType @@ -616,6 +650,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat val tf = super.getTypeface() super.setInputType(type) stagedInputType = type + configuredInputType = type // Input type password defaults to monospace font, so we need to re-apply the font super.setTypeface(tf) @@ -665,7 +700,11 @@ public open class ReactEditText public constructor(context: Context) : AppCompat keyListener = InternalKeyListener() } keyListener?.inputType = type - super.setKeyListener(keyListener) + applyKeyListener() + } + + private fun applyKeyListener() { + super.setKeyListener(if (isEditable) keyListener else null) } public fun setPlaceholder(placeholder: String?) { @@ -1030,10 +1069,9 @@ public open class ReactEditText public constructor(context: Context) : AppCompat protected fun showSoftKeyboard(): Boolean = inputMethodManager.showSoftInput(this, 0) - // Mirrors the guard on requestFocusProgrammatically(), plus editability ("editable" - // prop maps to isEnabled): never summon the keyboard for a read-only input. + // Never summon the keyboard for an input that cannot be edited. private fun showSoftKeyboardIfEditable() { - if (isEnabled && isInTouchMode && showSoftInputOnFocus) { + if (isEditable && isEnabled && isInTouchMode && showSoftInputOnFocus) { showSoftKeyboard() } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt index 82e5abd361bd..ed8638f75a3c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt @@ -606,7 +606,7 @@ public open class ReactTextInputManager public constructor() : @ReactProp(name = "editable", defaultBoolean = true) public fun setEditable(view: ReactEditText, editable: Boolean) { - view.isEnabled = editable + view.isEditable = editable } @ReactProp(name = ViewProps.NUMBER_OF_LINES, defaultInt = 1) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt index 3d82c1757b96..8c904b137562 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt @@ -9,6 +9,7 @@ package com.facebook.react.views.textinput import android.graphics.Color import android.os.Build +import android.os.Bundle import android.text.InputFilter import android.text.InputFilter.AllCaps import android.text.InputType @@ -16,9 +17,12 @@ import android.text.Layout import android.text.SpannableString import android.text.Spanned import android.text.TextUtils +import android.text.method.PasswordTransformationMethod import android.util.DisplayMetrics import android.view.Gravity +import android.view.KeyEvent import android.view.View +import android.view.accessibility.AccessibilityNodeInfo import android.view.inputmethod.EditorInfo import androidx.annotation.RequiresApi import androidx.autofill.HintConstants @@ -251,19 +255,93 @@ class ReactTextInputPropertyTest { @Test fun testEditable() { manager.updateProperties(view, buildStyles()) - assertThat(view.isEnabled).isTrue + assertThat(view.onCheckIsTextEditor()).isTrue manager.updateProperties(view, buildStyles("editable", false)) - assertThat(view.isEnabled).isFalse + assertThat(view.onCheckIsTextEditor()).isFalse + assertThat(view.isEnabled).isTrue manager.updateProperties(view, buildStyles("editable", null)) - assertThat(view.isEnabled).isTrue + assertThat(view.onCheckIsTextEditor()).isTrue manager.updateProperties(view, buildStyles("editable", false)) - assertThat(view.isEnabled).isFalse + assertThat(view.onCheckIsTextEditor()).isFalse + assertThat(view.isEnabled).isTrue manager.updateProperties(view, buildStyles("editable", true)) + assertThat(view.onCheckIsTextEditor()).isTrue + } + + @Test + fun testNonEditableTakesFocus() { + manager.updateProperties(view, buildStyles("editable", false)) assertThat(view.isEnabled).isTrue + assertThat(view.requestFocus()).isTrue + } + + @Test + fun testNonEditableRefusesAccessibilitySetText() { + val setText = Bundle() + setText.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "a") + + manager.updateProperties(view, buildStyles("editable", false)) + val info = view.createAccessibilityNodeInfo() + assertThat(info.isEditable).isFalse + assertThat(info.actionList) + .doesNotContain(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_TEXT) + assertThat(view.performAccessibilityAction(AccessibilityNodeInfo.ACTION_SET_TEXT, setText)) + .isFalse + assertThat(view.text.toString()).isEmpty() + + manager.updateProperties(view, buildStyles("editable", true)) + assertThat(view.performAccessibilityAction(AccessibilityNodeInfo.ACTION_SET_TEXT, setText)) + .isTrue + assertThat(view.text.toString()).isEqualTo("a") + } + + @Test + fun testNonEditableRefusesKeys() { + manager.updateProperties(view, buildStyles("editable", false)) + view.onKeyDown(KeyEvent.KEYCODE_A, KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A)) + assertThat(view.text.toString()).isEmpty() + + manager.updateProperties(view, buildStyles("editable", true)) + view.onKeyDown(KeyEvent.KEYCODE_A, KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A)) + assertThat(view.text.toString()).isEqualTo("a") + } + + @Test + fun testNonEditableKeepsInputType() { + val emailTypeFlags = InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS or InputType.TYPE_CLASS_TEXT + + manager.updateProperties(view, buildStyles("keyboardType", "email-address", "editable", false)) + manager.updateProperties(view, buildStyles("editable", true)) + assertThat(view.inputType and generalKeyboardTypeFlags).isEqualTo(emailTypeFlags) + } + + @Test + fun testNonEditableTakesKeyboardTypeChanges() { + val numericTypeFlags = + (InputType.TYPE_CLASS_NUMBER or + InputType.TYPE_NUMBER_FLAG_DECIMAL or + InputType.TYPE_NUMBER_FLAG_SIGNED) + + manager.updateProperties(view, buildStyles("editable", false)) + manager.updateProperties(view, buildStyles("keyboardType", "numeric")) + manager.updateProperties(view, buildStyles("editable", true)) + assertThat(view.inputType and generalKeyboardTypeFlags).isEqualTo(numericTypeFlags) + } + + @Test + fun testNonEditableKeepsMultilineAndSecureText() { + manager.updateProperties(view, buildStyles("multiline", true, "editable", false)) + assertThat(view.isMultiline).isTrue + + manager.updateProperties(view, buildStyles("multiline", false, "secureTextEntry", true)) + assertThat(view.transformationMethod).isInstanceOf(PasswordTransformationMethod::class.java) + + manager.updateProperties(view, buildStyles("secureTextEntry", false)) + assertThat(view.transformationMethod).isNotInstanceOf(PasswordTransformationMethod::class.java) } @Test