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](36507e4a3c), #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
This commit is contained in:
zhongwuzw 2019-03-12 11:04:55 -07:00 committed by Lorenzo Sciandra
parent bdf809e817
commit 1a35bc5562

View File

@ -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++;