Skip to content

Commit d49d52d

Browse files
Merge pull request #52734 from ikevin127/ikevin127-textInputCaretSelectionPatch
PATCH: Android - Fix caret resetting to the beginning of the input
2 parents 0c99ad0 + 98335d1 commit d49d52d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
diff --git a/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java
2+
--- a/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java
3+
+++ b/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java
4+
@@ -418,6 +418,10 @@ public class ReactEditText extends AppCompatEditText {
5+
return;
6+
}
7+
8+
+ maybeSetSelection(start, end);
9+
+ }
10+
+
11+
+ private void maybeSetSelection(int start, int end) {
12+
if (start != ReactConstants.UNSET && end != ReactConstants.UNSET) {
13+
// clamp selection values for safety
14+
start = clampToTextLength(start);
15+
@@ -544,7 +548,8 @@ public class ReactEditText extends AppCompatEditText {
16+
int selectionStart = getSelectionStart();
17+
int selectionEnd = getSelectionEnd();
18+
setInputType(mStagedInputType);
19+
- setSelection(selectionStart, selectionEnd);
20+
+ // Restore the selection
21+
+ maybeSetSelection(selectionStart, selectionEnd);
22+
}
23+
}
24+
25+
@@ -1063,11 +1068,17 @@ public class ReactEditText extends AppCompatEditText {
26+
public void onAttachedToWindow() {
27+
super.onAttachedToWindow();
28+
29+
+ int selectionStart = getSelectionStart();
30+
+ int selectionEnd = getSelectionEnd();
31+
+
32+
// Used to ensure that text is selectable inside of removeClippedSubviews
33+
// See https://github.com/facebook/react-native/issues/6805 for original
34+
// fix that was ported to here.
35+
36+
super.setTextIsSelectable(true);
37+
+
38+
+ // Restore the selection since `setTextIsSelectable` changed it.
39+
+ maybeSetSelection(selectionStart, selectionEnd);
40+
41+
if (mContainsImages) {
42+
Spanned text = getText();

0 commit comments

Comments
 (0)