Skip to content

Commit

Permalink
Avoid keypress event when text is pasted on macOS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Aug 9, 2022
1 parent 1bc9ddb commit 477663c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Libraries/Text/TextInput/Multiline/RCTUITextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ - (void)setSelectedTextRange:(UITextRange *)selectedTextRange notifyDelegate:(BO

- (void)paste:(id)sender
{
[super paste:sender];
_textWasPasted = YES;
[super paste:sender];
}

// Turn off scroll animation to fix flaky scrolling.
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Text/TextInput/Singleline/RCTUITextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ - (void)setSelectedTextRange:(UITextRange *)selectedTextRange notifyDelegate:(BO

- (void)paste:(id)sender
{
[super paste:sender];
_textWasPasted = YES;
[super paste:sender];
}

#pragma mark - Layout
Expand Down

0 comments on commit 477663c

Please sign in to comment.