validate input before calling setSelection to restore selection range (#47577)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47577

Changelog: [Android][Fixed] ensure setSelection in onAttachedToWindow is within text range

Reviewed By: javache

Differential Revision: D65824906

fbshipit-source-id: 3dc7d27bf4f9a10762f11fa4a0bcae8af13c7db7
This commit is contained in:
Zeya Peng 2024-11-13 11:38:02 -08:00 committed by Facebook GitHub Bot
parent 05367de5e6
commit 08759121cd

View File

@ -408,6 +408,10 @@ public class ReactEditText extends AppCompatEditText {
return;
}
maybeSetSelection(start, end);
}
private void maybeSetSelection(int start, int end) {
if (start != ReactConstants.UNSET && end != ReactConstants.UNSET) {
// clamp selection values for safety
start = clampToTextLength(start);
@ -534,7 +538,8 @@ public class ReactEditText extends AppCompatEditText {
int selectionStart = getSelectionStart();
int selectionEnd = getSelectionEnd();
setInputType(mStagedInputType);
setSelection(selectionStart, selectionEnd);
// Restore the selection
maybeSetSelection(selectionStart, selectionEnd);
}
}
@ -1071,7 +1076,7 @@ public class ReactEditText extends AppCompatEditText {
super.setTextIsSelectable(true);
// Restore the selection since `setTextIsSelectable` changed it.
setSelection(selectionStart, selectionEnd);
maybeSetSelection(selectionStart, selectionEnd);
if (mContainsImages) {
Spanned text = getText();