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

Allow filtering characters in onChangeText for TextInput #19087

Closed

Conversation

rplankenhorn
Copy link

Fixes #18874

Test Plan

Create a sample app that performs validation in the onChangeText callback and modifies the characters. (e.g. filters out emojis).

Release Notes

[IOS] [BUGFIX] [TextInput] - Fixed issue with not being able to filter characters in onChangeText.

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@react-native-bot react-native-bot added 📋Release Notes Component: TextInput Related to the TextInput component. labels May 1, 2018
@rplankenhorn rplankenhorn force-pushed the filterCharactersFix branch from 4a6346e to bae1f02 Compare May 1, 2018 19:14
@rplankenhorn rplankenhorn changed the base branch from 0.55-stable to master May 1, 2018 19:14
@rplankenhorn rplankenhorn force-pushed the filterCharactersFix branch from bae1f02 to bfe0554 Compare May 1, 2018 19:15
@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 May 1, 2018
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@tangkunyin
Copy link

@rplankenhorn It doesn't work for me. But this can help: #18456

@rplankenhorn
Copy link
Author

Hi @tangkunyin. I already commented on that change and it actually breaks a few things with text input so I don’t think it should be merged. I’ll work on creating a sample app so you can view the issue and my fix.

@nol13
Copy link

nol13 commented May 29, 2018

This patch and #18456 both seem to work for me to get react-native-tag-input working reliably again.

With this patch I need to also apply #18627 along with it to fix the faulty backspace emitting issue, where #18456 seems to take care of that issue as well.

Edit: I dunno maybe something was cached, need #18627 either way now, current fork using this + hamaron's backspace fix.

@facebook-github-bot
Copy link
Contributor

@rplankenhorn I tried to find reviewers for this pull request and wanted to ping them to take another look. However, based on the blame information for the files in this pull request I couldn't find any reviewers. This sometimes happens when the files in the pull request are new or don't exist on master anymore. Is this pull request still relevant? If yes could you please rebase? In case you know who has context on this code feel free to mention them in a comment (one person is fine). Thanks for reading and hope you will continue contributing to the project.

@Ashoat
Copy link
Contributor

Ashoat commented Jun 8, 2018

Does this PR achieve the same goals as #18456? Can somebody explain the differences?

@lucasbento
Copy link

Applied this patch to react-native 0.54.2 and works really well, thank you @rplankenhorn!

@pvagare
Copy link

pvagare commented Jul 9, 2018

Applied patch to react-native 0.54.2 and works really well, @rplankenhorn .

Please merge this pull request. So will use the same.

Thanks !

@lzxb
Copy link

lzxb commented Jul 13, 2018

I also have the same problem, the version is 0.55.4

@kelset kelset added the Missing Test Plan This PR appears to be missing a test plan. label Jul 24, 2018
@kelset kelset removed the request for review from mhorowitz July 24, 2018 11:26
@react-native-bot react-native-bot added ✅Test Plan and removed Missing Test Plan This PR appears to be missing a test plan. labels Jul 24, 2018
@Ashoat
Copy link
Contributor

Ashoat commented Jul 30, 2018

Does 892212b solve this problem? The initial issue was constrained to Unicode emoticons yes?

@wsun
Copy link
Contributor

wsun commented Aug 2, 2018

This is a nice patch, thanks @rplankenhorn!

After applying, I observed that I could no longer enter strings into uncontrolled password TextInput fields, so I modified the patch to ignore those situations.

@hramos
Copy link
Contributor

hramos commented Aug 2, 2018

What Ashoat said. Do we still need this patch now that 892212b has landed?

@nol13
Copy link

nol13 commented Aug 2, 2018

Don't think 892212b fixes issue.

Or at least, after replacing the patched RCTBaseTextInputShadowView.m with RCTBaseTextInputShadowView.m from master in my 55.3 project, the issue that this patch solves reappears. Haven't tested on 56.

