-
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
TextInput onChangeText is called with empty text if maxLength is exceeded on iOS #28774
Comments
Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions. |
Reran the snack it with latest version of Expo v38.0.0 and React Native 0.62.2 and the issue is still there. |
Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions. |
Same issue here... Screen.Recording.2021-03-19.at.10.09.59.movAs you can see on video, when I exceed the maximum length of the input it is automatically cleared. |
As a workaround I've removed onChangeText={value => {
if (value.length <= maxLength) {
setNewValue(value)
}
}} It minimizes the chances of the bug happening but doesn't fix it. |
Please resolve this ! |
I don't see any difference with this. Eg if the max length is 20 and I've written 19 chars, and then I add an emoji, it always clears the whole input text. My understanding is that an emoji counts like 2 chars in terms of max length, because in my case I have a max length of 20, and I can write either 20 chars max, or 10 emoji max. Hence, when I have 19 chars and I add an emoji it's like having 21 chars, which is beyond the max length, which triggers the input clearing. |
This comment is interesting: #24109 (comment)
|
I've found a workaround that fixes the issue: import * as React from 'react'
import { Platform, TextInput, TextInputProps } from 'react-native'
export interface TextInputFixedProps extends TextInputProps {
onChangeText: (text: string) => void
}
/**
* Workaround for https://github.com/facebook/react-native/issues/28774
* and https://github.com/status-im/status-react/issues/12919.
*
* Fixes `onChange` clearing the whole input when an emoji is typed as the last
* character when having `maxLength`. This happens on iOS only.
*
* Requires `onChangeText` to be set.
*/
export function TextInputFixed(props: TextInputFixedProps) {
if (Platform.OS === 'ios') {
return (
<TextInput
{...props}
onChangeText={undefined}
onChange={(event) => {
const newText = event.nativeEvent.text
const currentText = event._dispatchInstances.memoizedProps.text
const clearsInput =
props.maxLength &&
currentText.length === props.maxLength - 1 &&
newText.length === 0
if (!clearsInput) {
props.onChangeText(newText)
}
}}
/>
)
} else {
return <TextInput {...props} />
}
} It's a bit ugly to access the private I'm using React Native ~0.63.4. |
textInput_emoji_issue.movHi, I'm not able to reproduce on snack. Am I missing something?? |
@AlbertVilaCalvo We can close this, if this is fixed in 0.69? |
@athakur3 I have not upgraded to 0.69 so I don't know if it's fixed. |
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
This issue was closed because it has been stalled for 7 days with no activity. |
This issue is still existing. Is it being fixed? |
Description
If the max length of a controlled
TextInput
is exceeded on iOSonChangeText
will get called with an empty string. We noticed this issue when our users added an emoji as the last character in aTextInput
. 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.
Expected Results
Same behaviour as on Android that the emoji is not accepted as input.
Snack, code example, screenshot, or link to a repository:
https://snack.expo.io/8x3dBwzJe
The text was updated successfully, but these errors were encountered: