fix(android): update text natively only if needed (fixes selection menu missing) #36663
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
Problem
There exists a bug where on android when having a controlled
TextInput
using theselection
prop the selection menu would not show up (the first TextInput is uncontrolled and there you can see its working):Screen.Recording.2023-03-27.at.18.48.27.mov
There has also been an issue, which was closed as stale:
The reason for this behaviour is the following:
lastNativeSelectionState
null
lastNativeSelectionState
, causing the component to re-render.maybeSetText
call (e.g. updating the text of the native view)maybeSetText
doesn't check if the text is the same, and updates the texts nativelySolution
We don't want to update the text natively if it hasn't changed. There is already a check present in
maybeSetText
, but it only checks for text changes when the TextInput is a secure one (e.g. a password input).I propose that we always check if the text has changed, and only update the text natively if it really did.
These changes can be seen in this PR.
With these changes you can see that in the tester app the text selection also works for controlled inputs:
Screen.Recording.2023-03-27.at.19.16.49.mov
Changelog
[ANDROID] [FIXED] - Selection menu missing when TextInput uses controlled
selection
propTest Plan
TextInputEventsTestCase
, however I can't find it anywhere in the code. I would like to run those tests and confirm everything is still working as expectedTextInput
in the rn tester app