From f08c94bd36fe9cdc76bea521d585bcb939b85002 Mon Sep 17 00:00:00 2001 From: jsfu Date: Tue, 19 Feb 2019 21:37:06 -0800 Subject: [PATCH] fix the TextInput can't control input length when value's length > maxLength (#23545) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: I found the TextInput can't control input length when default value's length > maxLength. for example: 1.Set the value in special cases ``` ``` 2.Quickly press the keyboard with multiple fingers ``` // RCTBaseTextInputView.m …… if (_maxLength) { NSUInteger allowedLength = _maxLength.integerValue - backedTextInputView.attributedText.string.length + range.length; if (text.length > allowedLength) { …… ``` when value's length > maxLength,the allowedLength not a negative number.it was transformed into a big number,because it is type NSUInteger.so the `text.length > allowedLength` always false. [iOS][Fixed] - fix the TextInput can't control input length when value's length > maxLength Pull Request resolved: https://github.com/facebook/react-native/pull/23545 Differential Revision: D14146581 Pulled By: cpojer fbshipit-source-id: f53b1312ae55fad9fc10430ab94784c1a9ad4723 --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index c9f137043e5..3c8321c262b 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -305,9 +305,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame) } if (_maxLength) { - NSUInteger allowedLength = _maxLength.integerValue - backedTextInputView.attributedText.string.length + range.length; + NSInteger allowedLength = _maxLength.integerValue - backedTextInputView.attributedText.string.length + range.length; - if (text.length > allowedLength) { + if (allowedLength < 0 || 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