Skip to content

Commit 2ad3bb2

Browse files
uqmessiasfacebook-github-bot
authored andcommitted
fix(android): Normalize start and end args (#24938)
Summary: Fixes #18579 Normalize `start` and `end` arguments when `onSelectionChange` is dispatched on Android. It just applies a [fix](#18579 (comment)) sent by TheSavior (Thanks, by the way 😄) ## Changelog [Android] [Fixed] - fix(android): Normalize start and end args Pull Request resolved: #24938 Differential Revision: D15412005 Pulled By: cpojer fbshipit-source-id: bb132313cfb8877a682f3865a5f9e48d45ac20ac
1 parent d4ff5ed commit 2ad3bb2

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -963,16 +963,22 @@ public void onSelectionChanged(int start, int end) {
963963
// Android will call us back for both the SELECTION_START span and SELECTION_END span in text
964964
// To prevent double calling back into js we cache the result of the previous call and only
965965
// forward it on if we have new values
966-
if (mPreviousSelectionStart != start || mPreviousSelectionEnd != end) {
966+
967+
// Apparently Android might call this with an end value that is less than the start value
968+
// Lets normalize them. See https://github.com/facebook/react-native/issues/18579
969+
int realStart = Math.min(start, end);
970+
int realEnd = Math.max(start, end);
971+
972+
if (mPreviousSelectionStart != realStart || mPreviousSelectionEnd != realEnd) {
967973
mEventDispatcher.dispatchEvent(
968974
new ReactTextInputSelectionEvent(
969975
mReactEditText.getId(),
970-
start,
971-
end
976+
realStart,
977+
realEnd
972978
));
973979

974-
mPreviousSelectionStart = start;
975-
mPreviousSelectionEnd = end;
980+
mPreviousSelectionStart = realStart;
981+
mPreviousSelectionEnd = realEnd;
976982
}
977983
}
978984
}

0 commit comments

Comments
 (0)