> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Refinement List

> Shows a list of facets for refining search results.

export const FlavorSwitcher = ({current, baseHref = "", options = [], label = "InstantSearch framework"}) => {
  if (options.length === 0) {
    return <div className="not-prose" role="alert" style={{
      margin: "0.25rem 0 1.5rem",
      padding: "0.75rem",
      border: "1px solid #f59e0b",
      borderRadius: "0.625rem",
      color: "inherit",
      fontSize: "0.875rem"
    }}>
        FlavorSwitcher requires at least one option.
      </div>;
  }
  const selected = options.find(option => option.value === current) ?? options[0];
  return <div className="not-prose mint-flavor-switcher">
      <style>{`
        .mint-flavor-switcher {
          --mfs-bg: #ffffff;
          --mfs-bg-hover: #f4f4f5;
          --mfs-bg-current: #eef2ff;
          --mfs-border: #d4d4d8;
          --mfs-fg: #18181b;
          --mfs-muted: #71717a;
          --mfs-accent: #4f46e5;
          position: relative;
          width: min(100%, 19rem);
          margin: 0.25rem 0 1.5rem;
          color: var(--mfs-fg);
          font-size: 0.875rem;
          line-height: 1.25rem;
        }

        .dark .mint-flavor-switcher {
          --mfs-bg: #18181b;
          --mfs-bg-hover: #27272a;
          --mfs-bg-current: #272747;
          --mfs-border: #3f3f46;
          --mfs-fg: #fafafa;
          --mfs-muted: #a1a1aa;
          --mfs-accent: #a5b4fc;
        }

        .mint-flavor-switcher details {
          position: relative;
        }

        .mint-flavor-switcher summary {
          display: flex;
          min-height: 2.75rem;
          box-sizing: border-box;
          align-items: center;
          justify-content: space-between;
          gap: 0.75rem;
          padding: 0.625rem 0.75rem;
          border: 1px solid var(--mfs-border);
          border-radius: 0.625rem;
          background: var(--mfs-bg);
          color: var(--mfs-fg);
          cursor: pointer;
          font-weight: 600;
          list-style: none;
          transition: border-color 150ms ease, box-shadow 150ms ease;
        }

        .mint-flavor-switcher summary::-webkit-details-marker {
          display: none;
        }

        .mint-flavor-switcher summary:hover {
          border-color: var(--mfs-accent);
        }

        .mint-flavor-switcher summary:focus-visible {
          outline: 2px solid var(--mfs-accent);
          outline-offset: 2px;
        }

        .mint-flavor-switcher__label {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .mint-flavor-switcher__chevron {
          flex: none;
          transition: transform 150ms ease;
        }

        .mint-flavor-switcher details[open] .mint-flavor-switcher__chevron {
          transform: rotate(180deg);
        }

        .mint-flavor-switcher__menu {
          position: absolute;
          z-index: 50;
          top: calc(100% + 0.375rem);
          left: 0;
          width: 100%;
          box-sizing: border-box;
          margin: 0;
          padding: 0.375rem;
          border: 1px solid var(--mfs-border);
          border-radius: 0.625rem;
          background: var(--mfs-bg);
          box-shadow: 0 12px 30px rgb(0 0 0 / 16%);
          list-style: none;
        }

        .mint-flavor-switcher__menu li {
          margin: 0;
          padding: 0;
        }

        .mint-flavor-switcher__option {
          display: grid;
          gap: 0.125rem;
          padding: 0.625rem 0.75rem;
          border-radius: 0.4rem;
          color: var(--mfs-fg);
          text-decoration: none;
        }

        .mint-flavor-switcher__option:hover {
          background: var(--mfs-bg-hover);
        }

        .mint-flavor-switcher__option:focus-visible {
          outline: 2px solid var(--mfs-accent);
          outline-offset: -2px;
        }

        .mint-flavor-switcher__option[aria-current="page"] {
          background: var(--mfs-bg-current);
          color: var(--mfs-accent);
        }

        .mint-flavor-switcher__name {
          font-weight: 600;
        }

        .mint-flavor-switcher__description {
          color: var(--mfs-muted);
          font-size: 0.8125rem;
        }

        @media (prefers-reduced-motion: reduce) {
          .mint-flavor-switcher summary,
          .mint-flavor-switcher__chevron {
            transition: none;
          }
        }
      `}</style>

      <details>
        <summary aria-label={`${label}: ${selected.label}`}>
          <span className="mint-flavor-switcher__label">{selected.label}</span>
          <svg className="mint-flavor-switcher__chevron" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
            <path d="m6 9 6 6 6-6" />
          </svg>
        </summary>

        <ul className="mint-flavor-switcher__menu" aria-label={label}>
          {options.map(option => {
    const isCurrent = option.value === selected.value;
    const href = option.href ?? `${baseHref.replace(/\/$/, "")}/${encodeURIComponent(option.value)}`;
    return <li key={option.value}>
                <a className="mint-flavor-switcher__option" href={href} aria-current={isCurrent ? "page" : undefined}>
                  <span className="mint-flavor-switcher__name">
                    {option.label}
                  </span>
                  {option.description ? <span className="mint-flavor-switcher__description">
                      {option.description}
                    </span> : null}
                </a>
              </li>;
  })}
        </ul>
      </details>
    </div>;
};

