API Reference / InstantSearch iOS Widgets / Clear Filters
Widget signature
FilterClearConnector(
  filterState: FilterState,
  interactor: FilterClearInteractor,
  clearMode: ClearMode,
  filterGroupIDs: [FilterGroup.ID]?,
  controller: FilterClearController
)

About this widget

Clear Filters lets the user clear all filters that are currently active within the given FilterState.

Examples

Instantiate a FilterClearConnector.

1
2
3
4
5
6
7
8
let filterState: FilterState = .init()
let clearFiltersController: FilterClearButtonController = .init(button: UIButton())

let filterClearConnector: FilterClearConnector = .init(filterState: filterState,
                                                       clearMode: .specified,
                                                       filterGroupIDs: [.and(name: "color"), .or(name: "category",
                                                                                                 filterType: .facet)],
                                                       controller: clearFiltersController)

Parameters

filterState
type: FilterState
Required

The FilterState that holds your filters.

interactor
type: FilterClearInteractor
default: .init()
Required

The logic applied to Clear Filters.

clearMode
type: ClearMode
default: .specified
Optional

Whether we should clear the specified filters or all filters except them.

filterGroupIDs
type: [FilterGroup.ID]?
default: nil
Optional

The groupIDs of filters to clear. All filters will be cleared if unspecified.

controller
type: FilterClearController
default: nil
Optional

The Controller interfacing with a concrete clear filters view.

Low-level API

If you want to fully control the Clear Filters components and connect them manually, use the following components:

  • FilterClearInteractor: The logic for clearing filters in the FilterState.
  • FilterState: The current state of the filters.
  • FilterClearController: The controller that interfaces with a concrete clear filters view.
1
2
3
4
5
6
7
8
9
10
11
let filterState: FilterState = .init()
let filterClearInteractor: FilterClearInteractor = .init()
let clearFiltersController: FilterClearButtonController = .init(button: UIButton())

filterClearInteractor.connectFilterState(filterState,
                                         filterGroupIDs: [
                                          .and(name: "color"),
                                          .or(name: "category", filterType: .facet)
                                         ],
                                         clearMode: .specified)
filterClearInteractor.connectController(clearFiltersController)

Customizing your view

The controllers provided by default, like the FilterClearButtonController work well when you want to use native UIKit with their default behavior.

If you want to use another component (other than a UIButton) such as a UIView, a third-party input view, or you want to introduce some custom behavior to the already provided UIKit component, you can create your own controller conforming to the FilterClearController protocol.

Protocol

var onClick: ((Facet) -> Void)?:

Closure to call when the clear filters button is clicked.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class FilterClearButtonController: FilterClearController {

  public let button: UIButton

  public var onClick: (() -> Void)?

  public init(button: UIButton) {
    self.button = button
    button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
  }

  @objc private func didTapButton() {
    onClick?()
  }

}

Did you find this page helpful?