I asked this on Stack Overflow,, and here,, but so far no answers.
I don't know if this is a bug on the implementation of .searchable on macOS, or it's the expected behavior, but when you add a .searchable to a NavigationView, with the expected behavior of the search field being placed at the right most part of the toolbar, a focus issue happens.
When you type the text you want to search, I can filter the list with that text, but when I place the mouse cursor on the first item on the list and try to move down the list with the arrow key, with each arrow key press, the focus goes back to the search field, making it impossible to navigate up and down the list with the keyboard. The same happens if I select each item with the mouse.
Maybe I'm not implementing it right, or maybe it doesn't work yet with macOS, either way, this is the code I'm using. This is part of a three panel view, with this one being the middle view (right next to the side bar):
struct AllNotes: View {
@EnvironmentObject private var data: DataModel
@State var selectedNoteId: UUID?
@State var searchText: String = ""
var body: some View {
NavigationView {
List(data.notes.filter { searchText.isEmpty ? true : $0.text.localizedCaseInsensitiveContains(searchText) }) { note in
NavigationLink(
destination: NoteView(note: note),
tag: note.id,
selection: $selectedNoteId
) {
VStack(alignment: .leading) {
Text(getFirstLine(noteText: note.text)).font(.body).fontWeight(.bold)
}
}
}
.listStyle(InsetListStyle())
.toolbar {
// a few other buttons
}
}
.searchable(
text: $searchText,
placement: .toolbar,
prompt: "Search..."
)
}
}