From 1a35bc55625cd8734711d380e19b00d37fe0631e Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 12 Mar 2019 11:04:55 -0700 Subject: [PATCH] Fix TextInput maxLength when insert characters at begin (#23472) Summary: Fixes #21639 , seems we tried to fix this before, please see related `PR` like [D10392176](https://github.com/facebook/react-native/commit/36507e4a3c2fa58dcfdc9bd389b37536c66006d0), #18627, but they don't solve it totally. [iOS] [Fixed] - Fix TextInput maxLength when insert characters at begin Pull Request resolved: https://github.com/facebook/react-native/pull/23472 Reviewed By: mmmulani Differential Revision: D14366406 Pulled By: ejanzer fbshipit-source-id: fc983810703997b48824f84f2f9198984afba9cd --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index bbee808e7ca..fac75588340 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -404,18 +404,12 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame) } } - if (range.location + range.length > _predictedText.length) { - // _predictedText got out of sync in a bad way, so let's just force sync it. Haven't been able to repro this, but - // it's causing a real crash here: #6523822 + NSString *previousText = backedTextInputView.attributedText.string ?: @""; + + if (range.location + range.length > backedTextInputView.attributedText.string.length) { _predictedText = backedTextInputView.attributedText.string; - } - - NSString *previousText = [_predictedText substringWithRange:range] ?: @""; - - if (!_predictedText || backedTextInputView.attributedText.string.length == 0) { - _predictedText = text; } else { - _predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text]; + _predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range withString:text]; } if (_onTextInput) { @@ -450,7 +444,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame) [self textInputShouldChangeTextInRange:predictionRange replacementText:replacement]; // JS will assume the selection changed based on the location of our shouldChangeTextInRange, so reset it. [self textInputDidChangeSelection]; - _predictedText = backedTextInputView.attributedText.string; } _nativeEventCount++;