Skip to content

Commit eb81d1e

Browse files
committed
fix(android): show soft keyboard on text selection
1 parent 7a5ab34 commit eb81d1e

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
130130
private var keyListener: InternalKeyListener? = null
131131
private var detectScrollMovement = false
132132
private var onKeyPress = false
133+
private var selectionWasCollapsed = true
133134
private val textAttributes: TextAttributes
134135
private var typefaceDirty = false
135136
private var fontFamily: String? = null
@@ -492,13 +493,29 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
492493
if (selectionWatcher != null && hasFocus()) {
493494
selectionWatcher?.onSelectionChanged(selStart, selEnd)
494495
}
496+
maybeShowSoftKeyboardForSelection(selStart, selEnd)
495497
}
496498

497499
override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
498500
super.onFocusChanged(focused, direction, previouslyFocusedRect)
499501
if (focused && selectionWatcher != null) {
500502
selectionWatcher?.onSelectionChanged(selectionStart, selectionEnd)
501503
}
504+
if (focused) {
505+
maybeShowSoftKeyboardForSelection(selectionStart, selectionEnd)
506+
}
507+
}
508+
509+
private fun maybeShowSoftKeyboardForSelection(selectionStart: Int, selectionEnd: Int) {
510+
if (!hasFocus() || !showSoftInputOnFocus) {
511+
return
512+
}
513+
514+
val selectionIsCollapsed = selectionStart == selectionEnd
515+
if (!selectionIsCollapsed && selectionWasCollapsed) {
516+
showSoftKeyboard()
517+
}
518+
selectionWasCollapsed = selectionIsCollapsed
502519
}
503520

504521
internal fun setSelectionWatcher(selectionWatcher: SelectionWatcher?) {

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
package com.facebook.react.views.textinput
1313

14+
import android.content.Context
1415
import android.graphics.Color
1516
import android.os.Build
1617
import android.text.InputFilter
@@ -46,6 +47,15 @@ import org.robolectric.RuntimeEnvironment
4647
@RunWith(RobolectricTestRunner::class)
4748
class ReactTextInputPropertyTest {
4849

50+
private class TestReactEditText(context: Context) : ReactEditText(context) {
51+
var showSoftKeyboardCallCount = 0
52+
53+
override fun showSoftKeyboard(): Boolean {
54+
showSoftKeyboardCallCount++
55+
return true
56+
}
57+
}
58+
4959
private lateinit var context: BridgeReactContext
5060
private lateinit var catalystInstanceMock: CatalystInstance
5161
private lateinit var themedContext: ThemedReactContext
@@ -74,6 +84,17 @@ class ReactTextInputPropertyTest {
7484
view = manager.createViewInstance(themedContext)
7585
}
7686

87+
@Test
88+
fun testShowsSoftKeyboardWhenSelectionStartsWhileFocused() {
89+
val textInput = TestReactEditText(themedContext)
90+
textInput.setText("hello")
91+
textInput.onFocusChanged(true, View.FOCUS_DOWN, null)
92+
93+
textInput.setSelection(1, 3)
94+
95+
assertThat(textInput.showSoftKeyboardCallCount).isEqualTo(1)
96+
}
97+
7798
@Test
7899
fun testAutoCorrect() {
79100
manager.updateProperties(view, buildStyles())

0 commit comments

Comments
 (0)