Created
October 28, 2023 17:00
-
-
Save mattyoung/fe5657356e97b1acacde2bd10d5e5716 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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