# MultiColumn ComboBox

> A multi-column lookup as Combobox columns - a lightweight grid popup with column widths, alignment, formats, templates, sorting and multi-field search.

Canonical: https://salmonui.dev/docs/dropdowns/multicolumn-combobox

`Combobox columns={[...]}` turns the popup into a `role="grid"` table on the
shared fixed-height virtual window. It is not an embedded
`DataGrid`: the grid engine, editing and grouping would miss the open budget
of a lookup popup. The grid popup is a lazy chunk loaded on first open.

```tsx
import { Combobox } from '@salmonui/dropdowns/combobox';
import '@salmonui/dropdowns/combobox/styles.css';

<Combobox
  aria-label="Person"
  data={people}
  getItemValue={(p) => p.id}
  getItemLabel={(p) => p.name}
  columns={[
    { field: 'id', header: 'ID', width: 60, align: 'end' },
    { field: 'name', header: 'Name', width: '35%' },
    { field: 'joined', header: 'Joined', format: { dateStyle: 'medium' } },
    { field: 'active', header: 'Active', displayAsCheckbox: true },
  ]}
  searchFields={['name', 'city']}
/>
```

### Live demo: MultiColumn ComboBox

Live demo: https://salmonui.dev/demos/dropdowns/multicolumn-combobox

A Combobox whose popup is a grid with sortable headers, aligned, formatted and checkbox columns over 5,000 rows, searched by name and city.

`Demo.tsx`:

```tsx
'use client';

import { Combobox, type ComboboxColumn } from '@salmonui/dropdowns/combobox';

interface Person {
  readonly id: number;
  readonly name: string;
  readonly city: string;
  readonly age: number;
  readonly active: boolean;
}

const CITIES = ['Zürich', 'São Paulo', 'Kraków', 'Reykjavík', 'Montréal', 'Málaga', 'Lyon', 'Bern'];
const FIRST = ['Ana', 'Ben', 'Chloé', 'Dmitri', 'Elif', 'Farah', 'Gustav', 'Hana', 'Ivan', 'Jun', 'Kofi', 'Lucía', 'Mateus', 'Nora', 'Oskar', 'Priya', 'Quentin', 'Rosa', 'Søren', 'Tomás'];
const LAST = ['Novak', 'Okafor', 'Dubois', 'Petrov', 'Şahin', 'Haddad', 'Lindqvist', 'Sato', 'Horvat', 'Kim', 'Mensah', 'García', 'Costa', 'Berg', 'Nowak', 'Iyer', 'Moreau', 'Ortega', 'Jensen', 'Ruiz', 'Fischer', 'Rossi', 'Tanaka'];
// Deterministic names: 20 first names x 23 last names, so neighbouring ids differ in both.
const PEOPLE: Person[] = Array.from({ length: 5_000 }, (_, i) => ({ id: i + 1, name: `${FIRST[i % FIRST.length]} ${LAST[i % LAST.length]}`, city: CITIES[i % CITIES.length] as string, age: 20 + (i % 50), active: i % 3 !== 0 }));

const COLUMNS: ComboboxColumn<Person>[] = [
  { field: 'id', header: 'ID', width: 60, align: 'end' },
  { field: 'name', header: 'Name', width: '30%' },
  { field: 'city', header: 'City' },
  { field: 'age', header: 'Age', width: 70, align: 'end', format: { maximumFractionDigits: 0 } },
  { field: 'active', header: 'Active', width: 80, align: 'center', displayAsCheckbox: true },
];

// MultiColumn ComboBox: a grid popup with sortable headers over 5,000 rows,
// searching the name and city columns.
export default function MulticolumnComboboxDemo() {
  return (
    <div style={{ display: 'grid', gap: 4, maxWidth: 520 }}>
      <span id="demo-people-label">Person</span>
      <Combobox
        aria-labelledby="demo-people-label"
        data={PEOPLE}
        columns={COLUMNS}
        getItemValue={(p) => p.id}
        getItemLabel={(p) => p.name}
        searchFields={['name', 'city']}
        searchOperator="contains"
        ignoreAccents
        striped
        gridLines="horizontal"
        sorting={{ multiple: true }}
      />
    </div>
  );
}
```

