Skip to content

Instantly share code, notes, and snippets.

@mattyoung
Created October 28, 2023 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattyoung/fe5657356e97b1acacde2bd10d5e5716 to your computer and use it in GitHub Desktop.
Save mattyoung/fe5657356e97b1acacde2bd10d5e5716 to your computer and use it in GitHub Desktop.
import SwiftUI
struct SearchableStudy: View {
// !! Searchable searchCompletion() seems to have problem
// handling terms with common prefix like "Josh" and "Joshee"
// Try enter "Jos" and tap the "Josh" entery, it doesn't show
// the "Josh" entry. It show "Joshee" completion.
let names = ["Hellen", "Holly", "Josh", "Joshee", "Rhonda", "Ted"]
@State private var searchText = ""
var body: some View {
NavigationStack {
List {
ForEach(searchResults, id: \.self) { name in
NavigationLink {
Text(name)
} label: {
Text(name)
}
}
}
.navigationTitle("Contacts")
}
.searchable(text: $searchText) {
ForEach(searchResults, id: \.self) { result in
Text("Are you looking for \(result)?").searchCompletion(result)
}
}
}
var searchResults: [String] {
if searchText.isEmpty {
return names
} else {
return names.filter { $0.contains(searchText) }
}
}
}
#Preview {
SearchableStudy()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment