# Dropdown

> A Dropdown widget that allows users to pick an item from a list or search by typing

Dropdown widgets are [Input Fields](https://golemui.com/dx/widgets-reference/input-fields/overview.md) that provide a searchable list of items. Users can either type in the input to filter the list or select an item directly from the interactive dropdown menu.

The simplest way to populate `items` is a flat array of strings or numbers — the displayed label and the stored value are the same. When you need a separate stored value (e.g. an id behind a human-readable name), pass an array of objects and tell the widget which keys to use via [`labelField`](#using-objects) and [`valueField`](#using-objects).


```ts title="dropdown.ts"

export default [
  gui.inputs.dropdown('selection', {
    items: ['Apple', 'Banana', 'Cherry'],
    placeholder: 'Type to search...',
    label: 'Select an Item',
    uid: 'dropdown_id',
  }),
];
```

_Live demo on the docs page. Form definition: [dropdown.ts](https://golemui.com/template/widgets-dx/dropdown/dropdown.ts)._

## Props

Use these `props` to customize your Dropdown widget.

| prop              | type                  | description                                                                                                                         |
| ----------------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `items`           | array                 | **Required**. Array of strings, numbers, or plain objects. See [Using Objects](#using-objects).                                     |
| `autocomplete`    | string                | `'on'`, `'off'`, or a space-separated list of W3C autofill tokens                                                                   |
| `height`          | number                | Height of the dropdown list container                                                                                               |
| `hint`            | string                | A description to display below the input                                                                                            |
| `icon`            | string                | A css class to display an icon inside the input, on the left                                                                        |
| `inputDebounce`   | number                | Delay in milliseconds before triggering a search/filter. Defaults to `500`                                                          |
| `itemHeight`      | number                | Height of each individual item in the list                                                                                          |
| `itemRenderer`    | string                | Key of a registered renderer used to draw each item. See [Item Renderers](https://golemui.com/dx/features/item-renderers.md)                                |
| `labelField`      | string                | When items are objects, the key whose value is shown to the user. Defaults to `label`.                                              |
| `placeholder`     | string                | Text shown in the input when it is empty                                                                                            |
| `searchFields`    | array                 | List of fields to search on when filtering the list                                                                                 |
| `toggleAriaLabel` | string \| Localizable | Aria label for the panel toggle button. Defaults to `Show options`                                                                  |
| `valueField`      | string                | When items are objects, the key whose value is stored in the form. Defaults to `value`. Required for selection to emit a primitive. |

### Using Objects

The top-of-page demo uses an array of strings — each entry is both what the user sees and what gets stored. That's enough for many forms, but it falls short when you need to keep a stable id behind a human-readable label (e.g. a country code paired with a country name).

For that case, pass `items` as plain objects and use `labelField` and `valueField` to tell the widget which key holds the displayed text and which one holds the stored value. The example below pairs each country `name` with a short `code`, and `searchFields` lets the user filter by either.


```ts title="dropdown.ts"

export default [
  gui.inputs.dropdown('selection', {
    items: [
      {
        name: 'USA',
        code: 'US',
      },
      {
        name: 'Canada',
        code: 'CA',
      },
    ],
    labelField: 'name',
    valueField: 'code',
    searchFields: ['name', 'code'],
    label: 'Custom Fields',
    uid: 'dropdown_custom',
  }),
];
```

_Live demo on the docs page. Form definition: [dropdown-custom.ts](https://golemui.com/template/widgets-dx/dropdown/dropdown-custom.ts)._

If your objects already use the default key names (`label` and `value`), you can omit `labelField` and `valueField` entirely. Otherwise both should be set so selection can extract the right primitive.

### Hint

<Aside>A `label` is required for the `hint` to be displayed.</Aside>

Use the property `hint` to provide instructions for the user.


```ts title="dropdown.ts"

export default [
  gui.inputs.dropdown('selection', {
    items: ['Apple', 'Banana'],
    hint: 'Start typing to filter the available items',
    label: 'Select an Item',
    uid: 'dropdown_hint',
  }),
];
```

_Live demo on the docs page. Form definition: [dropdown-hint.ts](https://golemui.com/template/widgets-dx/dropdown/dropdown-hint.ts)._

### Icon

Use the property `icon` to add a visual indicator inside the input, on the left side. The value is a CSS class string, the same convention Select and Textinput use. The arrow button at the right edge opens and closes the panel without moving focus out of the input; label it for assistive technology with `toggleAriaLabel`.


```ts title="dropdown.ts"

export default [
  gui.inputs.dropdown('language', {
    icon: 'language',
    placeholder: 'Type to search...',
    items: ['English', 'Spanish', 'French', 'German', 'Portuguese'],
    label: 'Language',
  }),
];
```

_Live demo on the docs page. Form definition: [dropdown-icon.ts](https://golemui.com/template/widgets-dx/dropdown/dropdown-icon.ts)._

### Item Renderer

Use the property `itemRenderer` to draw each row with a registered renderer instead of the default template. The key must match one of the renderers registered on the form host — see [Item Renderers](https://golemui.com/dx/features/item-renderers.md) for the full setup.

This demo uses the registered `countryItemRenderer` to display each country with its flag.


```ts title="dropdown.ts"

export default [
  gui.inputs.dropdown('country', {
    labelField: 'label',
    valueField: 'id',
    searchFields: ['label'],
    itemRenderer: 'countryItemRenderer',
    items: [
      {
        id: 'AU',
        flag: '🇦🇺',
        label: 'Australia',
      },
      {
        id: 'BR',
        flag: '🇧🇷',
        label: 'Brazil',
      },
      {
        id: 'CA',
        flag: '🇨🇦',
        label: 'Canada',
      },
      {
        id: 'FR',
        flag: '🇫🇷',
        label: 'France',
      },
      {
        id: 'DE',
        flag: '🇩🇪',
        label: 'Germany',
      },
      {
        id: 'JP',
        flag: '🇯🇵',
        label: 'Japan',
      },
      {
        id: 'ES',
        flag: '🇪🇸',
        label: 'Spain',
      },
      {
        id: 'US',
        flag: '🇺🇸',
        label: 'United States',
      },
      {
        id: 'GB',
        flag: '🇬🇧',
        label: 'United Kingdom',
      },
    ],
    label: 'Pick a country',
    uid: 'dropdown_item_renderer',
  }),
];
```

_Live demo on the docs page. Form definition: [dropdown-item-renderer.ts](https://golemui.com/template/widgets-dx/dropdown/dropdown-item-renderer.ts)._

### Size

Use `height` to set the size of the dropdown's scrollable list, and `itemHeight` to fix the row height used by the virtual scroller. Tune `itemHeight` whenever you use a custom `itemRenderer` whose rows are taller than the default.


```ts title="dropdown.ts"

export default [
  gui.inputs.dropdown('selection', {
    hint: 'Open the dropdown to see the taller list with larger rows',
    height: 200,
    itemHeight: 56,
    items: ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'],
    label: 'Pick a number',
    uid: 'dropdown_size',
  }),
];
```

_Live demo on the docs page. Form definition: [dropdown-size.ts](https://golemui.com/template/widgets-dx/dropdown/dropdown-size.ts)._

### Input Debounce

Use `inputDebounce` to control how long the dropdown waits after each keystroke before applying the filter. Useful when the `filter` event triggers a server-side search.


```ts title="dropdown.ts"

export default [
  gui.inputs.dropdown('fruit', {
    hint: 'Filtering waits 800ms after the last keystroke',
    inputDebounce: 800,
    items: ['Apple', 'Apricot', 'Banana', 'Blueberry', 'Cherry', 'Grapefruit', 'Mango', 'Orange', 'Peach', 'Pineapple'],
    label: 'Pick a fruit',
    uid: 'dropdown_debounce',
  }),
];
```

_Live demo on the docs page. Form definition: [dropdown-debounce.ts](https://golemui.com/template/widgets-dx/dropdown/dropdown-debounce.ts)._

### Autocomplete

Use `autocomplete` to set the underlying input's `autocomplete` attribute. Accepts `'on'`, `'off'`, or a space-separated list of [W3C autofill tokens](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill).


```ts title="dropdown.ts"

export default [
  gui.inputs.dropdown('country', {
    autocomplete: 'country-name',
    labelField: 'name',
    valueField: 'code',
    searchFields: ['name'],
    items: [
      {
        name: 'France',
        code: 'FR',
      },
      {
        name: 'Germany',
        code: 'DE',
      },
      {
        name: 'Spain',
        code: 'ES',
      },
      {
        name: 'United Kingdom',
        code: 'GB',
      },
      {
        name: 'United States',
        code: 'US',
      },
    ],
    label: 'Country',
    uid: 'dropdown_autocomplete',
  }),
];
```

_Live demo on the docs page. Form definition: [dropdown-autocomplete.ts](https://golemui.com/template/widgets-dx/dropdown/dropdown-autocomplete.ts)._

## Styling

Dropdowns can be styled as explained in the [Styling Guide](https://golemui.com/dx/styling/overview.md).

### CSS Variables

Dropdowns leverage standard container and list variables.

| CSS Variable           | Description                     |
| ---------------------- | ------------------------------- |
| `--gui-radius-md`      | Border radius for the container |
| `--gui-border-default` | Default border color            |

### Anatomy

This is the anatomy of the Dropdown Widget in case you want to use your CSS styles.


```html title="dropdown-anatomy.html"
<gui-dropdown-input class="gui-dropdown gui-field">
  <gui-label>
    <span class="gui-label" id="dropdown_id_label">
      <span class="gui-label__text">Select an Item</span>
    </span>
  </gui-label>
  <div class="gui-widget">
    <input
      type="text"
      role="combobox"
      aria-autocomplete="list"
      id="dropdown_id"
      class="gui-widget-input"
      placeholder="Type to search..."
      aria-expanded="true"
      aria-controls="dropdown_id-list"
      aria-labelledby="dropdown_id_label"
    />
    <button
      type="button"
      class="gui-dropdown__arrow"
      aria-haspopup="listbox"
      aria-label="Show options"
      aria-expanded="true"
      aria-controls="dropdown_id-list"
    >
      <span class="gui-caret" aria-hidden="true"></span>
    </button>
    <div class="gui-picker__panel" popover="manual">
      <gui-list id="dropdown_id-list" role="listbox" tabindex="0">
        <div
          role="option"
          tabindex="-1"
          class="gui-list__item-wrapper"
          id="dropdown_id-item-0"
          aria-selected="false"
          aria-disabled="false"
          style="height: 40px"
        >
          <div class="gui-list__item">Apple</div>
        </div>
        <div
          role="option"
          tabindex="-1"
          class="gui-list__item-wrapper"
          id="dropdown_id-item-1"
          aria-selected="false"
          aria-disabled="false"
          style="height: 40px"
        >
          <div class="gui-list__item">Banana</div>
        </div>
        <div
          role="option"
          tabindex="-1"
          class="gui-list__item-wrapper"
          id="dropdown_id-item-2"
          aria-selected="false"
          aria-disabled="false"
          style="height: 40px"
        >
          <div class="gui-list__item">Cherry</div>
        </div>
      </gui-list>
    </div>
  </div>
</gui-dropdown-input>
```
