fix: report correct selection in Android onChange event during autofill#57460
Open
nsbarsukov wants to merge 1 commit into
Open
fix: report correct selection in Android onChange event during autofill#57460nsbarsukov wants to merge 1 commit into
nsbarsukov wants to merge 1 commit into
Conversation
On Android, filling a TextInput via platform autofill emitted an
onChange event whose nativeEvent.selection was {start: 0, end: 0}
regardless of the inserted text length.
TextView.autofill() first calls setText(), which notifies the
TextWatchers (where ReactTextInputTextWatcher reads the selection for
the topChange event) while the cursor still sits at its pre-autofill
position, and only afterwards moves the cursor to the end of the new
text. Track the in-progress autofill on ReactEditText and report the
selection at the end of the new text, matching where the framework
places the cursor immediately after.
Fixes react#57458
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
On Android, filling a
TextInputvia platform autofill emits anonChangeevent whosenativeEvent.selectionis{start: 0, end: 0}regardless of the inserted text length, while the same value inserted by pasting (and both paste and autofill on iOS) correctly reports the selection at the end of the inserted text.The root cause is in the Android framework's autofill flow:
TextView.autofill()first callssetText(), which notifies theTextWatchers — whereReactTextInputTextWatcherreads the selection for thetopChangeevent — while the cursor still sits at its pre-autofill position, and only afterwards moves the cursor to the end of the new text. Paste is unaffected because clipboard insertion goes throughEditable.replace(), which repositions the cursor before the watchers run.This change tracks the in-progress autofill on
ReactEditText(via anautofill()override setting anisBeingAutofilledflag) and, while it is set, reports the selection at the end of the new text — matching where the framework places the cursor immediately after.Fixes #57458
Changelog:
[ANDROID] [FIXED] - TextInput
onChangeevent reports staleselection({start: 0, end: 0}) when text is inserted via platform autofillTest Plan:
Added
ReactTextInputTextWatcherTest(Robolectric), which drives the real frameworkView.autofill()path and asserts on the dispatchedReactTextChangedEventpayload:testAutofillReportsSelectionAtEndOfText— autofills+15551234567and asserts the event carriestext: "+15551234567"andselection: {start: 12, end: 12}. Without the fix this test fails withselection: {start: 0, end: 0}, reproducing the exact bug from the issue.testTextInsertedAtCursorReportsSelectionAfterInsertedText— regression-guards the paste/typing path (insertion at the cursor viaEditable.insert()), which was already correct.