# `LedgerUI.Components.Table`
[🔗](https://github.com/devaction-labs/ledger_ui/blob/v0.2.0/lib/ledger_ui/components/table.ex#L1)

Ledger UI's tables: basic, bordered, hoverable, zebra, the grid and the lean `data` looks, with
`:col` slots the way Phoenix's generated `CoreComponents.table/1` takes them.

    <.table id="users" rows={@users}>
      <:col :let={user} label="Name">{user.name}</:col>
      <:col :let={user} label="Job" align="right">{user.job}</:col>
    </.table>

    <.table id="users" rows={@streams.users} variant="hoverable"
      row_click={fn {_id, user} -> JS.navigate(~p"/users/#{user.id}") end}>
      <:col :let={{_id, user}} label="Name" sort_key="name">{user.name}</:col>
      <:action :let={{_id, user}}><.button size="sm">Edit</.button></:action>
    </.table>

The markup is `div.lu-tablewrap > table.lu-table.<variant>`; the look lives in `ledger.css`.
`variant` is `nil` for the bare `lu-table`; `rounded` (`row`, `top`) rounds the header row.

## Events

Every event attr takes an event name or a `%JS{}`; `target` sets `phx-target`.

  * `on_sort`: `%{"column" => sort_key, "direction" => "asc" | "desc"}` (the direction to sort
    by next). A column with `sort_key` gets `class="sort"` and the arrows; `sort_by`/`sort_dir`
    mark the current order.
  * `on_select`: `%{"id" => key, "selected" => "true" | "false"}` (the new state of that row).
    `on_select_all`: `%{"selected" => "true" | "false"}`. `selected` lists the checked keys; the
    key is `row_key.(row)` or the row's DOM id. With a stream, `stream_insert` the rows whose
    selection changed and give `all_selected`.

## Hand-written tables

Without `rows`, the table renders its inner block: `table_head/1`, `table_body/1`,
`table_row/1`, `table_cell/1`, `table_foot/1` and `table_empty/1` (see each).

## Columns that leave by priority

A `:col` with `priority` 1, 2 or 3 leaves (header, cells and `:foot` cell together) when the
table's own box gets narrower than 720, 560 and 420px (a container query scoped to the table's
`id`). `table-narrow-hide` hides what carries it under 420px.

## Card layout, details panel, copy columns (used by `card_table/1`)

Extra attributes `card_below`, `details`, `row_editing`, `row_action` and the column attributes
`role`, `width`, `truncate`, `card`, `copy*` back the "card" table (see
`LedgerUI.Components.CardTable`): under `card_below` px each row is a two-line grid placed by the
`role` of its cells (`head`, `status`, `title`, `value`); `details` adds a panel under the clicked
row (hook `LedgerUI.CardTable`) with the `:detail` slots and the columns that left by priority;
`copy` turns a cell into a copy button; `row_editing` swaps a row for the `:edit` slot.

## Not implemented

`peek` / `peek_label` (the panel with the hidden columns) and `bleed` are accepted but draw
nothing yet. `borderless` only adds the `borderless` class (the stylesheet has no rule for it).

# `table`

## Attributes

