Skip to content

Commit 35f9585

Browse files
hamaronaleclarson
authored andcommitted
iOS: Fixed the bug where a Backspace event was emitted when entering characters after clearing a text in TextInput by an empty string (#18627)
Summary: The bug #18374 was caused by the loose condition to execute `stringByReplacingCharactersInRange` in the method `textInputShouldChangeTextInRange` . As a result, `findMismatch` wrongly returning `true` which ends up the Backspace event being fired in another `textInputShouldChangeTextInRange` call in `textInputDidChange`. Closes facebook/react-native#18627 Differential Revision: D8436331 Pulled By: hramos fbshipit-source-id: ec75a6ca926061cbf7cb106db652f2b4a71c9a0c
1 parent be0a536 commit 35f9585

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Libraries/Text/TextInput/RCTBaseTextInputView.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,12 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin
278278

279279
NSString *previousText = [_predictedText substringWithRange:range] ?: @"";
280280

281-
if (_predictedText) {
281+
// After clearing the text by replacing it with an empty string, `_predictedText`
282+
// still preserves the deleted text.
283+
// As the first character in the TextInput always comes with the range value (0, 0),
284+
// we should check the range value in order to avoid appending a character to the deleted string
285+
// (which caused the issue #18374)
286+
if (!NSEqualRanges(range, NSMakeRange(0, 0)) && _predictedText) {
282287
_predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text];
283288
} else {
284289
_predictedText = [text copy];

0 commit comments

Comments
 (0)