# SearchComboBox

A single-line input for **search-and-act** flows. The user types a query, chooses a suggestion (or optionally submits the raw text), an action fires, and the input clears for the next query. SearchComboBox does not represent a selected value and cannot be connected to a form.

## When to Use

- "Add item" inputs, resource launchers, or focused searches where every result choice is a discrete action
- Server-driven suggestions where results are fetched based on the query (use `filter={false}`)
- When you want the input to reset after every pick instead of showing the selected label
- When you optionally want to submit a free-form query that matches no suggestion

Choose a different component when the interaction has another outcome:

- Use [ComboBox](./ComboBox.md) when the input should keep one selected value and participate in a form.
- Use [FilterPicker](./FilterPicker.md) when a button should open a searchable list and keep one or more selected values.
- Use [CommandMenu](../actions/CommandMenu.md) for a searchable, keyboard-first palette of application commands, usually opened from a trigger or dialog.
- Use [CommandTextArea](./CommandTextArea.md) when suggestions insert tokens into multiline free-form text.
- Use [SearchInput](./SearchInput.md) when users submit free text without choosing suggestions.

## Component

---

### Properties

- **`children`** `ReactNode` — SearchComboBox.Item elements that define the available suggestions
- **`items`** `Iterable<T>` — Items for the listbox (alternative to children, enables data-driven mode)
- **`placeholder`** `string` — Placeholder text when the input is empty
- **`icon`** `ReactElement | null` — Icon rendered before the input. Defaults to a search icon; pass `null` to remove it
- **`inputValue`** `string` — The current input value in controlled mode
- **`defaultInputValue`** `string` — The default input value in uncontrolled mode
- **`emptyLabel`** `ReactNode` (default: context-aware `No results found` / `Loading…`) — Label shown when the list is empty. Once the list settles on a text label, not while `isLoading`, a polite status announces it to screen readers (an element label is shown but not announced), so a text label is on the page twice: a test finds it through the listbox's description (`toHaveAccessibleDescription`) rather than `getByText`
- **`filter`** `((textValue: string, inputValue: string) => boolean) | false` — Custom filter function, or `false` to disable internal filtering for external/server-side filtering
- **`isClearable`** `boolean` (default: `true`) — Whether the input is clearable via the clear button or Escape key
- **`hideTrigger`** `boolean` (default: `true`) — Whether the dropdown trigger button is hidden (search-field look)
- **`popoverTrigger`** `'focus' | 'input' | 'manual'` (default: `input`) — How the popover is triggered
- **`isLoading`** `boolean` (default: `false`) — Whether items are being loaded (e.g. from a server)
- **`loadingDelay`** `number` (default: `1000`) — Delay in ms before the loading indicator appears
- **`size`** `'small' | 'medium' | 'large'` (default: `medium`) — Size of the component
- **`listGap`** `number` — Space between options, in pixels. Defaults to a hairline (`$item-gap`, `1px`)
- **`disabledKeys`** `Iterable<Key>` — Keys of suggestions that cannot be selected
- **`onSelect`** `(key: string, textValue: string) => void` — Fired when a suggestion is picked; the input clears afterwards
- **`onSubmit`** `(value: string) => void` — Opt-in. Fired when Enter is pressed with no focused/available option; the input clears afterwards
- **`onInputChange`** `(value: string) => void` — Fired when the input text changes
- **`onClear`** `() => void` — Fired when the input is cleared
- **`onOpenChange`** `(isOpen: boolean) => void` — Fired when the popover opens or closes
- **`onFocus`** `() => void` — Fired when focus enters the component
- **`onBlur`** `() => void` — Fired when focus leaves the component
- **`onKeyDown`** `(e: KeyboardEvent) => void` — Fired when a key is pressed on the input, before the component's own key handling. Call `e.preventDefault()` to skip that handling for the key: a prevented `Enter` selects nothing and does not call `onSubmit`, a prevented `ArrowDown` does not open the popover. The event is React Aria's keyboard event. `Escape` and `Tab` go on to ancestors, so a surrounding `Dialog` still closes on `Escape` and moves focus on `Tab`, unless SearchComboBox acts on them or the handler prevents them. It acts on `Escape` while the list is on screen or the input has text: it closes the list, or clears the text, or, when `isClearable` is off, keeps the browser from clearing it, so the `Escape` that closes the dialog is the one pressed with the input empty. Other keys stop at the input unless the handler calls `e.continuePropagation()`
- **`prefix`** `ReactNode` — Content rendered before the input
- **`suffix`** `ReactNode` — Content rendered after the input
- **`suffixPosition`** `'before' | 'after'` (default: `before`) — Position of suffix relative to status icons
- **`direction`** `'top' | 'bottom'` (default: `bottom`) — Preferred direction for the dropdown
- **`shouldFlip`** `boolean` (default: `true`) — Whether the dropdown flips to fit in the viewport
- **`overlayOffset`** `number` (default: `8`) — Distance between input and dropdown in pixels
- **`containerPadding`** `number` (default: `8`) — Minimum padding between the popover and viewport edges
- **`inputProps`** `CubeSearchInputAttributes` — Attributes for the input, such as `enterKeyHint`, `autoCorrect` or `inputMode`. By default it sets `enterKeyHint="search"`, `autoCapitalize="none"`, `autoCorrect="off"` and `spellCheck={false}`, so a phone keyboard shows a Search key and neither capitalizes nor corrects a query; a value set here wins. It takes no event handlers, ARIA attributes, `value`, `type`, `role`, `id`, `placeholder`, `autoFocus`, `disabled`, `readOnly`, `className`, `style` or `autoComplete`: the component owns those, or takes them as props of its own

