Skip to content

Commit

Permalink
Added snippet for the searchable modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
rajhraval authored May 9, 2022
1 parent ae8bcb0 commit 789bd3f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Searchable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import SwiftUI

// During WWDC21, Apple introduced a new modifier .searchable that helps you to show a search bar on your app.
// Here’s how you can add the searchable modifier in your app.

struct ContentView: View {

let developers = ["Rod", "Mac", "Stewart", "Lisa", "Andrea", "Steve", "Julia", "Chris", "Penelope"]

var filteredDevelopers: [String] {
if searchText.isEmpty {
return developers
} else {
return developers.filter { $0.contains(searchText) }
}
}

@State private var searchText = ""

var body: some View {
NavigationView {
List {
ForEach(filteredDevelopers, id: \.self) { name in
Text(name)
}
}
.searchable(text: $searchText)
.navigationTitle("My App")
}
}
}

0 comments on commit 789bd3f

Please sign in to comment.