From ba8744a97326da233aec3385ca283e0e88a530dc Mon Sep 17 00:00:00 2001 From: Nick Lefever Date: Thu, 26 Oct 2023 19:25:27 +0200 Subject: [PATCH] [fabric] Make text input resign first responder through the window instance Summary: Fix AppKit exception throws when blurring text inputs by calling `resignFirstResponder` directly on the backing text input view. Resigning the first responder state has to happen through the window by calling `[window makeFirstResponder:nil]` which will: - call `resignFirstResponder` on the current first responder - if successful, the window will become the first responder Test Plan: - Run Zeratul with Fabric - Focus the search text input above the message threads - Click inside the active message thread to trigger the auto-focus of the composer and the blur on the search field - The focused field resigns the first responder status without throwing an exception https://pxl.cl/3GvZD Reviewers: shawndempsey, #rn-desktop Reviewed By: shawndempsey Differential Revision: https://phabricator.intern.facebook.com/D50700782 --- .../ComponentViews/TextInput/RCTTextInputComponentView.mm | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm index f1930cfb0ed7eb..a419c9faa1322b 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm @@ -552,7 +552,15 @@ - (void)focus - (void)blur { +#if !TARGET_OS_OSX // [macOS] [_backedTextInputView resignFirstResponder]; +#else + NSWindow *window = _backedTextInputView.window; + if (window && window.firstResponder == _backedTextInputView) { + // Calling makeFirstResponder with nil will call resignFirstResponder and make the window the first responder + [window makeFirstResponder:nil]; + } +#endif // macOS]; } - (void)setTextAndSelection:(NSInteger)eventCount