Open kunalchavhan opened 3 weeks ago
I was debugging this and found that this method is getting called whenever text is updated from react's state change. This method then calls setAttributedString
which then sets the text. I could not find any checks here for maxLength
prop.
When input is received from keyboard this method is called which has maxLength
prop check.
Any reason for not checking maxLength
prop here also?
@sammy-SC could you please take a look into this? Thank you for your time.
I was debugging this and found that this method is getting called whenever text is updated from react's state change. This method then calls
setAttributedString
which then sets the text. I could not find any checks here formaxLength
prop.When input is received from keyboard this method is called which has
maxLength
prop check.Any reason for not checking
maxLength
prop here also?
The code you are linking is for the new architecture but your repro is with the old architecture. For the old architecture, maxLength is handled here. It appears maxLength is not taken account for when the update comes from React.
This can be annoying but there is a simple workaround if this is blocking you. When setting text from React, you can check for the length of new text value and shorten it if it goes over the maxLength. maxLength is important for the updates to TextInput coming from native, because there your JavaScript code doesn't have a a chance to run. Thanks to maxLength prop, you have a chance to prevent iOS from painting TextInput with value that is longer than maxLength.
The code you are linking is for the new architecture but your repro is with the old architecture. For the old architecture, maxLength is handled here. It appears maxLength is not taken account for when the update comes from React.
This can be annoying but there is a simple workaround if this is blocking you. When setting text from React, you can check for the length of new text value and shorten it if it goes over the maxLength. maxLength is important for the updates to TextInput coming from native, because there your JavaScript code doesn't have a a chance to run. Thanks to maxLength prop, you have a chance to prevent iOS from painting TextInput with value that is longer than maxLength.
Thank you for the clarification and sorry for wrong linking of code. I pushed repro for new architecture here.
Thank you for suggesting workaround. I have checked that the maxLength
prop works for updates from React on android. Would it be possible to have the same behaviour on iOS also?
Thank you for the clarification and sorry for wrong linking of code. I pushed repro for new architecture here.
Thank you for suggesting workaround. I have checked that the
maxLength
prop works for updates from React on android. Would it be possible to have the same behaviour on iOS also?
@sammy-SC if you agree that iOS should handle maxLength
on updates from react, can I try to resolve this?
Description
maxLength
of Text Input is not working when value is changed after state update. It works as expected when input is given with keyboard.<TextInput style={styles.textInput} value={description} maxLength={10} onChangeText={text => setDescription(text)} />
Steps to reproduce
maxLength
of TextInput to any non zero numbermaxLength
maxLength
React Native Version
0.74.2
Affected Platforms
Runtime - iOS
Output of
npx react-native info
Stacktrace or Logs
Reproducer
https://github.com/kunalchavhan/rn-textinput-maxlength-reproducer
Screenshots and Videos
https://github.com/facebook/react-native/assets/61144478/4a377ec3-e60a-46ad-b54d-c6362ebd8622