sidnair added a commit to taptapsend/react-native that referenced this pull request Aug 3, 2018
The value in the native text input would sometimes be respected over the
value passed in. This was specifically an issue for text normalization:
for example, if we wanted to only allow digits in a text field and a
user enters "123a", the attributedText passed in from JS is "123"
(since it gets normalized), _previousAttributedText is "123" (since
that was the last value updated from the JS, but
baseTextInputView.attributedText is "123a" (since that gets updated by
the native event). In that scenario, the text in the input never gets
normalized.

There's a comment warning about not notifying the view of a text change
that isn't a real update, but as far as I can tell, this is a non-issue
here because RCTBaseTextInputView does its own checks to see if values
have changed.

This implementation was inspired by PR facebook#19087, but simplifies by
removing `_previousAttributedText` entirely and avoids breaking
uncontrolled text inputs.
@misheki
Copy link

misheki commented Aug 14, 2018

I applied this patch to 0.55.4 and is working perfectly so far! Thanks @rplankenhorn! I owe you coffee.

@lucasbento
Copy link

@shergin, @hramos: would any of you have time to take a look at this PR?

@HelmerBarcos
Copy link

I have tested this patch on 0.56 and it works nice but the filtered characters are showed for a second. This should be handled as on Android, where undesirable characters are not showed.

@RWOverdijk
Copy link

+1. Using a patch is literally a patch :p

sidnair added a commit to taptapsend/react-native that referenced this pull request Aug 21, 2018
The value in the native text input would sometimes be respected over the
value passed in. This was specifically an issue for text normalization:
for example, if we wanted to only allow digits in a text field and a
user enters "123a", the attributedText passed in from JS is "123"
(since it gets normalized), _previousAttributedText is "123" (since
that was the last value updated from the JS, but
baseTextInputView.attributedText is "123a" (since that gets updated by
the native event). In that scenario, the text in the input never gets
normalized.

There's a comment warning about not notifying the view of a text change
that isn't a real update, but as far as I can tell, this is a non-issue
here because RCTBaseTextInputView does its own checks to see if values
have changed.

This implementation was inspired by PR facebook#19087, but simplifies by
removing `_previousAttributedText` entirely and avoids breaking
uncontrolled text inputs.
@hramos
Copy link
Contributor

hramos commented Sep 12, 2018

In order to move this PR forward, it would be useful to have someone review if it fixes the issue when this patch is applied on top of the changes we have on master right now. We've landed some fixes to related issues since this PR was opened, and I want to make sure the PR is still valid before proceeding.

Copy link
Contributor

@hramos hramos left a comment

Choose a reason for hiding this comment

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

Rebase on top of master and make sure tests do not regress. Let us know if the fix is still valid after rebasing on top of master.

@nol13
Copy link

nol13 commented Sep 12, 2018

For my use case, issue appears to be resolved in 0.57!

@vovkasm
Copy link
Contributor

vovkasm commented Sep 12, 2018

@hramos I can confirm that filtering issue already resolved in RN 0.57.0
Tested with this simple application (branch rn-0.57): https://github.com/vovkasm/react-native-textinput-bug/tree/rn-0.57

@hramos hramos closed this Sep 13, 2018
dan-f pushed a commit to taptapsend/react-native that referenced this pull request Jul 1, 2019
The value in the native text input would sometimes be respected over the
value passed in. This was specifically an issue for text normalization:
for example, if we wanted to only allow digits in a text field and a
user enters "123a", the attributedText passed in from JS is "123"
(since it gets normalized), _previousAttributedText is "123" (since
that was the last value updated from the JS, but
baseTextInputView.attributedText is "123a" (since that gets updated by
the native event). In that scenario, the text in the input never gets
normalized.

There's a comment warning about not notifying the view of a text change
that isn't a real update, but as far as I can tell, this is a non-issue
here because RCTBaseTextInputView does its own checks to see if values
have changed.

This implementation was inspired by PR facebook#19087, but simplifies by
removing `_previousAttributedText` entirely and avoids breaking
uncontrolled text inputs.
dan-f pushed a commit to taptapsend/react-native that referenced this pull request Jul 10, 2019
The value in the native text input would sometimes be respected over the
value passed in. This was specifically an issue for text normalization:
for example, if we wanted to only allow digits in a text field and a
user enters "123a", the attributedText passed in from JS is "123"
(since it gets normalized), _previousAttributedText is "123" (since
that was the last value updated from the JS, but
baseTextInputView.attributedText is "123a" (since that gets updated by
the native event). In that scenario, the text in the input never gets
normalized.

There's a comment warning about not notifying the view of a text change
that isn't a real update, but as far as I can tell, this is a non-issue
here because RCTBaseTextInputView does its own checks to see if values
have changed.

This implementation was inspired by PR facebook#19087, but simplifies by
removing `_previousAttributedText` entirely and avoids breaking
uncontrolled text inputs.
benanderman pushed a commit to taptapsend/react-native that referenced this pull request Apr 10, 2020
The value in the native text input would sometimes be respected over the
value passed in. This was specifically an issue for text normalization:
for example, if we wanted to only allow digits in a text field and a
user enters "123a", the attributedText passed in from JS is "123"
(since it gets normalized), _previousAttributedText is "123" (since
that was the last value updated from the JS, but
baseTextInputView.attributedText is "123a" (since that gets updated by
the native event). In that scenario, the text in the input never gets
normalized.

There's a comment warning about not notifying the view of a text change
that isn't a real update, but as far as I can tell, this is a non-issue
here because RCTBaseTextInputView does its own checks to see if values
have changed.

This implementation was inspired by PR facebook#19087, but simplifies by
removing `_previousAttributedText` entirely and avoids breaking
uncontrolled text inputs.
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. Component: TextInput Related to the TextInput component.
Projects
None yet
Development

Successfully merging this pull request may close these issues.