Skip to content

Commit

Permalink
Fix TextInput maxLength when insert characters at begin (#23472)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zhongwuzw authored and kelset committed Mar 29, 2019
1 parent bdf809e commit 1a35bc5
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,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) {
Expand Down Expand Up @@ -450,7 +444,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++;
Expand Down

0 comments on commit 1a35bc5

Please sign in to comment.