Skip to content

Commit 9f9cbc7

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 a9373fd commit 9f9cbc7

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
@@ -268,7 +268,12 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin
268268

269269
NSString *previousText = [_predictedText substringWithRange:range] ?: @"";
270270

271-
if (_predictedText) {
271+
// After clearing the text by replacing it with an empty string, `_predictedText`
272+
// still preserves the deleted text.
273+
// As the first character in the TextInput always comes with the range value (0, 0),
274+
// we should check the range value in order to avoid appending a character to the deleted string
275+
// (which caused the issue #18374)
276+
if (!NSEqualRanges(range, NSMakeRange(0, 0)) && _predictedText) {
272277
_predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text];
273278
} else {
274279
_predictedText = text;

0 commit comments

Comments
 (0)