-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select text inside TextInput only once from onLayout
#47902
base: main
Are you sure you want to change the base?
Select text inside TextInput only once from onLayout
#47902
Conversation
@dmytrorykun has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@@ -134,6 +134,7 @@ public class ReactEditText extends AppCompatEditText { | |||
private boolean mContextMenuHidden = false; | |||
private boolean mDidAttachToWindow = false; | |||
private boolean mSelectTextOnFocus = false; | |||
private boolean hasSelectedTextOnFocus = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please follow existing conventions
private boolean hasSelectedTextOnFocus = false; | |
private boolean mDidSelectTextOnFocus = false; |
// Explicitly call this method to select text when layout is drawn | ||
selectAll(); | ||
// Prevent text on being selected for next layout pass | ||
mSelectTextOnFocus = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there other cases in Reanimated where props are reapplied multiple times? There are other places where that will cause problems, e.g. for ScrollView setting the contentOffset
prop.
@@ -630,6 +631,7 @@ public void maybeUpdateTypeface() { | |||
// VisibleForTesting from {@link TextInputEventsTestCase}. | |||
public void requestFocusFromJS() { | |||
requestFocusInternal(); | |||
hasSelectedTextOnFocus = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we avoid the onLayout focus here?
Summary:
The behavior used for
selectTextOnFocus
changed in #45004. The new behavior relies on the field being set as a prop to keep track whether the text should be focused inonLayout
or not. This causes a problem whenautofocus
is not used - when the layout of the text input changes, the text will get selected even if the focus doesn't change.This also presents itself when the prop is set multiple times. I don't think this can happen in bare React Native, but third-party libraries (such as Reanimated) can cause this path to run:
react-native/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp
Lines 56 to 63 in d424bb9
by cloning the node before it's committed. This behavior (cloning in Reanimated) is not ideal and a replacement is in the works, but in the meantime, it's causing a similar problem where the text will be selected on every layout change.
Changelog:
[ANDROID] [FIXED] - Fixed text being selected multiple times when
selectTextOnFocus
is used on aTextInput
Test Plan:
Code used in the videos
The behavior from the videos doesn't require Reanimated to be reproducible.
Screen.Recording.2024-11-22.at.10.33.43.mov
Screen.Recording.2024-11-22.at.10.33.22.mov
Screen.Recording.2024-11-22.at.10.15.33.mov
Screen.Recording.2024-11-22.at.10.18.29.mov