* `id` (`:string`) (required)
* `rows` (`:any`) - A list or a `@streams.x` stream. Leave it out to write the table by hand. Defaults to `nil`.
* `row_id` (`:any`) - Function giving each row's DOM id (streams: the stream id). Defaults to `nil`.
* `row_click` (`:any`) - Function of the row returning a `JS` or an event. Defaults to `nil`.
* `row_class` (`:any`) - Function of the row returning extra classes of its `<tr>`. Defaults to `nil`.
* `row_attrs` (`:any`) - Function of the row returning a map of extra `<tr>` attributes. Defaults to `nil`.
* `row_item` (`:any`) - Maps each row before the slots get it. Defaults to `&Function.identity/1`.
* `row_key` (`:any`) - Function giving the selection key of a row (default: DOM id). Defaults to `nil`.
* `variant` (`:string`) - `nil` is the bare `lu-table`. `grid` is the gridjs look; `data`/`data-grid` the lean one of `data_table/1`. Defaults to `nil`. Must be one of `nil`, `"basic"`, `"bordered"`, `"hoverable"`, `"zebra"`, `"grid"`, `"data"`, `"data-grid"`, or `"flat"`.
* `rounded` (`:string`) - Defaults to `nil`.Must be one of `nil`, `"row"`, `"top"`, or `"none"`.
* `sort_by` (`:string`) - Defaults to `nil`.
* `sort_dir` (`:string`) - Defaults to `"asc"`. Must be one of `"asc"`, or `"desc"`.
* `on_sort` (`:any`) - Defaults to `nil`.
* `on_select` (`:any`) - Defaults to `nil`.
* `on_select_all` (`:any`) - Defaults to `nil`.
* `selected` (`:list`) - Defaults to `[]`.
* `all_selected` (`:boolean`) - Defaults to `nil`.
* `hoverable` (`:boolean`) - Defaults to `nil`.
* `compact` (`:boolean`) - Defaults to `false`.
* `headless` (`:boolean`) - Defaults to `false`.
* `sticky_header` (`:boolean`) - Defaults to `false`.
* `borderless` (`:boolean`) - Defaults to `false`.
* `bleed` (`:boolean`) - Not implemented yet. Defaults to `false`.
* `card_below` (`:integer`) - Width (px) of the table's own box under which rows become two-line cards (needs `role`s). Defaults to `nil`.
* `details` (`:boolean`) - A click on a row opens a panel under it (hook `LedgerUI.CardTable`). Defaults to `false`.
* `panel_label` (`:string`) - Defaults to `"Details"`.
* `row_editing` (`:any`) - Function of the row; true swaps its cells for the `:edit` slot. Defaults to `nil`.
* `row_action` (`:any`) - Function of the row; false leaves the content of its `:action` cells out. Defaults to `nil`.
* `body_class` (`:any`) - Classes of the `<tbody>`. Defaults to `nil`.
* `select_attrs` (`:any`) - Function of the row returning extra attributes (`class`, `style`) of its checkbox label. Defaults to `nil`.
* `expand_open` (`:any`) - Function of the row: with `:expand`, shows its detail row always open (no toggle column), for rows it returns true for. Defaults to `nil`.
* `breakpoints` (`:list`) - Widths (px) under which priority 1, 2, 3... leave. Defaults to `[720, 560, 420]`.
* `wrapper` (`:boolean`) - `false` leaves out the `lu-tablewrap` box (a table inside a `lu-card-b`). Defaults to `true`.
* `container` (`:boolean`) - Defaults to `nil`.
* `peek` (`:boolean`) - Not implemented yet. Defaults to `false`.
* `peek_label` (`:string`) - Defaults to `"Hidden columns"`.
* `target` (`:any`) - Defaults to `nil`.
* `expand_label` (`:string`) - Defaults to `nil`.
* `scroll_label` (`:string`) - Defaults to `nil`.
* `sort_label` (`:string`) - Defaults to `nil`.
* `row_label` (`:any`) - Defaults to `nil`.
* `select_all_label` (`:string`) - Defaults to `"Select all rows"`.
* `select_label` (`:string`) - Defaults to `"Select row"`.
* `wrapper_class` (`:any`) - Defaults to `nil`.
* `class` (`:any`) - Classes of the `<table>`. Defaults to `nil`.
* Global attributes are accepted. Attributes of the `<table>`.
## Slots

* `action` - Cells appended to every row, after the columns. Accepts attributes:

  * `priority` (`:integer`) - Leaves with the columns of that priority.
  * `card` (`:boolean`) - `false` leaves the cell out of the card layout.
* `col` - A column; receives the row (after `row_item`). Accepts attributes:

  * `label` (`:string`)
  * `class` (`:any`)
  * `head_class` (`:any`)
  * `align` (`:string`) - Must be one of `"left"`, `"center"`, or `"right"`.
  * `sort_key` (`:string`)
  * `priority` (`:integer`)
  * `style` (`:string`) - Inline style of the cell (`<td>`).
  * `role` (`:string`) - Must be one of `"head"`, `"status"`, `"title"`, `"value"`, or `"meta"`.
  * `width` (`:string`) - CSS track: `max-content` (default) or `minmax(120px,1.3fr)`.
  * `truncate` (`:boolean`)
  * `card` (`:boolean`) - Keeps the cell in the card layout, in a line of its own.
  * `copy` (`:any`) - Function of the row returning the text a copy button copies.
  * `copy_label` (`:string`)
  * `copied_label` (`:string`)
  * `copy_show` (`:string`) - Must be one of `"inline"`.
  * `copy_tip` (`:string`) - Must be one of `"value_label"`.
* `detail` - A `label: value` line of the details panel; receives the row. Accepts attributes:

  * `label` (`:string`)
* `edit` - Inline form in place of the cells of a row for which `row_editing` is true.
* `empty` - Shown in a full-width row when there are no rows.
* `expand` - Detail shown under a row; receives `%{row: row, toggle: js}`.
* `foot` - A cell of the `<tfoot>` row, for totals. Accepts attributes:

  * `class` (`:any`)
  * `align` (`:string`) - Must be one of `"left"`, `"center"`, or `"right"`.
  * `colspan` (`:any`)
  * `priority` (`:integer`)
* `inner_block` - The hand-written table when `rows` is not given.

# `table_body`

## Attributes

* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.
## Slots

* `inner_block` (required)

# `table_cell`

## Attributes

* `header` (`:boolean`) - Renders a `<th scope="row">`. Defaults to `false`.
* `align` (`:string`) - Defaults to `nil`.Must be one of `nil`, `"left"`, `"center"`, or `"right"`.
* `colspan` (`:any`) - Defaults to `nil`.
* `rowspan` (`:any`) - Defaults to `nil`.
* `priority` (`:integer`) - Defaults to `nil`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.
## Slots

* `inner_block`

# `table_checkbox`

## Attributes

