Skip to content

fix: select text on auto focus for TextInput#45004

Closed
kunalchavhan wants to merge 4 commits intofacebook:mainfrom
kunalchavhan:fix/textinput-selecttext-autofocus
Closed

fix: select text on auto focus for TextInput#45004
kunalchavhan wants to merge 4 commits intofacebook:mainfrom
kunalchavhan:fix/textinput-selecttext-autofocus

Conversation

@kunalchavhan
Copy link
Contributor

Summary:

Fixes: #43413

This pull request addresses an issue on Android where the text selection was not working when both selectTextOnFocus and autoFocus were set to true on TextInput.
ReactTextInputManager was calling setSelectAllOnFocus on ReactEditText before its onLayout is called causing text selection to not work on auto focus.

Changes Made

  • Added logic to wait for the ReactEditText view's layout to be drawn before attempting to select the text.
    On the first layout pass, the code now explicitly calls selectAll() to select the text.
  • Implemented a check to ensure selectAll() is only called during the first layout pass, avoiding unnecessary calls on subsequent layout passes.

Impact
This change ensures that text selection is properly triggered when selectTextOnFocus and autoFocus are both enabled, improving the user experience and making text input behavior consistent and reliable.

Changelog:

[ANDROID] [FIXED]: fixed select text on auto focus for TextInput

Test Plan:

Before After
before after

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Jun 17, 2024
@analysis-bot
Copy link

analysis-bot commented Jun 17, 2024

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 20,366,365 -901,491
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 23,562,945 -902,029
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: 47261bf
Branch: main

@kunalchavhan
Copy link
Contributor Author

@cortinico Can you please help in reviewing this PR? Thank you for your time.

Copy link
Contributor

@cortinico cortinico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kunalchavhan thanks for sending this over

Can I ask you to add a sample for RN-Tester (the one you're showing in your screenshots)

@kunalchavhan
Copy link
Contributor Author

@kunalchavhan thanks for sending this over

Can I ask you to add a sample for RN-Tester (the one you're showing in your screenshots)

Sure, will add it.

@kunalchavhan
Copy link
Contributor Author

@cortinico I have added sample in RN-Tester as you suggested. Took the liberty to combine autoFocus and selectTextOnFocus in one sample instead creating separate one to avoid focus conflict hope this is fine.

module.exports = ([
{
title: 'Auto-focus',
title: 'Auto-focus & select text on focus',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why haven't you just added selectTextOnFocus here but instead you also added onChangeText?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To show selected text I have added value prop with initial value set to Hello World. Earlier the auto-focus sample allowed user input. After adding value prop user input was getting overridden, I have added onChangeText to change value to user's input.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cortinico do you want me to remove onChangeText?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, thanks for the clarification 👍

@facebook-github-bot
Copy link
Contributor

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@kunalchavhan
Copy link
Contributor Author

@cortinico One of the checks failed can't get more details on it. Please let me know if anythings needs to be done from my side.

@cortinico
Copy link
Contributor

@cortinico One of the checks failed can't get more details on it. Please let me know if anythings needs to be done from my side.

I'll take care of it. I'm discussing with one of my colleague if we want to merge this or not, I'll get back to you if I need further input

Comment on lines +242 to +247
if (mSelectTextOnFocus) {
// Explicitly call this method to select text when layout is drawn
selectAll();
// Prevent text on being selected for next layout pass
mSelectTextOnFocus = false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we only select text when the text is actually focused?

Making the text selected even when not focused doesn't seem to be right by this propname.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your input @javache. I will add a check isFocused along with mSelectTextOnFocus.

@facebook-github-bot
Copy link
Contributor

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@cortinico merged this pull request in 18d6028.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Jul 12, 2024
@github-actions
Copy link

This pull request was successfully merged by @kunalchavhan in 18d6028.

When will my fix make it into a release? | How to file a pick request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Needs: Author Feedback Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

selectOnFocus does not work with autoFocus

5 participants