Cell

Cell

Cell is a legacy migration surface: the Rust crate's single table cell. It emits a <td> whose class list is read off the ColumnDef the host passes (hk-cell plus the column's alignment), stamps the row and column indices on as data-* attributes, and either prints its value or hands it to a renderer the host supplies. It does not draw a column header, a row, a table or a thead — the host assembles those.

New code should not build a body out of Cell. The Vue surface ships HkTable, which owns the whole table: it takes one columns array and one rows array, renders the <thead> / <tbody> / <caption> itself, and adds the sizing, striping, hover, sorting, selection, row-actions and copy affordances that a page built from loose cells has to re-invent.

  • Reach for it only while migrating a Rust page that already renders this shape. On a new surface use HkTable — a cell there is a value in a row object, not a component.
  • HkTable also gives each header cell scope="col" and lets a column be marked sortable; Cell ships a sortable flag on its column, but the cell itself never draws a sort affordance (Sort is the separate bar that does — see Sort).
  • A single leading checkbox column, rather than data cells, is Selection or RowSelection; rows over the cells are ListRow.

Import

rust
1
2
use hikari_components::*;          // Cell, create_cell_renderer, ColumnDef, ColumnAlign, CellRenderer
use hikari_components::prelude::*; // CellProps, ColumnProps, rsx!, VNode

Cell is a plain function, Cell(CellProps { … }). Inside rsx! every prop may be omitted except column, the one required field (ColumnDef implements Default, so column: ColumnDef::default() compiles — it just renders a cell with an empty data-key and the left alignment). CellProps and ColumnProps are in the prelude; ColumnDef, ColumnAlign, CellRenderer and create_cell_renderer are not, so a host that names them takes them from hikari_components, as the import line above does. A render closure is wrapped by the free function create_cell_renderer, which is the only public constructor for the type.

One cell

The class list is built from the column, not from the cell: hk-cell, then hk-align-left / hk-align-center / hk-align-right from ColumnDef::align, then hk-cell-hover on every instance, then hk-cell-editable when editable is on, then whatever class the host adds. The <td> always carries data-row-index, data-col-index (both "0" unless given) and data-key (the column's column_key), and — on the default path only — data-editable.

Rust
NameQuantityState
Atlas42live

A custom renderer

render takes a CellRenderer, an Rc<dyn Fn(&str, usize, usize) -> Element>; create_cell_renderer builds one from a closure that receives the value and the two indices. With a renderer the component returns the td before running the default path, so data-editable is not stamped and editable only reaches the class list. The cell still prints no wrapper of its own: whatever the closure returns lands directly inside the <td>.

The renderer is not part of PartialEq on CellProps (only value, column, the two indices, class and editable are compared), so a re-render is not caused by swapping in a different closure with the same values.

Rust
State
Ready

Notes on the shape

  • The title on ColumnDef is not rendered by Cell: it is the header text another component places (HkTable's <th>, or Column). A cell built with a titled column still only shows value.
  • width, min_width, max_width, fixed, sortable, filterable, resizable and the column's own class are read by nobody inside Cell; the class list comes from align and the host's class alone.
  • class is appended after the palette classes without a separating space of its own, so ..Default::default() based columns and an empty class keep the trailing space the palette builder leaves — cosmetic in the markup, harmless in CSS.

API

Rust

Props — Cell

PropTypeDefaultDescription
valueString""—
columnColumnDefDefault::default()—
row_indexusize0—
col_indexusize0—
classString""—
renderOption<CellRenderer>None—
editableboolfalse—

Accessibility

  • The component emits a real <td>, so it inherits the semantics of whatever table the host wraps around it — but it writes no scope, no header association and no role. HkTable emits <th scope="col"> for each header; a hand-built table has to supply the header row and its scope itself.
  • The data-* indices are hooks for the host's CSS and tests, not for assistive technology: the row and column position is never announced from them.
  • editable is a class and (on the default path) a data-editable attribute only. There is no <input>, no contenteditable and no aria-readonly, so a cell marked editable is still plain text — the editing affordance is the host's to build.
  • Text is the cell body; a custom renderer's output is inserted verbatim inside the <td> and is therefore the host's responsibility for name and role.

Related components

  • HkTable — the Vue table that replaces a hand-assembled body of cells: column definitions, sorting, selection and row actions in one component.
  • Sort — the sort bar that belongs to a set of columns, rather than an affordance on one cell.
  • Selection and RowSelection — the leading selection column and the per-row checkbox.
  • ListRow — the row shape for a list, when the data is not a grid.
  • Table — the component page for the Rust table this cell was written for.