Skip to content

Commit 5ca0df4

Browse files
committed
Add a searchable fruit-filter playground
1 parent 67be6cf commit 5ca0df4

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#if canImport(AndroidSwiftUI)
2+
import AndroidSwiftUI
3+
#else
4+
import SwiftUI
5+
#endif
6+
7+
struct SearchablePlayground: View {
8+
@State private var query = ""
9+
10+
private let fruits = [
11+
"Apple", "Apricot", "Banana", "Blueberry", "Cherry", "Grape",
12+
"Lemon", "Lime", "Mango", "Orange", "Peach", "Pear", "Plum",
13+
]
14+
15+
private var filtered: [String] {
16+
query.isEmpty ? fruits : fruits.filter { $0.lowercased().contains(query.lowercased()) }
17+
}
18+
19+
var body: some View {
20+
ScrollView {
21+
VStack(alignment: .leading, spacing: 12) {
22+
if filtered.isEmpty {
23+
Text("No matches").foregroundColor(.gray)
24+
}
25+
ForEach(filtered, id: \.self) { fruit in
26+
Text(fruit)
27+
}
28+
}
29+
.padding()
30+
}
31+
.searchable(text: $query, prompt: "Search fruit")
32+
.navigationTitle("Searchable")
33+
}
34+
}

0 commit comments

Comments
 (0)