Skip to content

Instantly share code, notes, and snippets.

@oliverfoggin
Created August 5, 2021 19:36
Show Gist options
  • Save oliverfoggin/8920e3c5dde93c73bb600a743f6e1b24 to your computer and use it in GitHub Desktop.
Save oliverfoggin/8920e3c5dde93c73bb600a743f6e1b24 to your computer and use it in GitHub Desktop.
import Foundation
import SwiftUI
struct VanillaSearchableView: View {
let names = ["Oliver", "Emily", "Jessica", "Daniel", "Eva"]
@State var searchText: String = ""
var body: some View {
NavigationView {
List {
ForEach(searchResults, id: \.self) { name in
Text(name)
}
}
.navigationBarTitle("Searchable")
.searchable(
text: $searchText,
prompt: "Search names..."
)
}
}
var searchResults: [String] {
if searchText.isEmpty {
return names
} else {
return names.filter { $0.lowercased().contains(searchText.lowercased()) }
}
}
}
struct VanillaSearchableView_Previews: PreviewProvider {
static var previews: some View {
VanillaSearchableView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment