# List

> A virtualized List widget for displaying and selecting from large sets of data

List widgets are [Input Fields](https://golemui.com/dx/widgets-reference/input-fields/overview.md) designed to handle selection from potentially thousands of items efficiently. They use virtualization to only render the items currently visible in the viewport, ensuring high performance regardless of the total data volume.

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="list.ts"

export default [
  gui.inputs.list('selection', {
    items: ['Option 1', 'Option 2', 'Option 3', 'Option 4', 'Option 5'],
    height: 200,
    itemHeight: 40,
    label: 'Pick an option',
    uid: 'list_id',
  }),
];
```

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

## Props

Use these `props` to customize your List widget.

| prop           | type   | description                                                                                                                         |
| -------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------- |
| `items`        | array  | **Required**. Array of strings, numbers, or plain objects. See [Using Objects](#using-objects).                                     |
| `height`       | number | Total height of the scrollable viewport. Defaults to `300`                                                                          |
| `hint`         | string | A description to display below the list                                                                                             |
| `itemHeight`   | number | Fixed height of each item for virtualization logic. Defaults to `40`                                                                |
| `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`.                                              |
| `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 when label and value are identical, but lists are often backed by domain data where the human-readable name and the stored identifier differ.

For that case, pass `items` as plain objects and use `labelField` and `valueField` to tell the list which key holds the displayed text and which one holds the stored value.


```ts title="list.ts"

export default [
  gui.inputs.list('selection', {
    height: 200,
    itemHeight: 40,
    labelField: 'name',
    valueField: 'code',
    items: [
      {
        name: 'USA',
        code: 'US',
      },
      {
        name: 'Canada',
        code: 'CA',
      },
      {
        name: 'Mexico',
        code: 'MX',
      },
      {
        name: 'Brazil',
        code: 'BR',
      },
      {
        name: 'Argentina',
        code: 'AR',
      },
    ],
    label: 'Custom Fields',
    uid: 'list_custom',
  }),
];
```

_Live demo on the docs page. Form definition: [list-custom.ts](https://golemui.com/template/widgets-dx/list/list-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 additional context for the list.


```ts title="list.ts"

export default [
  gui.inputs.list('selection', {
    items: ['Option A', 'Option B'],
    hint: 'Please select one of the available choices from the list.',
    label: 'Pick an option',
    uid: 'list_hint',
  }),
];
```

_Live demo on the docs page. Form definition: [list-hint.ts](https://golemui.com/template/widgets-dx/list/list-hint.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 `complexListItemRenderer` to display a title and description per row. Bump `itemHeight` to match the renderer's row height so virtualization stays accurate.


```ts title="list.ts"

export default [
  gui.inputs.list('selection', {
    height: 240,
    itemHeight: 60,
    labelField: 'title',
    valueField: 'value',
    itemRenderer: 'complexListItemRenderer',
    items: [
      {
        value: 'blog',
        title: 'Blog',
        description: 'A minimalist Markdown blog with RSS feed and dark mode.',
      },
      {
        value: 'docs',
        title: 'Documentation Site',
        description: 'Versioned docs with full-text search and a live playground.',
      },
      {
        value: 'dashboard',
        title: 'Admin Dashboard',
        description: 'Data tables, charts, and role-based auth out of the box.',
      },
      {
        value: 'storefront',
        title: 'E-commerce Storefront',
        description: 'Product catalog, cart, and Stripe checkout wired up.',
      },
      {
        value: 'landing',
        title: 'Marketing Landing',
        description: 'Hero, pricing tiers, and a contact form ready to ship.',
      },
      {
        value: 'portfolio',
        title: 'Developer Portfolio',
        description: 'Showcase projects, embed demos, and host a personal blog.',
      },
    ],
    label: 'Pick a starter template',
    uid: 'list_item_renderer',
  }),
];
```

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

### Size

Use `height` to set the scrollable viewport height (default `300`) and `itemHeight` to set each row's height (default `40`). Both are in pixels; the list virtualizes and scrolls when the items exceed the viewport.


```ts title="list.ts"

export default [
  gui.inputs.list('selection', {
    hint: 'A shorter 200px viewport with taller 56px rows',
    height: 200,
    itemHeight: 56,
    items: ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'],
    label: 'Pick an option',
    uid: 'list_size',
  }),
];
```

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

## Styling

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

### CSS Variables

Following you will find a list with the CSS Variables and a quick description of what you will style.

| CSS Variable                 | Description                             |
| ---------------------------- | --------------------------------------- |
| `--gui-bg-default`           | Background color for the list container |
| `--gui-border-default`       | Border color for the list container     |
| `--gui-radius-md`            | Border radius for the list container    |
| `--gui-intent-primary`       | Background color for the selected item  |
| `--gui-intent-primary-hover` | Background color for items on hover     |
| `--gui-bg-disabled`          | Background color for disabled items     |

### Anatomy

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


```html title="list-anatomy.html"
<gui-list-input class="gui-list gui-field">
  <gui-label>
    <span class="gui-label" id="fieldUid_label">
      <span class="gui-label__text">Pick an option</span>
    </span>
  </gui-label>

  <div class="gui-widget">
    <gui-list aria-labelledby="fieldUid_label" role="listbox" tabindex="0">
      <div
        role="option"
        tabindex="-1"
        class="gui-list__item-wrapper"
        id="fieldUid-item-0"
        aria-selected="true"
        aria-disabled="false"
        style="height: 40px"
      >
        <div class="gui-list__item gui-list__item-selected">Option 1</div>
      </div>

      <div
        role="option"
        tabindex="-1"
        class="gui-list__item-wrapper"
        id="fieldUid-item-1"
        aria-selected="false"
        aria-disabled="false"
        style="height: 40px"
      >
        <div class="gui-list__item">Option 2</div>
      </div>

      <div
        role="option"
        tabindex="-1"
        class="gui-list__item-wrapper"
        id="fieldUid-item-2"
        aria-selected="false"
        aria-disabled="false"
        style="height: 40px"
      >
        <div class="gui-list__item">Option 3</div>
      </div>

      <div
        role="option"
        tabindex="-1"
        class="gui-list__item-wrapper"
        id="fieldUid-item-3"
        aria-selected="false"
        aria-disabled="false"
        style="height: 40px"
      >
        <div class="gui-list__item">Option 4</div>
      </div>

      <div
        role="option"
        tabindex="-1"
        class="gui-list__item-wrapper"
        id="fieldUid-item-4"
        aria-selected="false"
        aria-disabled="false"
        style="height: 40px"
      >
        <div class="gui-list__item">Option 5</div>
      </div>
    </gui-list>
  </div>
</gui-list-input>
```
