Skip to content
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

[TextInput]: Fixed duplicated letters being added when changing text through onChangeText #27757

Closed
wants to merge 1 commit into from

Conversation

safaiyeh
Copy link
Contributor

@safaiyeh safaiyeh commented Jan 14, 2020

Summary

Fix for #11068.
getText().replace caused a bug where letters would be duplicated when the text was being set through onChangeText:

<TextInput ref={(el) => this.input = el} onChangeText={(val) => { this.input.setNativeProps({text: val.toUpperCase()}); }}/>

Debugged and tried a bunch of different ways to solve it explained here: #11068 (comment)

Changelog

[Android] [Fixed] - Fixed duplicated letters being added when changing text through onChangeText

Test Plan

Added this to RNTester:

<TextInput ref={(el) => this.input = el} onChangeText={(val) => { this.input.setNativeProps({text: val.toUpperCase()}); }}/>

Verified it works properly and there were no regressions in the other examples.

Note: There is some performance issues when typing for a long time, probably a lag between clearing and appending. Would love to see if someone has suggestions in handling that.

Edit: Seems like the perf is a known issue #20119

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 14, 2020
@react-native-bot react-native-bot added Component: TextInput Related to the TextInput component. Bug Platform: Android Android applications. labels Jan 14, 2020
@JoshuaGross
Copy link
Contributor

Does this retain selections when typing in the middle of text?

Could you add some videos?

@safaiyeh
Copy link
Contributor Author

safaiyeh commented Jan 20, 2020

That is true @JoshuaGross. Append would always put the cursor to the end. Do you have any ideas I could try for this? I debugged this for a while and can't find a solid answer.

Maybe a diff check and find the appropriate place to put the cursor?

@martianatwork
Copy link

@safaiyeh @JoshuaGross I’m not expert at this but here are some suggestions, can we introduce a function that transforms the text at java instead of letting keyboard transform text?

Ex. We have autocapitalise option then on changetext we can apply the transformation and forward the updated text to view. Currently user can type in lowercase even if the uppercase is set.

Or we create a transformation prop which will update the text at JavaScript and the complete state will be managed at JavaScript.

I’m not sure about the performance impact.

@safaiyeh
Copy link
Contributor Author

@martianatwork I like those solutions. I will give them a shot!

One flaw to having options for on change is custom text transforms. I like the second solution because it can cover both.

@vonovak
Copy link
Contributor

vonovak commented Feb 9, 2020

textinput is very tricky. We wrote our own component for rich text input, and just want to add that if this is to be merged, it should be very thoroughly tested. I think some issues will only get resolved once we have sync communication between JS and native (fabric stable).

@safaiyeh
Copy link
Contributor Author

safaiyeh commented Feb 9, 2020

Is that component open sourced? @vonovak

Ive working on a text input heavy app and the performance kills UX at some point.

@vonovak
Copy link
Contributor

vonovak commented Feb 9, 2020

@safaiyeh no, it's not open sourced, and it's highly unlikely that it would be...

@@ -503,7 +503,8 @@ public void maybeSetText(ReactTextUpdate reactTextUpdate) {
// When we update text, we trigger onChangeText code that will
// try to update state if the wrapper is available. Temporarily disable
// to prevent an infinite loop.
getText().replace(0, length(), spannableStringBuilder);
getText().clear();
append(spannableStringBuilder);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Pull request safaiyeh#2

Integer currentPosition = getSelectionStart();
getText().clear();
append(spannableStringBuilder);
setSelection(currentPosition);

setSelection sets the cursor position to the previous one after append operation.

This is some quick testing of the functionality with the react-native master branch. Let me know your feedback, I can publish a separate Pull Request if you are not available otherwise I can futher help you to review your pull request and test the changes.

BEFORE AFTER

Thanks a lot
Fabrizio Bertoglio

This comment was marked as outdated.

Copy link
Contributor

Choose a reason for hiding this comment

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

@SrBrahma
Copy link

SrBrahma commented Sep 4, 2020

I have the same issue when using toUpperCase method on the input string.

@sdandois
Copy link

How can I apply this as a patch?

@lunaleaps
Copy link
Contributor

@safaiyeh, @fabriziobertoglio1987 Is this PR still relevant with #29070 open? They seem like they both fix issue #11068.

If so, can I close this PR?

@safaiyeh
Copy link
Contributor Author

Yup that PR takes care of it. I’ll close this @lunaleaps

@safaiyeh safaiyeh closed this Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Component: TextInput Related to the TextInput component. Platform: Android Android applications.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants