react-native: TextInput onChangeText is called with empty text if maxLength is exceeded on iOS
Description
If the max length of a controlled TextInput
is exceeded on iOS onChangeText
will get called with an empty string. We noticed this issue when our users added an emoji as the last character in a TextInput
. We only see the issue on iOS, on Android the the emoji can not be added. Since emojis are counted as two chars I’m guessing the emoji is causing the text to have length maxLength+1 and that is causing issues on iOS.
Using an uncontrolled TextInput
the emoji is replace with the last character in the screenshot below but keeps it’s value.
React Native version:
0.61.4
Steps To Reproduce
Provide a detailed list of steps that reproduce the issue.
- Create a controlled TextInput with a maxLength
- Input text until you reach maxLength - 1
- Add a emoji
Expected Results
Same behaviour as on Android that the emoji is not accepted as input.
Snack, code example, screenshot, or link to a repository:
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 7
- Comments: 15
As a workaround I’ve removed
maxLength
prop and added the following code to theonChangeText
callback:It minimizes the chances of the bug happening but doesn’t fix it.
I’ve found a workaround that fixes the issue:
It’s a bit ugly to access the private
_dispatchInstances
, but it does work.I’m using React Native ~0.63.4.