Reputation: 336
Using the searchable
view modifier with tokens allows me to present the user with search suggestions. When the search field is activated, the list of suggested tokens is presented on top of the list view (containing the results). Once tokens are selected and the user submits the search, the list of suggestions disappears and the list view is visible again. At least, that's what is supposed to happen.
However I'm increasingly running into the situation (bug?) where the suggested tokens block the actual results view, even after I submit the search (by pressing enter). The only way to dismiss the token list is to cancel the search, which obviously also discards the search query.
Below is a minimally reproducable example, tested on an iPad (iOS 16.4). I've tried the iPad simulator as well as a physical device. The latter used to work intermittently but now also doesn't dismiss the token view when submitting the search. I've tried both hardware and software keyboards on both devices.
struct SearchableTest: View {
@StateObject private var filterModel = TestFilterModel()
var body: some View {
NavigationSplitView {
List {
Text("Testrow 1")
Text("Testrow 2")
}
.searchable(text: $filterModel.searchFilter,
tokens: $filterModel.selectedTokens,
suggestedTokens: $filterModel.suggestedTokens,
placement: .navigationBarDrawer,
prompt: "Search",
token: { $0.tokenLabel })
} detail: {
Text("Detail View")
}
.task { filterModel.load() }
}
}
@MainActor final class TestFilterModel: ObservableObject {
@Published var searchFilter = ""
@Published var selectedTokens = [TestFilterToken]()
@Published var suggestedTokens = [TestFilterToken]()
func load() {
self.suggestedTokens = [
.itemType("Type 1"),
.itemType("Type 2"),
.itemType("Type 3"),
.status("Status 1"),
.status("Status 2"),
.status("Status 3"),
]
}
}
// ======================================================= //
// MARK: - Tokens
// ======================================================= //
enum TestFilterToken: Identifiable {
case itemType(String)
case status(String)
var id: String { self.title }
var tokenLabel: Label<Text, Image> {
Label(title, systemImage: systemImage)
}
var title: String {
switch self {
case let .itemType(string), let .status(string):
return string
}
}
var systemImage: String {
switch self {
case .itemType:
return "circle.fill"
case .status:
return "square.fill"
}
}
}
I think the source of the problem lies in the fact that the return key on the keyboards I've tried are not considered a search submission anymore. When search submission worked, the return key would show the text 'search' and pressing it would indeed dismiss the suggestion view. But I have no idea how to enforce this.
Upvotes: 4
Views: 490