A simple read-only table with a caption, row headers and an empty state.
| Invoice | Customer | Status | Amount |
|---|---|---|---|
| INV-2041 | Harbor Supply Co. | Paid | $1,840.00 |
| INV-2042 | Juniper Dental | Due | $620.00 |
| INV-2043 | Northpeak Outfitters | Overdue | $3,275.50 |
| INV-2044 | Lumen Schools | Paid | $990.00 |
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Badge } from "./ui/uicontext/components/Badge.ts"import { DataTable } from "./ui/uicontext/components/DataTable.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ frame: { inlineSize: "min(100%, 40rem)" },})const invoices = [ { id: "INV-2041", customer: "Harbor Supply Co.", status: "Paid", amount: 1840 }, { id: "INV-2042", customer: "Juniper Dental", status: "Due", amount: 620 }, { id: "INV-2043", customer: "Northpeak Outfitters", status: "Overdue", amount: 3275.5 }, { id: "INV-2044", customer: "Lumen Schools", status: "Paid", amount: 990 },]const tones = { Paid: "success", Due: "info", Overdue: "danger" } as constconst money = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })export const InvoiceTable = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.frame)).children( DataTable(h, { caption: "Invoices this month", rows: invoices, rowKey: (invoice) => invoice.id, empty: "No invoices this month.", columns: [ { key: "id", heading: "Invoice", rowHeader: true, cell: (invoice) => invoice.id }, { key: "customer", heading: "Customer", cell: (invoice) => invoice.customer }, { key: "status", heading: "Status", cell: (invoice) => Badge(h, { tone: tones[invoice.status as keyof typeof tones] }).children(invoice.status) }, // numbers line up on the right, in tabular figures { key: "amount", heading: "Amount", numeric: true, cell: (invoice) => money.format(invoice.amount) }, ], }), )Installation
pnpm dlx shadcn@latest add checkfu/uicontext/data-tableUsage
import { DataTable } from "./ui/uicontext/components/DataTable.ts"interface Project { readonly id: string readonly name: string readonly owner: string}export const ProjectTable = <M>(h: Html.H<M>, projects: ReadonlyArray<Project>) => DataTable(h, { caption: "Projects", rows: projects, rowKey: (project) => project.id, empty: "No projects yet.", columns: [ { key: "name", heading: "Name", rowHeader: true, cell: (project) => project.name }, { key: "owner", heading: "Owner", cell: (project) => project.owner }, ], })Examples
Empty
With no rows, the table keeps its headings and says why it's empty.
| Service | Time |
|---|---|
| Nothing deployed today. | |
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { DataTable } from "./ui/uicontext/components/DataTable.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ frame: { inlineSize: "min(100%, 32rem)" },})interface Deploy { readonly id: string readonly service: string readonly at: string}const deploys: ReadonlyArray<Deploy> = []export const NoDeploys = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.frame)).children( DataTable(h, { caption: "Deploys today", rows: deploys, rowKey: (deploy) => deploy.id, // one row across every column, so the headings still say what will be here empty: "Nothing deployed today.", columns: [ { key: "service", heading: "Service", rowHeader: true, cell: (deploy) => deploy.service }, { key: "at", heading: "Time", cell: (deploy) => deploy.at }, ], }), )API reference
Options
| Name | Type | Description |
|---|---|---|
caption | string | A title above the table that names what it lists, read first by screen readers |
rowsrequired | Row[] | The rows to show, in order |
rowKeyrequired | (row: Row) => string | A stable id for each row, so rows keep their place when the list changes |
columnsrequired | readonly [Column<Row, Message, R>, ...Column<Row, Message, R>[]] | At least one column: its header, its cell, and whether it heads the row |
emptyrequired | string | What the table says when there are no rows |
Accessibility
- A native table with a
caption; column headers havescope="col"and row headersscope="row". - With no rows, it says so in one cell across the table.