<div className="mint-flavor-switcher-slot not-prose">
  <FlavorSwitcher
    current="ios"
    baseHref="/doc/api-reference/widgets/refinement-list"
    options={[
{ value: "js", label: "JavaScript", description: "InstantSearch.js" },
{ value: "react", label: "React", description: "React InstantSearch" },
{ value: "vue", label: "Vue", description: "Vue InstantSearch" },
{ value: "ios", label: "iOS", description: "InstantSearch iOS" },
{ value: "android", label: "Android", description: "InstantSearch Android" },
{ value: "flutter", label: "Flutter", description: "Algolia for Flutter" },
]}
  />
</div>

```swift Signature theme={"system"}
FacetListConnector(
  searcher: SingleIndexSearcher,
  filterState: FilterState,
  attribute: Attribute,
  selectionMode: SelectionMode,
  facets: [Facet],
  operator: RefinementOperator,
  groupName: String,
  controller: FacetListController,
  presenter: FacetListPresenter
)
```

<Card title="Explore example code" icon="github" href="https://github.com/algolia/instantsearch-ios/tree/master/Examples/Showcase">
  Browse the Refinement List example code on GitHub.
</Card>

## About this widget

`RefinementList` is a filtering view that displays [facets](/doc/guides/managing-results/refine-results/filtering)
and lets users refine the search results by filtering on specific values.

### Requirements

