@zuilib/table
v0.0.1
Published
Table component for ZUI component library - built on tanstack-react-table.
Maintainers
Readme
@zuilib/table
Data tables for ZUI on @tanstack/react-table v8: sorting, pagination, global search, facet/search column filters, expandable rows, and column helpers.
When to use
- Tabular data with client-side sort/filter/page
- Server-driven tables via
manualControls+rowCount - You already use
@zuilib/componentsfor buttons and global styles
Prerequisites
| Package | Version |
|---------|---------|
| react, react-dom | ^18.0.0 |
| @tanstack/react-table | ^8.0.0 |
| @zuilib/components | styles + Button in footer |
import '@zuilib/components/styles.css'Table components use Tailwind classes from core tokens; no separate CSS file in this package.
Installation
pnpm add @zuilib/table @zuilib/components @headlessui/react @tanstack/react-tableMonorepo:
{
"dependencies": {
"@zuilib/table": "workspace:*",
"@zuilib/components": "workspace:*",
"@tanstack/react-table": "^8.0.0"
}
}Import rules
| Rule | Detail |
|------|--------|
| Default exports | import Table from '@zuilib/table/table' |
| Hook | import useTable from '@zuilib/table/use-table' |
| Column helpers | Functions like textColumn, not components |
| Pass undefined data carefully | useTable uses a stable empty array when data is undefined (e.g. failed fetch) to avoid re-render loops |
Exports
| Subpath | Export | Purpose |
|---------|--------|---------|
| @zuilib/table/table | Table (default), TableProps | Main grid UI |
| @zuilib/table/use-table | useTable (default), TableModel, UseTableOptions | TanStack instance + ZUI helpers |
| @zuilib/table/use-table-state | useTableState (default) | Low-level pagination/sort/filter state |
| @zuilib/table/table-footer | TableFooter (default) | Pagination controls |
| @zuilib/table/table-styles | TableStyles, TableStyleDefaults, TableStyleConfig, Alignment | Style overrides |
| @zuilib/table/columns/text-column | textColumn | Single-value column |
| @zuilib/table/columns/two-values-column | twoValuesColumn | Primary + secondary stacked cell |
| @zuilib/table/cells/date-cell | DateCell | toLocaleDateString() cell renderer |
| @zuilib/table/cells/date-time-cell | DateTimeCell | Date + time cell renderer |
| @zuilib/table/filters/facet-filter-fn | facetFilterFn | Multi-select facet filter |
| @zuilib/table/filters/date-range-filter-fn | dateRangeFilterFn, DateRangeFilter | Start/end date filter |
| @zuilib/table/filters/boolean-filter-fn | booleanFilterFn | Exact boolean match |
SimpleTable is internal only — use Table or plain HTML for custom layouts.
Quick start
import { useMemo } from 'react'
import useTable from '@zuilib/table/use-table'
import Table from '@zuilib/table/table'
import TableFooter from '@zuilib/table/table-footer'
import textColumn from '@zuilib/table/columns/text-column'
import '@zuilib/components/styles.css'
type User = { id: string; name: string; email: string }
function UserTable({ users }: { users: User[] }) {
const columns = useMemo(
() => [
textColumn<User>({ key: 'name', header: 'Name' }),
textColumn<User>({ key: 'email', header: 'Email', enableSorting: false }),
],
[]
)
const model = useTable(users, columns, { pageSize: 10 })
return (
<>
<Table model={model} loading={false} />
<TableFooter tableModel={model} />
</>
)
}Core flow
- Define
ColumnDef[](helpers or raw TanStack defs). const model = useTable(data, columns, options).- Render
<Table model={model} />and optionally<TableFooter tableModel={model} />.
TableModel<T> = TanStack Table<T> plus:
globalFilter: stringresetAllFilters()— clears global filter, column filters, and sorting
useTable
const model = useTable(data, columns, {
pageSize: 10,
initialSort: [{ id: 'name', desc: false }],
columnVisibility: { email: false },
canRowExpand: (row) => row.original.hasDetails,
filterFns: { facet: facetFilterFn },
onStateChange: ({ pagination, sorting, globalFilter, columnFilters }) => {},
manualControls: true, // server-side mode
rowCount: totalFromApi,
})| Option | Purpose |
|--------|---------|
| pageSize | Default page size (default 10) |
| initialSort | SortingState |
| columnVisibility | Hide columns |
| canRowExpand | Row expand predicate |
| filterFns | Custom filter fns registered on the table |
| onStateChange | Sync URL/API when state changes |
| manualControls | Sets manualPagination, manualFiltering, manualSorting |
| rowCount | Total rows when manual pagination |
Global search: model.setGlobalFilter('query') or bind UI to model.globalFilter. Uses filterFns.includesString.
Table component
<Table
model={model}
loading={isLoading}
loadingMessage="Fetching…"
error={isError}
errorMessage={<CustomError />}
noDataMessage="No users"
stickyHeaders
onRowClick={(row) => navigate(row.original.id)}
rowClassName={(row) => (row.getIsSelected() ? 'bg-muted' : undefined)}
renderExpandedRow={({ row }) => <DetailPanel data={row.original} />}
styles={customTableStyles}
/>| Prop | Type |
|------|------|
| model | TableModel<T> required |
| loading | Shows loading row spanning all columns |
| error | Shows error row when not loading |
| noDataMessage | Empty state when no rows (and not loading/error) |
| renderExpandedRow | Renders below row when expanded |
| onRowClick | Row click handler |
| rowClassName | Per-row class |
| stickyHeaders | Sticky <thead> |
| styles | TableStyleConfig partial override |
Column helpers
textColumn
textColumn<Row>({
key: 'status', // accessorKey
header: 'Status',
cell: (ctx) => <Badge>{ctx.getValue()}</Badge>, // optional
enableSorting: true,
enableFiltering: true,
})twoValuesColumn
Stacked primary (bold) + secondary (muted) values:
twoValuesColumn<Row>({
primary: { accessorKey: 'name', header: 'Name' },
secondary: { accessorKey: 'email', header: '' },
enableSorting: false,
filterFn: facetFilterFn,
})Column meta (header/cell behaviour)
Set on columnDef.meta for built-in header UI:
| meta.type | Behaviour |
|-------------|-----------|
| 'filter-faceted' | Facet checkbox popover (needs filterFn: facetFilterFn and faceted values) |
| 'filter-search' | Column search popover |
| 'select-checkbox' | Select-all checkbox header |
| align | 'left' \| 'center' \| 'right' on header/cell |
| noWrap | true — nowrap cell |
Example facet column:
import { facetFilterFn } from '@zuilib/table/filters/facet-filter-fn'
{
...textColumn<Row>({ key: 'status', header: 'Status' }),
filterFn: facetFilterFn,
meta: { type: 'filter-faceted' },
}Register custom filter names in useTable options: filterFns: { facet: facetFilterFn } if using string filterFn ids.
Filter functions
| Export | Filter value shape |
|--------|-------------------|
| facetFilterFn | unknown[] — row value must be in array |
| dateRangeFilterFn | { start?: Date \| string, end?: Date \| string } |
| booleanFilterFn | boolean — exact match |
Cell renderers
Use as cell in a column def:
import DateCell from '@zuilib/table/cells/date-cell'
import DateTimeCell from '@zuilib/table/cells/date-time-cell'
textColumn<Row>({
key: 'createdAt',
header: 'Created',
cell: DateCell,
})TableFooter
<TableFooter tableModel={model} scrollTarget={document.getElementById('table-top')} />Renders First / Previous / Next / Last using @zuilib/components/button. Returns null when totalPages <= 1.
Styling
import { TableStyleDefaults, type TableStyleConfig } from '@zuilib/table/table-styles'
const styles: TableStyleConfig = {
header: { base: '...', border: '...' },
row: { base: '...', hover: '...', selected: '...', clickable: '...' },
cell: { base: '...' },
thead: { base: '...', sticky: '...' },
}
<Table model={model} styles={styles} />Server-side table
const model = useTable(data, columns, {
manualControls: true,
rowCount: apiTotal,
onStateChange: ({ pagination, sorting, columnFilters, globalFilter }) => {
fetchPage({ pagination, sorting, columnFilters, globalFilter })
},
})Pass undefined for data while loading; avoid passing a new [] each render.
Related packages
@zuilib/components— required styles; footer buttons@zuilib/form— not required for tables
Build (maintainers)
pnpm --filter @zuilib/table build