From 17415938c7180a95811db949122b8ad24a442866 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 0a8ea15b7c0eaf..e10e508f0904d1 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -398,18 +398,12 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin } } - 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) { @@ -444,7 +438,6 @@ - (void)textInputDidChange [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++;