Table

Display tabular data in format.

General

Display formatted data in rows and columns.

compose

Show other components in the table.

width

Specifies the width of all or part of the columns.

actions

Custom elements can be displayed in the table, and any changes will be immediately rendered.

update row

You can use custom elements to update a specific row.

custom head

pagination

Set pagination with a pageSize and the table slices data and shows a Pagination below it. The footer only appears when there is more than one page. onRow and onCell receive the index inside the current page.

server pagination

With total, data is already the current page: the table only shows the Pagination. Control page and load the rows in onPageChange.

TypeScript Example

Get a better experience in TS by specifying a generic type.

type User = {
  name: string
  role: string
  records: Array<{ date: string }>
}
const renderHandler: TableColumnRender<User> = (value, rowData, index) => {
  return <div>{rowData.date}</div>
}
const data: Array<User> = [
  { name: 'witt', role: 'admin', records: [{ date: '2021-05-01' }] }
]

const MyComponent = () => (
  <Table<User> data={data}>
    <Table.Column<User> prop="name" label="username" />
    <Table.Column<User> prop="role" label="role" />
    <Table.Column<User> prop="records" label="records" render={renderHandler} />
  </Table>
)

orders

Use render to show a value as a status with a Dot, so the state reads before the word.

sorting

Add sortable to a column and its header becomes a button. A click sorts ascending, the next descending, and the third goes back to the order of the data. Numbers are compared by value.

selection

selectable adds a checkbox to each row and one in the header for the rows in view. Give each row a stable rowKey, and control selectedKeys to clear the selection after an action.

data table

Sorting, selection and pagination together. The rows are sorted before they are split in pages, and the selection is kept when you change the page.

APIs

Table.Props

AttributeDescriptionTypeAccepted valuesDefault
datadata sourceArray<T>--
initialDatainital datasArray<T>-[]
emptyTextdisplayed text when table's content is emptystring--
hovertable's hover effectboolean-true
onRowcallback row's content by clickTableOnRowClick--
onCellcallback cell's content by clickTableOnCellClick--
onChangedata change event(data: T) => void--
paginationsplit the rows in pagesTablePagination--
rowClassNameset className to each rowTableRowClassNameHandler--
sortthe column the rows are sorted by, to control itTableSort / null--
initialSortsort on the first renderTableSort / null-null
onSortChangecalled with the new sort, or null when it is cleared(sort: TableSort | null) => void--
selectablea checkbox on each row, and one in the headerboolean-false
rowKeywhat identifies a row: a prop, or a function. The position in data when missingkeyof T / (row: T) => string | number--
selectedKeyskeys of the selected rows, to control themArray<string | number>--
initialSelectedKeyskeys of the rows selected on the first renderArray<string | number>-[]
onSelectionChangecalled with the keys and the rows selected(keys, rows: T[]) => void--
selectRowLabelname of the checkbox of a row, for screen readersstring-Select row
selectAllLabelname of the checkbox in the headerstring-Select all rows
...native propsTableHTMLAttributes'id', 'name' ...-

Table.Column.Props

AttributeDescriptionTypeAccepted valuesDefault
prop(required)table-column's propstring--
labeltable-column's labelstring--
widthwidth number (px)number--
classNameset className to each columnstring--
sortablethe header becomes a button that sorts the rowsboolean-false
sorterhow two rows compare, when the default order does not fit(a: T, b: T) => number--
renderrender returns elements in all columnsTableColumnRender--

TableSort

type TableSort = { prop: string; direction: 'asc' | 'desc' }

Sorting and selection

  • • Order: numbers are compared by value, and any other value as text, where item 2 comes before item 10. Use sorter on a column for anything else. The header has aria-sort, and the sorted column shows an arrow.
  • • Server rows: with pagination.total, the rows are the ones of the server, so the table does not sort them. It only calls onSortChange, and you send the new order with the data.
  • • Selection keys: the selection is kept by key, so it survives a sort and a change of page. onSelectionChange receives the rows that are in data, which are only the current page when the rows are on the server.
  • • Select all: the checkbox in the header selects the rows in view, the current page. It is checked when all of them are selected, and in between when only some are.

TablePagination

AttributeDescriptionTypeAccepted valuesDefault
pageSizerows in each pagenumber--
pagecurrent page, to control itnumber-1
onPageChangecalled with the page the user picked(page: number) => void--
totalall the rows on the server, data is the pagenumber--

TableOnRowClick

type TableOnRowClick<T> = (rowData: T, rowIndex: number) => void

TableOnCellClick

type TableOnCellClick<T> = (
  value: T[keyof T],
  rowIndex: number,
  colunmIndex: number
) => void

TableRowClassNameHandler

type TableRowClassNameHandler<T> = (rowData: T, rowIndex: number) => string

TableColumnRender

type TableColumnRender<T extends Record> = (
  value: T[keyof T],
  rowData: T,
  rowIndex: number
) => JSX.Element | void
Edit this page on GitHub