Skip to content

Commit 1a35bc5

Browse files
zhongwuzwkelset
authored andcommitted
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](36507e4), #18627, but they don't solve it totally. [iOS] [Fixed] - Fix TextInput maxLength when insert characters at begin Pull Request resolved: #23472 Reviewed By: mmmulani Differential Revision: D14366406 Pulled By: ejanzer fbshipit-source-id: fc983810703997b48824f84f2f9198984afba9cd
1 parent bdf809e commit 1a35bc5

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

Libraries/Text/TextInput/RCTBaseTextInputView.m

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -404,18 +404,12 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin
404404
}
405405
}
406406

407-
if (range.location + range.length > _predictedText.length) {
408-
// _predictedText got out of sync in a bad way, so let's just force sync it. Haven't been able to repro this, but
409-
// it's causing a real crash here: #6523822
407+
NSString *previousText = backedTextInputView.attributedText.string ?: @"";
408+
409+
if (range.location + range.length > backedTextInputView.attributedText.string.length) {
410410
_predictedText = backedTextInputView.attributedText.string;
411-
}
412-
413-
NSString *previousText = [_predictedText substringWithRange:range] ?: @"";
414-
415-
if (!_predictedText || backedTextInputView.attributedText.string.length == 0) {
416-
_predictedText = text;
417411
} else {
418-
_predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text];
412+
_predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range withString:text];
419413
}
420414

421415
if (_onTextInput) {
@@ -450,7 +444,6 @@ - (void)textInputDidChange
450444
[self textInputShouldChangeTextInRange:predictionRange replacementText:replacement];
451445
// JS will assume the selection changed based on the location of our shouldChangeTextInRange, so reset it.
452446
[self textInputDidChangeSelection];
453-
_predictedText = backedTextInputView.attributedText.string;
454447
}
455448

456449
_nativeEventCount++;

0 commit comments

Comments
 (0)