From 477663cba8c353d950a9a928844929903d55933a Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Tue, 9 Aug 2022 14:30:35 -0700 Subject: [PATCH] Avoid keypress event when text is pasted on macOS Summary: There is an issue on react-native-macOS in which clipboard pastes cause a keyPress event both for SingleLine and Multiline text fields. This problem does not exist on iOS. However, we can fix it for macOS and keep the iOS behavior unchanged. # Invocation order on macOS ## macOS Singeline textField - https://github.com/microsoft/react-native-macos/blob/main/Libraries/Text/TextInput/Multiline/RCTUITextView.m#L309 is called - [NSTextView(NSPasteboard) paste:] () is called - [NSTextView(NSSharing) shouldChangeTextInRanges:replacementStrings:] () is called - https://github.com/microsoft/react-native-macos/blob/main/Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m#L382 is called - https://github.com/microsoft/react-native-macos/blob/main/Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m#L323 is called - https://github.com/microsoft/react-native-macos/blob/main/Libraries/Text/TextInput/RCTBaseTextInputView.m#L436 There is the issue. As ```!backedTextInputView.textWasPasted``` is still ```NO``` we accidently send a keyPress event ## macOS Multiline textView - [NSTextView(NSSharing) shouldChangeTextInRanges:replacementStrings:] () is called - https://github.com/microsoft/react-native-macos/blob/main/Libraries/Text/TextInput/Singleline/RCTUITextField.m#L438 is called - https://github.com/microsoft/react-native-macos/blob/main/Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m#L91 is called - https://github.com/microsoft/react-native-macos/blob/main/Libraries/Text/TextInput/RCTBaseTextInputView.m#L436 There is the issue. As ```!backedTextInputView.textWasPasted``` is still ```NO``` we accidently send a keyPress event # Invocation order on iOS Problem does not arise as https://github.com/facebook/react-native/blob/main/Libraries/Text/TextInput/RCTBaseTextInputView.m#L381 is not called as [UIPasteboard _performAsDataOwner:block:] () is used, not causing an side-effects Changelog: [macOS][Fixed] - Avoid keypress event when text is pasted on macOS Reviewed By: sammy-SC Differential Revision: D38460692 fbshipit-source-id: 343425d3866d32973b118c90a5bfd8ee9db146b6 --- Libraries/Text/TextInput/Multiline/RCTUITextView.m | 2 +- Libraries/Text/TextInput/Singleline/RCTUITextField.m | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/Text/TextInput/Multiline/RCTUITextView.m b/Libraries/Text/TextInput/Multiline/RCTUITextView.m index 92371bc8bf..9d38dd14bc 100644 --- a/Libraries/Text/TextInput/Multiline/RCTUITextView.m +++ b/Libraries/Text/TextInput/Multiline/RCTUITextView.m @@ -164,8 +164,8 @@ static UIColor *defaultPlaceholderColor() - (void)paste:(id)sender { - [super paste:sender]; _textWasPasted = YES; + [super paste:sender]; } // Turn off scroll animation to fix flaky scrolling. diff --git a/Libraries/Text/TextInput/Singleline/RCTUITextField.m b/Libraries/Text/TextInput/Singleline/RCTUITextField.m index b71ef0c961..42a6bc801b 100644 --- a/Libraries/Text/TextInput/Singleline/RCTUITextField.m +++ b/Libraries/Text/TextInput/Singleline/RCTUITextField.m @@ -188,8 +188,8 @@ - (void)paste:(id)sender { - [super paste:sender]; _textWasPasted = YES; + [super paste:sender]; } #pragma mark - Layout