#### Notable Properties

**onSelect vs onSubmit** — `onSelect(key, textValue)` fires when a suggestion from the list is chosen. When the popover has visible suggestions, pressing Enter selects the highlighted one (or the first visible one when nothing is highlighted). `onSubmit(value)` is an opt-in escape hatch: only when there are no visible suggestions and the user presses Enter is the raw trimmed text submitted. Without `onSubmit`, Enter with no visible suggestion is a no-op. Both callbacks clear the input afterwards.

**filter** — By default the list is filtered client-side with a contains match. Pass a custom `(textValue, inputValue) => boolean`, or set `filter={false}` to disable internal filtering entirely and provide already-filtered `items` yourself (server-side filtering).

**isLoading + loadingDelay** — While `isLoading` is `true`, the previous results stay visible, and a list already on screen stays even when the load empties it, showing `Loading…` (or your `emptyLabel`), so a search doesn't make it blink out between keystrokes. The loading indicator, and a list that opens with no results while loading, only appear after `loadingDelay` has elapsed, so fast responses never flash a loading state.

### Base Properties

Supports [Base properties](../../BaseProperties.md)

### Field Properties

Supports the layout-related [Field properties](../../FieldProperties.md) (`label`, `description`, `message`, `labelPosition`, etc.). Note that SearchComboBox is not form-connected: it does not accept `name`/`rules` and does not participate in form state or validation.

### Styling Properties

#### styles

Customizes the root wrapper element of the SearchComboBox (the input container).

**Sub-elements:**

- `Prefix` - Container for prefix content and icons
- `Suffix` - Container for suffix content, status icons, and action buttons
- `InputIcon` - Container for the left icon

#### inputStyles

Customizes the text input element where users type.

#### triggerStyles

Customizes the dropdown trigger button (visible when `hideTrigger={false}`).

#### listBoxStyles

Customizes the dropdown list container that displays suggestions.

#### overlayStyles

Customizes the popover overlay wrapper that contains the list.

By default the popover is as wide as its options, long labels and descriptions included. It is never narrower than the field, and never wider than half the viewport or `60x`, whichever is larger, nor than the viewport less a margin. It does not narrow while open, as the list scrolls or the input filters it. A `width` here replaces that rule.

#### optionStyles

Customizes individual option items in the dropdown list.

#### sectionStyles

Customizes section containers when using grouped suggestions.

#### headingStyles

Customizes section heading elements within grouped suggestions.

### Style Properties

These properties allow direct style application without using the `styles` prop:

- **Position:** `gridArea`, `order`, `gridColumn`, `gridRow`, `placeSelf`, `alignSelf`, `justifySelf`, `zIndex`, `margin`, `blockMargin`, `inlineMargin`, `inset`, `blockInset`, `inlineInset`, `position`, `scrollMargin`, `blockScrollMargin`, `inlineScrollMargin`, `scrollPadding`, `blockScrollPadding`, `inlineScrollPadding`
- **Dimension:** `width`, `height`, `blockSize`, `inlineSize`, `flexBasis`, `flexGrow`, `flexShrink`, `flex`

### Modifiers

The `mods` property accepts the following modifiers you can override:

- **`invalid`** `boolean` — Applied when validation state is invalid
- **`valid`** `boolean` — Applied when validation state is valid
- **`disabled`** `boolean` — Applied when the component is disabled
- **`readonly`** `boolean` — Applied when the component is read-only: a recessed fill and a softer border, except in high contrast
- **`focused`** `boolean` — Applied when the input has focus
- **`loading`** `boolean` — Applied when the loading indicator is visible
- **`prefix`** `boolean` — Applied when prefix content is present
- **`suffix`** `boolean` — Applied when suffix content is present
- **`clearable`** `boolean` — Applied when the clear button is visible

## Examples

### Clears on Select

