# `ixfx-data-table`

A key/value data table with responsive layout, hover details, and keyboard/tickle navigation.

---

## Quick start

```html
<ixfx-data-table id="table"></ixfx-data-table>

<script type="module">
  import '@ixfx/components/data-table';

  const table = document.querySelector('#table');
  table.rows = [
    { label: `Name`, value: `Ada` },
    { label: `Age`, value: 30 },
    { label: `Active`, value: true, detail: `Account was active since 2020` },
  ];
</script>
```

Import via bundle: `import '@ixfx/components';` or per-component `import '@ixfx/components/data-table';`.

---

## Rows

```ts
type DataTableRow = {
  label: string;
  value: string | number | boolean | Node | TemplateResult;
  fullWidth?: boolean;  // spans entire row width
  detail?: string | TemplateResult;  // hover/click reveals this
  id?: string;
  // editing (see below)
  editable?: boolean;
  dataType?: 'text'|'number'|'boolean'|'custom'; // default: inferred from the value's type
  editor?: 'auto'|'select'|DataTableEditorFactory;
  editorOptions?: readonly (string|{value,label})[];
  editorPopup?: boolean;                // render the editor in an anchored popup
  editTrigger?: 'default'|'click';      // 'click' = single click opens (user mode)
  renderer?: (value, row) => unknown;   // custom display of the value
};
```

---

## Layout

| `layout` | Description |
|---|---|
| `auto` (default) | Responsive — widens to 2–3 pair columns on wide containers, stacks on narrow |
| `rows` | Always stacked (key above value) |
| `columns` | Always two-column grid regardless of width |

Switch to stacked at a custom breakpoint:

```html
<ixfx-data-table narrow-breakpoint="620"></ixfx-data-table>
```

---

## Selection & tickle

Clicking a row selects it and reveals its detail panel. `selectedIndex` tracks the selection:

```ts
table.selectedIndex = 0;  // select first row
table.addEventListener(`data-table-row-click`, (e) => {
  console.log(e.detail.row, e.detail.index, e.detail.selected);
});
```

Hover reveals the detail panel after `tickle-delay` ms (default 400). Clicking reveals immediately.

---

## Editing

Modelled on `ixfx-data-grid` editing. Each row's value is the editable cell; only `string`, `number` and
`boolean` values can be edited (a `Node` / `TemplateResult` value never is). Edits write back to
`row.value`, replace `rows` with a fresh array, and fire `data-table-cell-edit`.

| `editability` | Behaviour |
|---|---|
| `disabled` | No editing |
| `manual` | Only via `startEdit` |
| `user` (default) | Slow double-click (two clicks 150–700ms apart on the same row) opens the editor; `Enter` on the tickled row opens it |
| `immediate` | Editable values always render as editors |

```ts
table.editable = true;                       // every primitive row, unless row.editable === false
table.editable = (row, index) => index < 3;  // or a predicate (wins over row.editable)
// default (false): only rows with `editable: true`
table.rows = [
  { id: `name`, label: `Name`, value: `Ada`, editable: true },
  { id: `age`, label: `Age`, value: 30, editable: true },                    // number editor
  { id: `on`, label: `Active`, value: true, editable: true },                // checkbox
  { id: `role`, label: `Role`, value: `a`, editable: true, editor: `select`, editorOptions: [`a`, `b`] },
  { id: `c`, label: `Custom`, value: `x`, editable: true,
    editor: ({ value, commit, cancel }) => html`<my-picker .value=${value} @pick=${e => commit(e.detail)} @close=${cancel}>` },
];
table.startEdit(`name`);   // by id (or row index)
table.stopEdit();          // cancel
```

Rows are keyed by `id`, falling back to their index. A `number` row commits a number (invalid or empty input
keeps the old value). A custom editor's `commit(v)` writes the value and fires the event itself.

**Keyboard.** The table is focusable only while something is editable. Not editing: `ArrowUp/Down` move the
tickled row; `Enter` opens its editor (`user` mode). While editing: `Enter` commits, `Escape` cancels,
`Tab`/`ArrowDown` commit and open the next editable row, `Shift+Tab`/`ArrowUp` the previous one (`Up`/`Down`
are left to a `<select>`). Blur inside the table commits; focus leaving the table reverts (not in `immediate`).

### Rich editor popup

Any `editor` factory can render in a popup instead of in place. Set `editorPopup: true` and the table shows the
factory's template in a native popover anchored under the value (so it is never clipped), keeps the value visible,
and handles focus, positioning and dismissal. Combine with `renderer` for a custom display and `editTrigger: 'click'`
to open on a single click.

```ts
{
  id: `colour`, label: `Colour`, value: `oklch(0.7 0.15 250)`, editable: true,
  editorPopup: true, editTrigger: `click`,
  renderer: v => html`<span class="swatch" style="background:${v}"></span> ${v}`,
  editor: ctx => html`<ixfx-colour-picker value=${ctx.value}
    @input=${e => ctx.update(e.currentTarget.value)}></ixfx-colour-picker>`,
}
```

The factory context is `{ row, index, rowKey, value, commit, update, cancel }`:

* `update(v)` records a pending value without closing (use for live controls such as sliders).
* `commit(v)` writes `v`, fires `data-table-cell-edit` and closes.
* `cancel()` closes and discards.
* Click outside commits the last value passed to `update` (like blur does for inline editors); `Escape` cancels.
  In `immediate` mode popup rows still open on click rather than rendering an always-on editor.

`part="editor-popup"` is exposed for styling. The popup lives in the table's shadow root, so focus moving into it
does not count as leaving the table.

---

## CSS variables

| Variable | Default | Description |
|---|---|---|
| `--data-table-name-width` | `max-content` | Column width for labels |
| `--data-table-value-width` | `minmax(0, 1fr)` | Column width for values |
| `--data-table-cell-align` | `baseline` | Cell vertical alignment |
| `--data-table-row-padding` | `0.35em` | Row padding |
| `--data-table-name-align` | `left` | Label text alignment |

---

## Methods

| Method | Signature | Description |
|---|---|---|
| `rows` | `readonly DataTableRow[]` | Row data |

| Property | Type | Description |
|---|---|---|
| `selectedIndex` | `number \| undefined` | Currently selected row index |
| `layout` | `'auto' \| 'rows' \| 'columns'` | Layout mode |
| `narrowBreakpoint` | `number` | Breakpoint for stacking (px) |
| `tickleDelay` | `number` | Hover detail delay (ms) |
| `editable` | `boolean \| (row, index) => boolean` | Default editability for rows |
| `editability` | `'disabled' \| 'manual' \| 'user' \| 'immediate'` | Editing mode (default `user`) |
| `startEdit(idOrIndex)` / `stopEdit()` | method | Programmatic editing |

---

## Events

| Event | Detail | Description |
|---|---|---|
| `data-table-row-click` | `{ row, index, selected }` | Fired on row click |
| `data-table-cell-edit` | `{ row, index, rowKey, oldValue, newValue }` | A value was committed |
