Data table

Sort, search, select and page through records.

Members 24

0 selected

Invoices

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/data-table.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/data-table.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { DataTable as Engine } from "@/lib/fabricui/data-table-engine";
import { start } from "@/lib/fabricui/skeleton-engine";
import { weave } from "@/lib/fabricui/avatar-engine";
import "./button.css";
import "./checkbox.css";
import "./skeleton.css";
import "./badge.css";
import "./avatar.css";
import "./data-table.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
components/fabricui/data-table.css
/*
 * Data table — rows of records to sort, search, select and page through.
 * Shared by every page with a table; table.js drives it (class DataTable).
 *
 *   <div class="dt">                      the frame
 *     <div class="dt__bar">…</div>        a toolbar (title, search, actions): the page's own
 *     <div class="dt__body"></div>        DataTable renders the table and its footer here
 *   </div>
 *
 * Effects, where they fit:
 *  - dither: while a page or sort is loading, the rows are skeletons and one
 *    glint sweeps across the whole table (skeleton.css/js);
 *  - goo: the current page's highlight in the pager slides between numbers
 *    like a drop (goo-slide.js);
 *  - fabric: the bar of actions for selected rows pops in as cloth, and a
 *    row's menu drops from its button (cloth-drop.js, context-menu.js).
lib/fabricui/data-table-engine.js
/*
 * data-table-engine.js — DataTable: sorting, searching, selecting and paging a
 * list of records into a .dt__body (see data-table.css). From table.js. Uses
 * checkbox.css for row selection, skeleton-engine.js for the loading glint,
 * goo-slide.js for the pager. Added for React: destroy().
 *
 *   const table = new DataTable(el, {
 *     rows,                                   // your records
 *     rowId: r => r.id,
 *     columns: [
 *       { key: "name", label: "Name", width: "34%", sort: r => r.name, html: r => `…`, skeleton: "person" },
 *       { key: "amount", label: "Amount", align: "end", sort: r => r.cents, html: r => `…` },
 *       { key: "more", width: "48px", html: r => `…` },   // no label, no sort
 *     ],
 *     pageSize: 6,
 *     selectable: true,                       // a checkbox column, select-all for the page

Usage

Usage
import { DataTable, type DataTableColumn } from "@/components/fabricui/data-table";

type Invoice = { id: number; customer: string; cents: number };
const esc = (s: string) => s.replace(/[&<>"]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]!);
const money = (c: number) => (c / 100).toLocaleString("en-US", { style: "currency", currency: "USD" });

const columns: DataTableColumn<Invoice>[] = [
  { key: "id", label: "Invoice", width: "24%", sort: r => r.id, html: r => `INV-${r.id}` },
  { key: "customer", label: "Customer", sort: r => r.customer, html: r => esc(r.customer) },
  { key: "amount", label: "Amount", align: "end", sort: r => r.cents, html: r => money(r.cents) },
];

export function Invoices({ rows }: { rows: Invoice[] }) {
  return <DataTable rows={rows} columns={columns} pageSize={6} loadMs={380} toolbar={<h2 className="dt__title">Invoices</h2>} />;
}

Props

PropTypeDefault
rows

Your records. New rows draw again, keeping the selection of rows still there.

R[]—
columns

html(row) is the cell as HTML (escape what users typed). sort(row) makes the header sort: ascending, descending, none. skeleton is the cell's placeholder while loading: "person", "badge" or "none". A new columns array takes effect on the next draw (cells, sorting, skeletons); the header (which columns, labels, widths) is set when the table mounts.

{ key, label?, width?, align?, sort?, html, skeleton? }[]—
rowId

Each row's id. A new one takes effect on the next draw.

(row) => idrow => row.id
pageSize

Rows per page. A change draws again right away.

number8
selectable

A checkbox column with select-all for the page; clicking a row ticks it. Read once, when the table mounts.

booleanfalse
search

Does a row match? Used by ref.search(query). A new one takes effect on the next draw.

(row, query) => boolean—
loadMs

A short load on page and sort changes: skeletons with the dither glint. Read once, when the table mounts.

number0
empty

What shows when nothing matches, as HTML. A new one takes effect on the next draw.

(query) => { icon?, title, sub?, action? }—
summary

The footer's left side, as HTML ("1–6 of 24" by default). A new one takes effect on the next draw.

(view, from, to) => string—
onSelect

Called with the selected rows' ids.

(ids) => void—
toolbar

The bar above the table: a title (className="dt__title"), a search, buttons.

ReactNode—
children

Goes after the table: a bar of actions for selected rows (className="dt-bulk"), say.

ReactNode—
ref

Search, select, show a load or draw again from code.

{ search(q), select(ids), load(ms), render(), selected }—