From e6cffcba3f2e3c65caf739eeebbf359693d86565 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Thu, 10 Sep 2026 17:27:37 +0300 Subject: [PATCH] Clear the search query when leaving the search tab Switching away from the search tab now resets the query, so returning to it shows the recent searches instead of the previous results. The search field no longer focuses itself on appearance. --- .../Sources/Screens/SearchScreen/View/SearchScreen.swift | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ElementX/Sources/Screens/SearchScreen/View/SearchScreen.swift b/ElementX/Sources/Screens/SearchScreen/View/SearchScreen.swift index 47d9a30d1..501417178 100644 --- a/ElementX/Sources/Screens/SearchScreen/View/SearchScreen.swift +++ b/ElementX/Sources/Screens/SearchScreen/View/SearchScreen.swift @@ -97,14 +97,19 @@ struct SearchScreen: View { .searchResultsKeyboardNavigation(moveUp: { moveSelection(backwards: true) }, moveDown: { moveSelection(backwards: false) }, cancel: { context.send(viewAction: .cancel) }) - // The TabView calls onAppear each time the search tab is selected, so the field re-focuses - // and the selection resets on every switch. + // The TabView calls onAppear/onDisappear each time the search tab is selected/deselected, + // so the query is cleared and the selection resets on every switch. .onAppear { context.send(viewAction: .appeared) isSearchFieldFocused = true selectedID = selectableIDs.first updateHardwareKeyboardConnected() } + .onDisappear { + // Previews and snapshot tests mount and unmount the view, don't clear their fixed state. + guard !ProcessInfo.isXcodePreview, !ProcessInfo.isRunningTests else { return } + context.searchQuery = "" + } .onReceive(NotificationCenter.default.publisher(for: .GCKeyboardDidConnect)) { _ in updateHardwareKeyboardConnected() }