From 9f9cbc773f327d7153868eb85cb7dc5092c2faa5 Mon Sep 17 00:00:00 2001 From: hamaron Date: Thu, 14 Jun 2018 18:06:05 -0700 Subject: [PATCH] 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 https://github.com/facebook/react-native/pull/18627 Differential Revision: D8436331 Pulled By: hramos fbshipit-source-id: ec75a6ca926061cbf7cb106db652f2b4a71c9a0c --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 177b4afccd..2b00a8b047 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -268,7 +268,12 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin NSString *previousText = [_predictedText substringWithRange:range] ?: @""; - if (_predictedText) { + // After clearing the text by replacing it with an empty string, `_predictedText` + // still preserves the deleted text. + // As the first character in the TextInput always comes with the range value (0, 0), + // we should check the range value in order to avoid appending a character to the deleted string + // (which caused the issue #18374) + if (!NSEqualRanges(range, NSMakeRange(0, 0)) && _predictedText) { _predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text]; } else { _predictedText = text;