Web and native

Table in kanso-ui 0.59.0, for React.

import { Table } from '@kanso-labs/kanso-ui'

A grid of rows and columns with optional sorting, selection, column resizing, a sticky header and a footer row.

Material Design has no current data table page, so the geometry follows the archived Material Design data tables page: a 56px header row in a medium weight, 52px body rows, 16px of padding on each side of a cell and an arrow beside the sorted column's name. Its colour roles and state layers are the current system's, the same hover, pressed and selected treatment a list row takes. It is built on React Aria's Table.

What you provide

  • An accessible name: aria-label or aria-labelledby.
  • Table.Header with Table.Column entries, one of them marked isRowHeader so a screen reader can read a row by it.
  • Table.Body with Table.Row entries, each holding one Table.Cell per column.
  • An id on every row and every column. They share one namespace, so keep the two sets apart (for example, prefix column ids with col); a shared value drops every column.
  • A surface around it if it needs one, such as an outlined Card with no padding.

Props

PropTypeDefaultDescription
captionReactNode—The table's title, drawn over it as Kanso's caption, in the voice face at 22/28, and the name a screen reader gives the table, unless an `aria-label` or `aria-labelledby` names it instead. With a caption, the caption and the table sit in one wrapper, and the call site's `ref` and a value `className` and `style` land on that wrapper. A function `className` or `style`, which reads the table's render state, stays on the table itself.
classNameTableProps$1['className']—A function may compute the class from the table's render state.
onResizeResizableTableContainerProps['onResize']—Called as a column is dragged, with every column's width.
onResizeEndResizableTableContainerProps['onResizeEnd']—Called once a drag ends, with every column's width.
resizableboolean—Lets columns marked `resizable` be dragged, by putting the table in the container React Aria keeps the resize state on. A resizable table sizes to its columns rather than to its parent, and that container is what fills the width and scrolls instead.
resizeLabelstring—What the resize handle is called, for a screen reader. React Aria composes it with the column's own name. Left out, React Aria names it in the reader's locale — "Resizer" in English.
selectAllLabelstring—What the select-all checkbox is called, for a screen reader. Left out, React Aria names it in the reader's locale — "Select All" in English.
selectLabelstring—What each row's selection checkbox is called, for a screen reader. The row's own text is what names the row; this names the box inside it. Left out, React Aria names it in the reader's locale — "Select" in English.
stickyHeaderboolean—Holds the header row in place while the rows scroll under it. Needs the table inside something that scrolls; on a table that does not, it draws the same as without it.
styleTableProps$1['style']—A function may compute the style from the table's render state.

Also takes the props of Omit<TableProps$1, 'className' | 'style'>.

Parts

  • Table.Header: the header row holding the columns, with a rule under it.
  • Table.Column: one column; its children are its name. isRowHeader marks the column a row is read by, allowsSorting makes the header cell the control that sorts, selection draws the select-all checkbox, and resizable draws a drag handle at its trailing edge (with defaultWidth, minWidth and maxWidth). align: 'start' | 'end', default 'start'; end is for a column of numbers, setting the name against the end padding with the sort arrow before it.
  • Table.Body: the rows. renderEmptyState is what it shows when there are none.
  • Table.Row: one row, with an id. onAction runs when it is pressed.
  • Table.Cell: one cell. selection draws the row's checkbox in place of its content. align: 'start' | 'end' — give it its column's value, since a cell does not follow the header on its own.
  • Table.Footer: a row under the body for totals and the like, kept out of selection and keyboard navigation.
  • Table.LoadMore: the row shown while more rows are fetched, placed inside Table.Body after the rows. React Aria calls onLoadMore when it comes into view and draws it only while isLoading.

Example

import { Table } from '@kanso-labs/kanso-ui'
<Table aria-label="Label" selectionMode="multiple">
  <Table.Header>
    <Table.Column id="colSelect" selection />
    <Table.Column id="colName" isRowHeader>Label</Table.Column>
    <Table.Column id="colValue">Value</Table.Column>
  </Table.Header>
  <Table.Body>
    <Table.Row id="first">
      <Table.Cell selection />
      <Table.Cell>First item</Table.Cell>
      <Table.Cell>01</Table.Cell>
    </Table.Row>
  </Table.Body>
</Table>

The design’s classes

k-table, k-table-wrap, k-num, with k-mark, k-badge, k-badge--positive and k-badge--caution. Maps to Table in kanso-ui and react-native-kanso-ui.