/** * TwoUX composition: an editable grid for enriching many columns (WAI-ARIA grid pattern). * One tab stop; arrows move between cells, Home/End to the row's ends; Enter or F2 edits, Esc cancels, * Enter saves and stays, Tab saves and moves on. Validation per column; the message shows in the status line. * Pinned columns and the header stay in view while scrolling; the totals row counts only the rows shown. * Optional row selection: a checkbox column (Shift+Space toggles the active row) and the bulk action bar above the header. */ import { Pencil } from "lucide-react" import * as React from "react" import { cn } from "../lib/cn" import { Checkbox } from "./checkbox" import { type BulkAction, BulkActionBar } from "./data-table" export type GridColumn = { id: string label: string /** px; pinned columns need one */ width: number pin?: boolean editable?: boolean type?: "text" | "number" /** Return a message to block the save. */ validate?: (value: string) => string | undefined total?: "sum" | "average" } export type GridRow = { id: string } & Record export function DataGrid({ columns, rows, onCellChange, "aria-label": label, selectedRowIds, onSelectedRowIdsChange, bulkActions, noun, className, }: { columns: GridColumn[] rows: GridRow[] onCellChange: (rowId: string, columnId: string, value: string | number) => void "aria-label": string /** Selected rows. Pass with onSelectedRowIdsChange to add a checkbox column. */ selectedRowIds?: string[] onSelectedRowIdsChange?: (ids: string[]) => void /** Actions for the bulk action bar above the header while rows are selected; extras move into More. */ bulkActions?: BulkAction[] /** Plural noun for the selection count: "2 listings selected". */ noun?: string className?: string }) { const selectable = !!onSelectedRowIdsChange const selected = selectedRowIds ?? [] const setSelected = (ids: string[]) => onSelectedRowIdsChange?.(ids) const toggleRow = (id: string) => setSelected(selected.includes(id) ? selected.filter((x) => x !== id) : [...selected, id]) const allChecked = rows.length > 0 && rows.every((r) => selected.includes(r.id)) ? true : rows.some((r) => selected.includes(r.id)) ? "indeterminate" : false // The checkbox column is pinned at the left edge, so pinned data columns start after it (w-9, 36px). const offset = selectable ? 36 : 0 const [pos, setPos] = React.useState({ r: 0, c: 0 }) const [editing, setEditing] = React.useState(null) const [error, setError] = React.useState("") const [status, setStatus] = React.useState("") const grid = React.useRef(null) const lefts = columns.reduce((acc, c, i) => [...acc, i === 0 ? offset : acc[i - 1] + (columns[i - 1].pin ? columns[i - 1].width : 0)], []) const totalsRow = columns.some((c) => c.total) const cellEl = (r: number, c: number) => grid.current?.querySelector(`[data-cell="${r}-${c}"]`) React.useEffect(() => { if (editing === null) cellEl(pos.r, pos.c)?.focus({ preventScroll: false }) }, [pos, editing]) const move = (r: number, c: number) => setPos({ r: Math.max(0, Math.min(rows.length - 1, r)), c: Math.max(0, Math.min(columns.length - 1, c)) }) const startEdit = (initial?: string) => { const col = columns[pos.c] if (!col.editable) { setStatus(`${col.label} can't be edited here.`); return } setError("") setEditing(initial ?? String(rows[pos.r][col.id] ?? "")) } const commit = (then: "stay" | "next") => { const col = columns[pos.c], row = rows[pos.r] const v = editing ?? "" const msg = col.validate?.(v) if (msg) { setError(msg); setStatus(`${col.label}: ${msg}`); return false } onCellChange(row.id, col.id, col.type === "number" ? Number(v) : v) setStatus(`Saved ${col.label} for ${row[columns[0].id]}.`) setEditing(null) if (then === "next") move(pos.r, pos.c + 1) return true } const onKey = (e: React.KeyboardEvent) => { if (editing !== null) { if (e.key === "Escape") { e.preventDefault(); setEditing(null); setError(""); setStatus("Edit cancelled.") } else if (e.key === "Enter") { e.preventDefault(); commit("stay") } else if (e.key === "Tab") { e.preventDefault(); commit("next") } return } const k = e.key if (k === "ArrowRight") { e.preventDefault(); move(pos.r, pos.c + 1) } else if (k === "ArrowLeft") { e.preventDefault(); move(pos.r, pos.c - 1) } else if (k === "ArrowDown") { e.preventDefault(); move(pos.r + 1, pos.c) } else if (k === "ArrowUp") { e.preventDefault(); move(pos.r - 1, pos.c) } else if (k === "Home") { e.preventDefault(); move(e.ctrlKey ? 0 : pos.r, 0) } else if (k === "End") { e.preventDefault(); move(e.ctrlKey ? rows.length - 1 : pos.r, columns.length - 1) } else if (k === " " && e.shiftKey && selectable) { e.preventDefault(); toggleRow(rows[pos.r].id); setStatus(`${selected.includes(rows[pos.r].id) ? "Deselected" : "Selected"} ${rows[pos.r][columns[0].id]}.`) } else if (k === "Enter" || k === "F2") { e.preventDefault(); startEdit() } else if (k.length === 1 && !e.metaKey && !e.ctrlKey && !e.altKey && columns[pos.c].editable) { e.preventDefault(); startEdit(k) } } const fmt = (v: unknown) => (typeof v === "number" ? v.toLocaleString("en-GB") : String(v ?? "")) const total = (c: GridColumn) => { const nums = rows.map((r) => Number(r[c.id])).filter((n) => !Number.isNaN(n)) if (!nums.length) return "" const sum = nums.reduce((a, b) => a + b, 0) return c.total === "average" ? (sum / nums.length).toLocaleString("en-GB", { maximumFractionDigits: 1 }) : sum.toLocaleString("en-GB") } const stickyStyle = (i: number) => (columns[i].pin ? ({ "--l": `${lefts[i]}px` } as React.CSSProperties) : undefined) const widthStyle = (i: number) => ({ ...stickyStyle(i), "--w": `${columns[i].width}px` }) as React.CSSProperties return (
{selectable && bulkActions?.length ? ( setSelected([])} actions={bulkActions} /> ) : null}
{selectable && ( )} {columns.map((c, i) => ( ))} {rows.map((row, r) => ( {selectable && ( )} {columns.map((c, ci) => { const here = pos.r === r && pos.c === ci const isEditing = here && editing !== null return ( ) })} ))} {totalsRow && ( {selectable && ))} )}
setSelected(v ? rows.map((r) => r.id) : [])} /> {c.label}{c.editable && }
toggleRow(row.id)} /> { if (!isEditing) setPos({ r, c: ci }) }} onDoubleClick={() => startEdit()} style={widthStyle(ci)} className={cn( "h-9 w-(--w) min-w-(--w) border-b border-line px-3 whitespace-nowrap outline-none", c.pin ? "sticky left-(--l) z-[1]" : "", selected.includes(row.id) ? "bg-primary-soft" : "bg-surface", c.type === "number" && "tabular text-right", c.editable && !isEditing && "hover:bg-hover", here && (isEditing && error ? "shadow-[inset_0_0_0_2px_var(--color-bad)]" : "shadow-[inset_0_0_0_2px_var(--color-primary)]"), isEditing && "bg-surface" )} > {isEditing ? ( { setEditing(e.target.value); setError("") }} onBlur={() => { if (editing !== null && !commit("stay")) setEditing(null) }} className={cn("h-full w-full bg-transparent outline-none", c.type === "number" && "text-right")} /> ) : ( {fmt(row[c.id])} )}
} {columns.map((c, i) => ( {i === 0 ? `Totals (${rows.length} rows shown)` : c.total ? `${c.total === "average" ? "Avg " : ""}${total(c)}` : ""}

{status || `Arrow keys move between cells. Enter or F2 edits, Esc cancels, Tab saves and moves on.${selectable ? " Shift+Space selects the row." : ""}`}

) }