Skip to content

Commit 1741593

Browse files
zhongwuzwfacebook-github-bot
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 0b26645 commit 1741593

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

Libraries/Text/TextInput/RCTBaseTextInputView.m

+4-11
Original file line numberDiff line numberDiff line change
@@ -398,18 +398,12 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin
398398
}
399399
}
400400

401-
if (range.location + range.length > _predictedText.length) {
402-
// _predictedText got out of sync in a bad way, so let's just force sync it. Haven't been able to repro this, but
403-
// it's causing a real crash here: #6523822
401+
NSString *previousText = backedTextInputView.attributedText.string ?: @"";
402+
403+
if (range.location + range.length > backedTextInputView.attributedText.string.length) {
404404
_predictedText = backedTextInputView.attributedText.string;
405-
}
406-
407-
NSString *previousText = [_predictedText substringWithRange:range] ?: @"";
408-
409-
if (!_predictedText || backedTextInputView.attributedText.string.length == 0) {
410-
_predictedText = text;
411405
} else {
412-
_predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text];
406+
_predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range withString:text];
413407
}
414408

415409
if (_onTextInput) {
@@ -444,7 +438,6 @@ - (void)textInputDidChange
444438
[self textInputShouldChangeTextInRange:predictionRange replacementText:replacement];
445439
// JS will assume the selection changed based on the location of our shouldChangeTextInRange, so reset it.
446440
[self textInputDidChangeSelection];
447-
_predictedText = backedTextInputView.attributedText.string;
448441
}
449442

450443
_nativeEventCount++;

0 commit comments

Comments
 (0)