patch RN to remove JS hack for selectTextOnFocus
#3747
Closed
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.
Why
Note: Let's see what feedback comes from facebook/react-native#44307 before merging this (or at least before releasing to prod)
There seems to be a bug in React Native's
TextInput
code when usingselectTextOnFocus
on iOS. After a brief investigation, I discovered thatselectAll
is called on the underlyingUITextField
whenevertextInputDidBeginEditing
is fired. However, it turns out that shortly aftertextInputDidBeginEditing
is called, another method -reactFocus
- is called on the view. OncereactFocus
is called, the text selection is removed, and it appears that it never even happened at all.This patch moves the
selectAll
call to be ran after thereactFocus
call has finished. This results in the consistent behavior we expect when selecting the text.To note,
reactFocus
is the method used on the native side whenever you calltextInputRef.current?.focus()
.reactFocus
is also special to theRCTBaseTextInputView
, so we know that this method is related to the focusing of the input.As I stated in the readme for the patch, it is quite possible that the current hack fix would continue to work.
It likely works, because by the time the
onFocus
event is received and we subsequently update the selection,reactFocus
has also already been called. There very well could be situations where this is not true - note that the native code fires theonFocus
event prior toreactFocus
, so it is theoretically possible I believe for the event to be received and the JS selection to be called beforereactFocus
fires - although it's probably unlikely.Test Plan
Build for iOS with this patch, and observe that the search input filed is properly highlighted whenever the input is focused.
Here is a video of the before and after:
Screen.Recording.2024-04-28.at.5.29.57.PM.mov