```jsx
<SearchComboBox
  label="Add a fruit"
  placeholder="Search a fruit..."
  onSelect={(key, textValue) => addFruit(key, textValue)}
>
  <SearchComboBox.Item key="apple">Apple</SearchComboBox.Item>
  <SearchComboBox.Item key="banana">Banana</SearchComboBox.Item>
</SearchComboBox>
```

### Submit Free-form Text

```jsx
<SearchComboBox
  label="Search or add a tag"
  placeholder="Type and press Enter..."
  onSubmit={(value) => addTag(value)}
>
  <SearchComboBox.Item key="apple">Apple</SearchComboBox.Item>
  <SearchComboBox.Item key="banana">Banana</SearchComboBox.Item>
</SearchComboBox>
```

### Custom Empty Label

```jsx
<SearchComboBox
  label="Search a fruit"
  emptyLabel="No fruit matches your search"
>
  <SearchComboBox.Item key="apple">Apple</SearchComboBox.Item>
  <SearchComboBox.Item key="banana">Banana</SearchComboBox.Item>
</SearchComboBox>
```

### Server-side Filtering

Set `filter={false}` and drive `items` from your own (debounced) fetch. Toggle `isLoading` while the request is in flight — the indicator only appears if it takes longer than `loadingDelay`.

```jsx
const [value, setValue] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [results, setResults] = useState([]);

useEffect(() => {
  if (!value.trim()) {
    setResults([]);
    return;
  }
  setIsLoading(true);
  const handle = setTimeout(async () => {
    setResults(await fetchCountries(value));
    setIsLoading(false);
  }, 400); // debounce is your responsibility
  return () => clearTimeout(handle);
}, [value]);

<SearchComboBox
  filter={false}
  label="Country"
  placeholder="Search countries..."
  inputValue={value}
  isLoading={isLoading}
  items={results}
  emptyLabel={
    !value.trim()
      ? 'Type to search'
      : isLoading
        ? 'Searching…'
        : 'No countries found'
  }
  onInputChange={setValue}
  onSelect={(key) => selectCountry(key)}
>
  {(item) => <SearchComboBox.Item key={item.key}>{item.name}</SearchComboBox.Item>}
</SearchComboBox>
```

## Accessibility

### Keyboard Navigation

- `Tab` - Moves focus to/from the input
- `ArrowDown` - Opens the popover (if closed) or moves focus to the next suggestion. A disabled option is reached too, as WAI-ARIA keeps it focusable, so a screen reader says why it is unavailable; Enter ignores it, and the list never highlights one on its own
- `ArrowUp` - Opens the popover (if closed) or moves focus to the previous suggestion
- `Enter` - Selects the highlighted suggestion, falling back to the first suggestion the user can pick; when there is none, submits the raw text via `onSubmit`. On a disabled suggestion the arrow keys reached, it does nothing
- `Escape` - Closes the popover (if open) or clears the input (if clearable and popover closed). A popover a load still holds back, before `loadingDelay` has passed, counts as closed: `Escape` cancels it and clears the input, or goes on to a surrounding dialog when the input is empty
- `Home` / `End` - Moves focus to the first / last suggestion (when the popover is open)
- Typing - Filters suggestions based on the input value

An `onKeyDown` handler runs before all of these and can skip any of them for a key by calling `e.preventDefault()`.

### ARIA Properties

- `role="combobox"` - Identifies the input as a combobox
- `aria-expanded` - Indicates whether the popover is open
- `aria-haspopup="listbox"` - Indicates the combobox has a listbox popup
- `aria-controls` - References the listbox ID when the popover is open
- `aria-activedescendant` - References the currently focused suggestion
- `aria-label` - Provides an accessible label when no visible label exists

## Best Practices

1. **Do**: Debounce server requests before updating `items`

   ```jsx
   <SearchComboBox filter={false} isLoading={isLoading} items={results} />
   ```

2. **Do**: Provide a meaningful `emptyLabel` for both the empty and loading states

   ```jsx
   <SearchComboBox emptyLabel={query ? 'No results' : 'Type to search'} />
   ```

3. **Don't**: Use SearchComboBox to capture a persistent value in a form — use [ComboBox](./ComboBox.md) instead

## Related Components

- [ComboBox](./ComboBox.md) - Use for an input-based searchable field that keeps one selected value
- [FilterPicker](./FilterPicker.md) - Use when searched items become persistent single or multiple field values
- [CommandMenu](../actions/CommandMenu.md) - Use for searchable, keyboard-first application commands in a popover or dialog
- [CommandTextArea](./CommandTextArea.md) - Use when suggestions insert tokens into multiline free-form text
- [SearchInput](./SearchInput.md) - Use for free-text search without suggestions
- [ListBox](./ListBox.md) - The underlying list component used in the popover