* `id` (`:string`) - Defaults to `nil`.
* `checked` (`:boolean`) - Defaults to `false`.
* `indeterminate` (`:boolean`) - Draws the dash of a partial selection (needs an `id`, for its hook). Defaults to `false`.
* `label` (`:string`) (required) - The `aria-label` of the box.
* `on_click` (`:any`) - Defaults to `nil`.
* `values` (`:map`) - Sent as `phx-value-*` with `on_click`. Defaults to `%{}`.
* `target` (`:any`) - Defaults to `nil`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted. Attributes of the `<label>`.

# `table_detail_row`

## Attributes

* `row_id` (`:string`) (required)
* `open` (`:boolean`) - Defaults to `false`.
* `class` (`:any`) - Classes of the detail panel. Defaults to `nil`.
* Global attributes are accepted.
## Slots

* `inner_block` (required)

# `table_empty`

## Attributes

* `id` (`:string`) (required)
* `class` (`:any`) - Classes of the cell. Defaults to `nil`.
* Global attributes are accepted.
## Slots

* `inner_block` (required)

# `table_expand_toggle`

## Attributes

* `row_id` (`:string`) (required)
* `open` (`:boolean`) - Defaults to `false`.
* `label` (`:string`) - Defaults to `"Toggle details"`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.

# `table_foot`

## Attributes

* `class` (`:any`) - Classes of the `:cell` row. Defaults to `nil`.
* Global attributes are accepted.
## Slots

* `cell` - A `<td>` of the totals row. Accepts attributes:

  * `class` (`:any`)
  * `align` (`:string`) - Must be one of `"left"`, `"center"`, or `"right"`.
  * `colspan` (`:any`)
  * `priority` (`:integer`)
* `inner_block` - Free `<tr>` rows after the `:cell` row.

# `table_head`

## Attributes

* `sort_by` (`:string`) - Defaults to `nil`.
* `sort_dir` (`:string`) - Defaults to `"asc"`. Must be one of `"asc"`, or `"desc"`.
* `on_sort` (`:any`) - Defaults to `nil`.
* `on_select_all` (`:any`) - Adds a select-all checkbox cell first; the event it sends. Defaults to `nil`.
* `all_selected` (`:boolean`) - Defaults to `false`.
* `some_selected` (`:boolean`) - Defaults to `false`.
* `select_all_id` (`:string`) - Defaults to `nil`.
* `select_all_label` (`:string`) - Defaults to `"Select all rows"`.
* `sort_label` (`:string`) - Defaults to `nil`.
* `target` (`:any`) - Defaults to `nil`.
* `sticky_header` (`:boolean`) - Defaults to `false`.
* `class` (`:any`) - Classes of the `<tr>`. Defaults to `nil`.
* Global attributes are accepted. Attributes of the `<thead>`.
## Slots

* `col` - A header cell; its content is the label (or give `label`). Accepts attributes:

  * `label` (`:string`)
  * `class` (`:any`)
  * `align` (`:string`) - Must be one of `"left"`, `"center"`, or `"right"`.
  * `colspan` (`:any`)
  * `priority` (`:integer`)
  * `sort_key` (`:string`)
  * `title` (`:string`)
  * `id` (`:string`)
* `inner_block` - Extra `<th>` cells after the columns, or further header rows.

# `table_row`

## Attributes

* `selected` (`:boolean`) - `true` checks the box and marks the row (`sel`). Defaults to `nil`.
* `on_click` (`:any`) - Defaults to `nil`.
* `on_select` (`:any`) - Adds a selection checkbox cell first; the event it sends. Defaults to `nil`.
* `select_value` (`:any`) - Sent as `"id"` by the checkbox. Defaults to `nil`.
* `select_label` (`:string`) - Defaults to `"Select row"`.
* `target` (`:any`) - Defaults to `nil`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.
## Slots

* `cell` - A `<td>` of the row. Accepts attributes:

  * `class` (`:any`)
  * `align` (`:string`) - Must be one of `"left"`, `"center"`, or `"right"`.
  * `colspan` (`:any`)
  * `rowspan` (`:any`)
  * `priority` (`:integer`)
  * `title` (`:string`)
  * `id` (`:string`)
* `inner_block` - Free cells after the `:cell` ones.

# `table_skeleton`

## Attributes

* `id` (`:string`) (required)
* `rows` (`:integer`) - Defaults to `5`.
* `columns` (`:list`) - Header labels; one column each. Defaults to `[]`.
* `cols` (`:integer`) - How many columns when `columns` is empty. Defaults to `4`.
* `variant` (`:string`) - Defaults to `nil`.Must be one of `nil`, `"basic"`, `"bordered"`, `"hoverable"`, `"zebra"`, `"grid"`, `"data"`, `"data-grid"`, or `"flat"`.
* `rounded` (`:string`) - Defaults to `nil`.Must be one of `nil`, `"row"`, `"top"`, or `"none"`.
* `compact` (`:boolean`) - Defaults to `false`.
* `headless` (`:boolean`) - Defaults to `false`.
* `shapes` (`:list`) - Per column: `text` (a bar), `avatar`, `pill` or `number`. Defaults to `[]`.
* `label` (`:string`) - Defaults to `"Loading"`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.

# `table_toggle_row`

The `JS` that opens or closes the detail row of the row with DOM id `row_id`
(`table_expand_toggle/1` and `table_detail_row/1`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