| Task | How |
| --- | --- |
| Column data and title | `field`, `header` |
| Column width (px or %) | `width`: a number (px) or `'40%'`; columns without a width share the rest |
| Alignment | `align: 'start' \| 'end' \| 'center'` |
| Number and date formats | `Intl.NumberFormatOptions` (numbers), `Intl.DateTimeFormatOptions` (dates) in `locale`, or `(value, item) => string` |
| Cell and header templates | `renderCell({ item, value, text })`, `renderHeader({ column })` |
| Booleans as checkboxes | `displayAsCheckbox` |
| Cell attributes | `cellProps`: an object or `(item) => props` with `className`, `style`, `data-*` |
| Row height | `rowHeight` |
| Grid lines | `gridLines`: `'default'`, `'both'`, `'none'`, `'horizontal'`, `'vertical'` |
| Alternate row shading | `striped` |
| Wrap long text | One row height; long text ends in an ellipsis with the full text as `title` |
| Resize columns | Widths come from `columns` |
| Sort by a column | `sorting` (default `true`): header buttons cycle ascending, descending, none with `aria-sort`; `sortable: false` on a column turns it off there |
| Sort by several columns | `sorting={{ multiple: true }}`: Ctrl / Cmd + click adds a level |
| Initial sort | `sorting={{ sortBy: [{ columnId: 'name', direction: 'asc' }] }}` (reactive) |
| Search | Typing searches `searchFields` (default: the label) |

Keys are the Combobox keys with rows as the navigable unit, no cell
navigation. ARIA: the input has `aria-haspopup="grid"`,
`aria-controls` and `aria-activedescendant` on the active `row`; rows carry
`aria-selected` and `aria-rowindex`, the grid `aria-rowcount`. The header
row stays visible while the rows scroll.

### Live demo: MultiColumn ComboBox templates and remote rows

Live demo: https://salmonui.dev/demos/dropdowns/multicolumn-templates

Cell and header templates, per-cell props, a taller fixed row height, a reactive initial sort, row keyboard navigation with grid ARIA, and range reads over 100,000 remote rows.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Combobox, type ComboboxColumn, type ComboboxSort } from '@salmonui/dropdowns/combobox';
import { EventLog, useEventLog } from '../../components/demo-kit';
import { createDemoListSource } from '../../lib/demo-list-source';

interface Product {
  readonly id: number;
  readonly name: string;
  readonly category: string;
  readonly price: number;
  readonly stock: number;
}

const CATEGORIES = ['Audio', 'Cameras', 'Laptops', 'Phones', 'Tablets', 'Wearables'];
const PRODUCTS: Product[] = Array.from({ length: 60 }, (_, i) => ({
  id: 1001 + i,
  name: `${CATEGORIES[i % CATEGORIES.length]} model ${String.fromCharCode(65 + (i % 26))}${i + 1}`,
  category: CATEGORIES[i % CATEGORIES.length] as string,
  price: 49 + ((i * 37) % 900),
  stock: (i * 13) % 40,
}));

const COLUMNS: ComboboxColumn<Product>[] = [
  { field: 'id', header: 'SKU', width: 70, align: 'end' },
  {
    field: 'name',
    header: 'Product',
    width: '40%',
    renderCell: ({ item, text }) => (
      <span>
        <strong>{text}</strong>
        <br />
        <small>{item.category}</small>
      </span>
    ),
  },
  { field: 'price', header: 'Price', align: 'end', format: { style: 'currency', currency: 'EUR' } },
  {
    field: 'stock',
    header: 'Stock',
    width: 80,
    align: 'end',
    renderHeader: ({ column }) => <abbr title="Units in the warehouse">{column.header}</abbr>,
    // Low-stock cells get a class and a style.
    cellProps: (item) => (item.stock < 5 ? { className: 'demo-low-stock', style: { color: 'var(--salmon-color-danger-text)', fontWeight: 600 } } : undefined),
  },
];

