MDK Logo

DataTable

Generic typed data table built on TanStack React Table. Supports pagination, sorting, expansion, row selection and custom column renderers.

Generic typed data table built on TanStack React Table. Supports pagination, sorting, expansion, row selection and custom column renderers.

import { DataTable } from "@tetherto/mdk-react-devkit";
PropStatusTypeDefaultDescription
dataRequiredI[]The data to be shown in the table. See https://tanstack.com/table/v8/docs/guide/data
columnsRequiredColumnDef<I, any>[]The column configuration table. See https://tanstack.com/table/v8/docs/guide/column-defs
fullWidthOptionalboolean | undefinedtrueIs table full width
enableRowSelectionOptionalboolean | ((row: Row<I>) => boolean) | undefinedfalseShow a checkbox column and enables selection
enableMultiRowSelectionOptionalboolean | undefinedtrueEnables selection of multiple rows
selectionsOptionalRowSelectionState | undefinedundefinedSpecify the selected rows. If `undefined`, the selections are managed internally Object with the key of row ID and a boolean specifying if the row is selected. The default row ID is the index. This can be changed using `getRowId` prop
onSelectionsChangeOptional((selections: RowSelectionState) => void) | undefinedCallback to be called when the row are selected / unselected
enablePaginationOptionalboolean | undefinedtrueShow pagination
borderedOptionalboolean | undefinedfalseAdd borders to all cells
paginationOptionalPaginationState | undefinedundefinedSpecify the pagination params. Object of shape { pageIndex: number, pageSize: number }. If `undefined` then the pagination is managed internally.
onPaginationChangeOptional((pagination: PaginationState) => void) | undefinedCallback to be called when the pagination params change
sortingOptionalSortingState | undefinedundefinedSpecify the sorting params. If `undefined` then the sorting is managed internally.
defaultSortingOptionalSortingState | undefined[]Default sorting applied when the table is first mounted (uncontrolled mode). Ignored when `sorting` is provided.
onSortingChangeOptional((sorting: SortingState) => void) | undefinedCallback to be called when the sorting changes
wrapperClassNameOptionalstring | undefinedClassname of the wrapper element
contentClassNameOptionalstring | undefinedClassname of the content element
tableClassNameOptionalstring | undefinedClassname of the table element
loadingOptionalboolean | undefinedfalseShow a loading indicator overlay
enableRowExpansionOptionalboolean | undefinedfalseShow a columns with a button which can expand the row
canRowExpandOptional((row: Row<I>) => boolean) | undefinedfalseCallback to check if a row can be expanded
expandedRowsOptionalExpandedState | undefinedSpecify the expanded rows If `undefined`, the expansions are managed internally Object with the key of row ID and a boolean specifying if the row is selected. The default row ID is the index. This can be changed using `getRowId` prop
onExpandedRowsChangeOptional((expandedRows: ExpandedState) => void) | undefinedCallback to be called when the rows are expanded or collapsed
renderExpandedContentOptional((row: Row<I>) => React.ReactNode) | undefinedRender the content of the expanded row. Required when `enableRowExpansion` is `true`
getRowIdOptional((row: I, index: number, parent?: Row<I> | undefined) => string) | undefinedGet the row ID for a row. If not specified index is the default row ID.

Usage

Sortable, paginated, optionally selectable / expandable table built on TanStack React Table. Controlled and uncontrolled modes for each piece of state.

Props (subset)

PropTypeRequiredDefaultDescription
dataI[]yesRows.
columnsDataTableColumnDef<I>[]yesTanStack column defs.
fullWidthbooleannotrueStretch to container width.
enableRowSelectionboolean | ((row) => boolean)nofalseCheckbox column.
enableMultiRowSelectionbooleannotrueAllow multi-select.
selectionsDataTableRowSelectionStatenoControlled row-selection state.
onSelectionsChange(s: DataTableRowSelectionState) => voidnoSetter.
enablePaginationbooleannotrueShow pagination footer.
paginationDataTablePaginationStatenoControlled pagination.
sortingDataTableSortingStatenoControlled sorting.
borderedbooleannofalseAdd cell borders.
loadingbooleannofalseShow loading overlay.
enableRowExpansionbooleannofalseShow row expansion column.
renderExpandedContent(row) => ReactNodenoRequired when row expansion is enabled.
getRowId(row, index, parent?) => stringnoindexStable row ID source.

See data-table.tsx for the full list (16 props).

Column meta

FieldApplied by DataTableDescription
alignyesleft | center | right on cells

Data contracts

DataTableColumnDef, DataTableRow, DataTableSortingState, DataTablePaginationState, DataTableRowSelectionState, DataTableExpandedState are re-exported from @tetherto/mdk-react-devkit.

Example

/** * Runnable example for DataTable. */import type { DataTableColumnDef } from '@tetherto/mdk-react-devkit'import { DataTable } from '@tetherto/mdk-react-devkit'type Miner = {  id: string  status: 'online' | 'warning' | 'offline'  hashrate: number  power_w: number}const data: Miner[] = [  { id: 'miner-01', status: 'online', hashrate: 102.4, power_w: 3200 },  { id: 'miner-02', status: 'warning', hashrate: 95.1, power_w: 3450 },  { id: 'miner-03', status: 'offline', hashrate: 0, power_w: 0 },]const columns: DataTableColumnDef<Miner, unknown>[] = [  { accessorKey: 'id', header: 'Miner' },  { accessorKey: 'status', header: 'Status' },  { accessorKey: 'hashrate', header: 'Hashrate (TH/s)' },  { accessorKey: 'power_w', header: 'Power (W)' },]export const DataTableExample = () => {  return <DataTable<Miner> data={data} columns={columns} getRowId={(row) => row.id} />}