DEV Community

Liang Wang
Liang Wang

Posted on

Searchable

struct SuccessView: View {
  @ObservedObject var viewModel: UserViewModel
  @State private var searchText = ""

  var results: [User] {
    searchText.isEmpty ? viewModel.users : viewModel.users.filter {$0.name.contains(searchText)}
  }

  var body: some View {
    List {
      ForEach(results) { user in
        NavigationLink {
          UserDetailView(user: user)
        } label: {
          UserRowView(user: user)
        } //: NavigationLink
      } //: ForEach
    } //: List
    .searchable(text: $searchText)
  }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)