Created
August 5, 2021 19:36
-
-
Save oliverfoggin/8920e3c5dde93c73bb600a743f6e1b24 to your computer and use it in GitHub Desktop.
This file contains 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 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