interface Order {
  readonly id: number;
  readonly customer: string;
  readonly city: string;
}
const CITIES = ['Lagos', 'Lima', 'Lyon', 'Oslo', 'Perth', 'Pune', 'Quito', 'Seoul'];
// 100,000 orders on the "server": the popup reads ranges of them as it scrolls.
const FIRST = ['Amara', 'Bruno', 'Carmen', 'Dawit', 'Emma', 'Felix', 'Grace', 'Hugo', 'Ines', 'Jonas', 'Keiko', 'Lena', 'Marco', 'Nadia', 'Omar', 'Pia', 'Rahul', 'Sofia', 'Tariq', 'Yara'];
const LAST = ['Adeyemi', 'Bauer', 'Castillo', 'Dahl', 'Evans', 'Fontaine', 'Gupta', 'Hoang', 'Ivanova', 'Jensen', 'Kowalski', 'Lindgren', 'Mendes', 'Nakamura', 'Olsen', 'Park', 'Quispe', 'Reyes', 'Silva', 'Tran', 'Vargas', 'Weber', 'Yilmaz'];
const ORDERS: Order[] = Array.from({ length: 100_000 }, (_, i) => ({ id: i + 1, customer: `${FIRST[i % FIRST.length]} ${LAST[i % LAST.length]}`, city: CITIES[i % CITIES.length] as string }));
const ORDER_COLUMNS: ComboboxColumn<Order>[] = [
  { field: 'id', header: 'Order', width: 90, align: 'end' },
  { field: 'customer', header: 'Customer' },
  { field: 'city', header: 'City' },
];

const BY_PRICE: ComboboxSort = { sortBy: [{ columnId: 'price', direction: 'desc' }] };
const BY_NAME: ComboboxSort = { sortBy: [{ columnId: 'name', direction: 'asc' }] };

export default function MulticolumnTemplatesDemo() {
  const [log, push] = useEventLog();
  const [sorting, setSorting] = useState<ComboboxSort>(BY_PRICE);
  const [orders] = useState(() => createDemoListSource<Order>({ rows: ORDERS, latency: 300, log: push }));
  return (
    <div style={{ display: 'grid', gap: 16, maxWidth: 560 }}>
      <div style={{ display: 'grid', gap: 4 }}>
        <span id="mccb-tpl-product">Product</span>
        <Combobox<Product>
          aria-labelledby="mccb-tpl-product"
          data={PRODUCTS}
          columns={COLUMNS}
          getItemValue={(p) => p.id}
          getItemLabel={(p) => p.name}
          searchFields={['name', 'category']}
          searchOperator="contains"
          rowHeight={44}
          sorting={sorting}
        />
        <div style={{ display: 'flex', gap: 8 }}>
          <Button variant="outline" aria-pressed={sorting === BY_PRICE} onClick={() => setSorting(BY_PRICE)}>
            Sort by price, highest first
          </Button>
          <Button variant="outline" aria-pressed={sorting === BY_NAME} onClick={() => setSorting(BY_NAME)}>
            Sort by name
          </Button>
        </div>
      </div>
      <div style={{ display: 'grid', gap: 4 }}>
        <span id="mccb-tpl-order">Order (100,000 on a remote source)</span>
        <Combobox<Order> aria-labelledby="mccb-tpl-order" data={orders} columns={ORDER_COLUMNS} getItemValue={(o) => o.id} getItemLabel={(o) => `#${o.id} ${o.customer}`} searchFields={['customer', 'city']} sorting={false} />
      </div>
      <EventLog entries={log} label="Order requests" />
    </div>
  );
}
```

Rows do not wrap text and columns do not resize.
Wrapped rows would have varying heights, which breaks the
fixed-height virtual window that keeps a lookup popup fast; instead long
text ends in an ellipsis with the full text as `title`. To show more
text, use wider columns, or a larger `rowHeight` with a two-line
`renderCell`. Resizing inside a transient popup would need pointer drag
handling and width state that resets on close, for a feature lookup popups
rarely need; set widths in `columns`.

Remote data under virtualization is never fetched in full:
`data` can be any `@salmonui/data` source,
and the grid reads ranges of at most 100 rows as it scrolls or jumps, with
pending rows shown as placeholders until their range arrives. The release
gate scrolls a 100,000-row remote source and checks that every read is a
range and the frame budget holds.

Value, events, forms and server rendering are those of
[Combobox](https://salmonui.dev/docs/dropdowns/combobox); see the
[API reference](https://salmonui.dev/docs/reference/dropdowns/combobox) for `ComboboxColumn`.