The [`attribute`](#param-attribute) provided to the widget must be in attributes for faceting,
either on the [dashboard](/doc/guides/managing-results/refine-results/faceting/how-to/declaring-attributes-for-faceting-with-dashboard) or using the [`attributesForFaceting`](/doc/api-reference/api-parameters/attributesForFaceting) parameter with the API.

## Examples

Instantiate a `FacetListConnector` and launch an initial search on its searcher.

```swift Swift icon=code theme={"system"}
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourSearchOnlyAPIKey",
                            indexName: "YourIndexName")

let filterState: FilterState = .init()

let categoryTableViewController: UITableViewController = .init()
let categoryListController: FacetListTableController = .init(tableView: categoryTableViewController.tableView)
let facetListPresenter: FacetListPresenter = .init(sortBy: [.count(order: .descending)], limit: 5, showEmptyFacets: false)

let categoryConnector: FacetListConnector = .init(searcher: searcher,
                                                  filterState: filterState,
                                                  attribute: "category",
                                                  selectionMode: .multiple,
                                                  facets: [.init(value: "initial facet", count: 10)],
                                                  operator: .and,
                                                  controller: categoryListController,
                                                  presenter: facetListPresenter)

searcher.search()
```

## Parameters

<ParamField body="searcher" type="HitsSearcher" required>
  The [`Searcher`](/doc/api-reference/widgets/instantsearch/ios) that handles your searches.
</ParamField>

<ParamField body="filterState" type="FilterState" required>
  The [`FilterState`](/doc/api-reference/widgets/filter-state/ios) that holds your filters.
</ParamField>

<ParamField body="attribute" type="Attribute" required>
  The attribute to filter.
</ParamField>

<ParamField body="selectionMode" type="SelectionMode" post={["default: .multiple"]}>
  Whether a user can select `.single` or `.multiple` values.
</ParamField>

<ParamField body="facets" type="[Facet]" post={["default: "]}>
  If specified, the default facet value(s) to apply.
</ParamField>

<ParamField body="operator" type="RefinementOperator" required>
  Whether you apply an `and` or `or` behavior to the facets in the filterState.

  For example, if you select `color` as the attribute and an `or` behavior,\
  the filter sent to Algolia will be `color:red OR color:green`.

  Use [filters or facet filters](/doc/guides/managing-results/refine-results/filtering/in-depth/filters-and-facetfilters) for more complex result refinement.
</ParamField>

<ParamField body="groupName" type="String" post={["default: The raw value of the `attribute` parameter"]}>
  Filter group name.
</ParamField>

<ParamField body="controller" type="FacetListController" post={["default: nil"]}>
  Controller interfacing with a concrete facet list view.
</ParamField>

<ParamField body="presenter" type="SelectableListPresentable" post={["default: nil"]}>
  Presenter defining how a facet appears in the controller.
</ParamField>

## Presenter

<ParamField body="sortBy" type="[FacetSortCriterion]" default="[.count(order: .descending)]">
  How to sort facets. Must be one or more of the following values:

  * `.count(order: .descending)`
  * `.count(order: .ascending)`
  * `.alphabetical(order: .descending)`
  * `.alphabetical(order: .ascending)`
  * `.isRefined`

  ```swift Swift icon=code theme={"system"}
  // Tie-breaking algorithm where refined values are shown first.
  // If refined values are tied, show the facets with the largest counts.
  // If counts are tied, show facets in alphabetical order.
  facetListPresenter =
      FacetListPresenter(
        sortBy: [.isRefined, .count(order: .descending), .alphabetical(order: .ascending)]
      )
  ```
</ParamField>

<ParamField body="limit" type="Int" default={10}>
  The number of facet values to retrieve.

  ```swift Swift icon=code theme={"system"}
  facetListPresenter =  .init(limit: 5)
  ```
</ParamField>

<ParamField body="showEmptyFacets" type="Bool" default={true}>
  Whether to show facets with a facet count of 0.

  ```swift Swift icon=code theme={"system"}
  facetListPresenter =  .init(showEmptyFacets: false)
  facetListPresenter =  .init(showEmptyFacets: true)
  ```
</ParamField>

## Low-level API

If you want to fully control the `RefinementList` components and connect them manually, you can use the following components:

* `Searcher`. The [`Searcher`](/doc/api-reference/widgets/instantsearch/ios) that handles your searches.
* `FilterState`. The current state of the filters.
* `FacetListInteractor`. The logic applied to the facets.
* `FacetListController`. The controller that interfaces with a concrete facet list view.
* `FacetListPresenter`. Optional. The presenter that controls the sorting and other settings of the facet list view.

```swift Swift icon=code theme={"system"}
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourSearchOnlyAPIKey",
                            indexName: "YourIndexName")
let filterState: FilterState = .init()

let facetListInteractor: FacetListInteractor  = .init(facets: [.init(value: "initial facet", count: 10)], selectionMode: .multiple)

let categoryTableViewController: UITableViewController = .init()
let categoryListController: FacetListTableController = .init(tableView: categoryTableViewController.tableView)
let facetListPresenter: FacetListPresenter = .init(sortBy: [.count(order: .descending)], limit: 5, showEmptyFacets: false)

facetListInteractor.connectSearcher(searcher, with: "category")
facetListInteractor.connectFilterState(filterState, with: "category", operator: .or)
facetListInteractor.connectController(categoryListController)
facetListInteractor.connectController(categoryListController, with: facetListPresenter)
searcher.connectFilterState(filterState)

searcher.search()
```

## Customizing your view

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

If you want to use another component such as a `UICollectionView`,
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 `FacetListController` protocol.

### Protocol

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

Closure to call when a new facet is clicked.

`func setSelectableItems(selectableItems: [SelectableItem<Facet>])`

Function called when a new array of selectable facets is updated. This is the UI State of the refinement list.

`func reload()`

Function called when you want ti reload the list view.

### Implementation example

```swift Swift icon=code theme={"system"}
open class FacetListTableViewController: NSObject, FacetListController {

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

  public var tableView: UITableView

  var selectableItems: [SelectableItem<Facet>] = []
  var cellID: String

  public init(tableView: UITableView, cellID: String = "FacetList") {
    self.tableView = tableView
    self.cellID = cellID
    super.init()
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
    tableView.dataSource = self
    tableView.delegate = self
  }

  public func setSelectableItems(selectableItems: [SelectableItem<Facet>]) {
    self.selectableItems = selectableItems
  }

  public func reload() {
    tableView.reloadData()
  }

}

extension FacetListTableViewController: UITableViewDataSource {

  open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return selectableItems.count
  }

  open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)

    let selectableRefinement: SelectableItem<Facet> = selectableItems[indexPath.row]

    let facetAttributedString = NSMutableAttributedString(string: selectableRefinement.item.value)
    let facetCountStringColor = [NSAttributedString.Key.foregroundColor: UIColor.gray, .font: UIFont.systemFont(ofSize: 14)]
    let facetCountString = NSAttributedString(string: " (\(selectableRefinement.item.count))", attributes: facetCountStringColor)
    facetAttributedString.append(facetCountString)

    cell.textLabel?.attributedText = facetAttributedString

    cell.accessoryType = selectableRefinement.isSelected ? .checkmark : .none

    return cell
  }

}

extension FacetListTableViewController: UITableViewDelegate {

  open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectableItem = selectableItems[indexPath.row]

    self.onClick?(selectableItem.item)
  }

}
```

### SwiftUI

InstantSearch provides the `FacetList` SwiftUI view which you can embed in your views.
It uses `FacetListObservableController` as a data model,
which is an implementation of the `FacetListController` protocol adapted for usage with SwiftUI.
`FacetListObservableController` must be connected to the `FacetListConnector` or `FacetListInteractor`,
like any other `FacetListController` implementation.
You can define the appearance of the view representing a single facet and its selection state
or use the `FacetRow` view provided by InstantSearch.

```swift Swift icon=code theme={"system"}
struct ContentView: View {

  @ObservedObject var facetListController: FacetListObservableController

  var body: some View {
    FacetList(facetListController) { facet, isSelected in
      // Use the implementation provided by InstantSearch
      // FacetRow(facet: facet,
                  isSelected: isSelected)
      // Or declare a custom single facet view
      HStack {
        Text(facet.value)
        Spacer()
        if isSelected {
          Image(systemName: "checkmark")
            .foregroundColor(.accentColor)
        }
      }
      .contentShape(Rectangle())
      .frame(idealHeight: 44)
      .padding(.horizontal, 5)
    }
  }
}
```

If you prefer to create a custom SwiftUI view that presents the list of facets,
you can directly use the `FacetListObservableController` as a data model.
It provides `facets` and `selections` properties along with convenient `toggle` and `isSelected` functions to streamline the design process of your custom SwiftUI view.
