---
title: "Recipe: a combobox"
description: "Filter options while a field keeps focus and names its active option."
modificationDate: "2026-11-14"
---

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

# Recipe: a combobox

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/combobox: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

Filter options while the field keeps focus.

```prompt Add a field that filters options as I type
Add a combobox to the Exact app in this folder: a field that filters a list of options while it keeps focus. Use https://github.com/expo/exact/blob/main/docs/recipes/combobox.md as the source for each step. The Exact checkout has the same page at docs/recipes/combobox.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If I did not say which field, where its options come from and what the app does with the chosen option, propose it from the app and ask me before you write code.
3. In app.contract, keep the query, whether the list is open, the index of the active option and the picked option in states, and filter the options in a derive that ignores case. An edit sets the query, resets the index and opens the list. Focus opens the list and blur closes it. A choice sets the picked option and the query, and closes the list.
4. Draw an `input role="combobox"` with an `aria-label`, `aria-expanded`, `aria-controls` that names the listbox, and `aria-activedescendant` that names the active option, or `""` for none. While the list is open and has options, draw a `scroll` with a `max-height` that holds a `column role="listbox"` of `button role="option"`, each with `aria-selected`, `tabindex=-1` and `retainFocus=true`, so a click keeps focus in the field.
5. Handle keys in the input's `key` action, and ignore them while `e.isComposing`. `ArrowDown`, `ArrowUp`, `Home` and `End` move the active option, call `preventDefault()`, and bring the option into view with `scrollIntoView(<id>, block="nearest")`. `Enter` chooses the active option while the list is open. `Escape` closes the list. Tab leaves the field. Actions read old state: after you assign the index, use a `let` value.
6. Add a test in app.test.contract like the recipe's: type a query, move with `type "<id>" key "ArrowDown"`, press `Enter`, and expect the picked option and `expect tree missing` for the listbox. Then expect `Escape` and `Tab` to close the list.
7. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 8) and report it; do not keep trying.
8. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
9. Tell me which field changed and where its options come from, what the tests and checks reported, and the launch command for each target platform. Only the web exposes `aria-activedescendant`.
```

```contract
component Combobox
  state query = ""
  state open = false
  state chosen = 0
  state picked = ""
  derive hits = ["Apricot", "Apple", "Pear", "Plum"].filter(name => name.toLowerCase().includes(query.toLowerCase()))
  action edit(value: string)
    query = value
    chosen = 0
    open = true
  action began
    open = true
  action ended
    open = false
  action choose(name: string)
    picked = name
    query = name
    open = false
    chosen = 0
  action searchKey(k: string, e: KeyboardEvent)
    if not e.isComposing
      if hits.length > 0 and (k == "ArrowDown" or k == "ArrowUp" or k == "Home" or k == "End")
        preventDefault()
        let next = k == "Home" ? 0 : k == "End" ? hits.length - 1 : k == "ArrowDown" ? min(chosen + 1, hits.length - 1) : max(chosen - 1, 0)
        chosen = next
        open = true
        match hits.at(next)
          case some(name)
            scrollIntoView(`option-${name}`, block="nearest")
          case none
            chosen = 0
      else if k == "Enter" and open
        match hits.at(chosen)
          case some(name)
            preventDefault()
            choose(name)
          case none
            chosen = 0
      else if k == "Escape"
        preventDefault()
        open = false
        chosen = 0
  view
    column
      input id="query" testId="query" role="combobox" aria-label="Fruit" value=query input=edit key=searchKey focus=began blur=ended
        aria-expanded=(open and hits.length > 0) aria-controls="options"
        aria-activedescendant=(open ? (match hits.at(chosen) { case some(name) => `option-${name}`, case none => "" }) : "")
      if open and hits.length > 0
        scroll max-height="50vh"
          column id="options" role="listbox" aria-label="Fruit" testId="options"
            for name, i in hits key=name
              button id=`option-${name}` testId=`option-${name}` role="option" aria-selected=(i == chosen) tabindex=-1 retainFocus=true press=choose(name)
                text name
      text picked testId="picked"
      button "Continue"
```

```contract-test
test "filter, choose, close, and Tab out"
  tap "query"
  type "query" "ap"
  type "query" key "ArrowDown"
  expect state chosen == 1
  type "query" key "End"
  expect state chosen == 1
  type "query" key "ArrowUp"
  expect state chosen == 0
  type "query" key "Home"
  type "query" key "ArrowDown"
  type "query" key "Enter"
  expect text "picked" == "Apple"
  expect tree missing "options"
  type "query" "p"
  type "query" key "Escape"
  expect tree missing "options"
  type "query" "pl"
  expect tree has "option-Plum"
  type "query" key "Tab"
  expect state open == false
```

- Options use `tabindex=-1`; `retainFocus=true` keeps a click in the field. Tab leaves; blur closes the list.
- `aria-controls` names the listbox; `aria-activedescendant` names the active option, or is empty for none. Only the web exposes it.
- Use `next` after assigning state: actions read old state.
