A convenience modifier that adds a UISearchBar on top of a SwiftUI List and allows filtering its content with a query

SearchableList

Description

A convenience modifier that adds a UISearchBar on top of a SwiftUI List and allows filtering its content with a query (a binding string).

Description

The SearchableList relies on ViewModifier and UIViewRepresentable (accessible only through lists using .searchable(query:)) in order to allow users to easily filter the list`s content.

Usage

struct ExampleView: View {
  @State private var query: String = ""

  var body: some View {
    List {
      ForEach(TestFruits.allCases, id: \.self) {
        if query != "" {
          if $0.rawValue.contains(query) {
            Text($0.rawValue)
          }
        } else {
          Text($0.rawValue)
        }
      }
    }
    .searchable(query: $query)
  }
}

Preview

Looking for Starks

No Filter Filter

GitHub

View Github