mirror of
https://github.com/status-im/react-native.git
synced 2025-01-28 02:04:55 +00:00
Fix TextInput stack overflow when pasting text in multiline input with maxlength
Summary: When pasting text longer than maxlenght, the textDidChange: call we did would end calling back into textView:shouldChange: because we saw an unexpected multi-character change. Since this is an expected mutation, update predictedText appropriately. Reviewed By: majak Differential Revision: D3561524 fbshipit-source-id: 07bb78d830ccfa3aed6ee274dc30adeadce9e1f8
This commit is contained in:
parent
55fb4f4a75
commit
23e087fc26
@ -679,6 +679,12 @@ exports.examples = [
|
||||
keyboardType="url"
|
||||
style={[styles.multiline, styles.multilineWithFontStyles]}
|
||||
/>
|
||||
<TextInput
|
||||
placeholder="multiline text input with max length"
|
||||
maxLength={5}
|
||||
multiline={true}
|
||||
style={styles.multiline}
|
||||
/>
|
||||
<TextInput
|
||||
placeholder="uneditable multiline text input"
|
||||
editable={false}
|
||||
|
@ -377,16 +377,20 @@ static NSAttributedString *removeReactTagFromString(NSAttributedString *string)
|
||||
if (_maxLength) {
|
||||
NSUInteger allowedLength = _maxLength.integerValue - textView.text.length + range.length;
|
||||
if (text.length > allowedLength) {
|
||||
// If we typed/pasted more than one character, limit the text inputted
|
||||
if (text.length > 1) {
|
||||
// Truncate the input string so the result is exactly maxLength
|
||||
NSString *limitedString = [text substringToIndex:allowedLength];
|
||||
NSMutableString *newString = textView.text.mutableCopy;
|
||||
[newString replaceCharactersInRange:range withString:limitedString];
|
||||
textView.text = newString;
|
||||
_predictedText = newString;
|
||||
|
||||
// Collapse selection at end of insert to match normal paste behavior
|
||||
UITextPosition *insertEnd = [textView positionFromPosition:textView.beginningOfDocument
|
||||
offset:(range.location + allowedLength)];
|
||||
textView.selectedTextRange = [textView textRangeFromPosition:insertEnd toPosition:insertEnd];
|
||||
|
||||
[self textViewDidChange:textView];
|
||||
}
|
||||
return NO;
|
||||
|
Loading…
x
Reference in New Issue
Block a user