By : admin
Language: javascript
Date Published: 1 month ago
In a 4‑year-old frontend, data tables tend to accumulate conditional props and flags until they are impossible to reason about. A more maintainable pattern is a headless table component: business logic lives in a hook, while rendering stays completely customizable. The hook owns concerns like sorting, pagination, column visibility, and selection, and exposes a minimal API surface instead of dozens of Boolean props.
This separation improves performance and testability. You can unit test the hook with plain data and stubbed events, while the table UI remains a thin presentational layer. For financial or e‑commerce dashboards, it also lets you reuse the same behavior with different designs: dense layouts for ops teams, card-like layouts for customers, or virtualized views for huge datasets. The key is to make the hook opinionated about state transitions but unopinionated about markup so it scales with new requirements instead of fighting them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | // headless-table/useHeadlessTable.ts import { useMemo, useState } from "react"; export type Row = Record<string, unknown>; export type Column<T extends Row> = { key: keyof T; label: string; sortable?: boolean; width?: number | string; }; export type SortState<T extends Row> = { key: keyof T | null; direction: "asc" | "desc"; }; type Options<T extends Row> = { rows: T[]; columns: Column<T>[]; initialSort?: SortState<T>; pageSize?: number; }; export function useHeadlessTable<T extends Row>({ rows, columns, initialSort, pageSize = 20, }: Options<T>) { const [sort, setSort] = useState<SortState<T>>( initialSort ?? { key: null, direction: "asc" } ); const [page, setPage] = useState(1); const totalPages = Math.max(1, Math.ceil(rows.length / pageSize)); function toggleSort(key: keyof T) { setSort((prev) => { if (prev.key !== key) { return { key, direction: "asc" }; } return { key, direction: prev.direction === "asc" ? "desc" : "asc", }; }); setPage(1); } const sortedRows = useMemo(() => { if (!sort.key) return rows; const sorted = [...rows].sort((a, b) => { const av = a[sort.key!]; const bv = b[sort.key!]; if (av == null && bv == null) return 0; if (av == null) return -1; if (bv == null) return 1; if (av === bv) return 0; if (typeof av === "number" && typeof bv === "number") { return av - bv; } return String(av).localeCompare(String(bv)); }); if (sort.direction === "desc") sorted.reverse(); return sorted; }, [rows, sort]); const paginatedRows = useMemo(() => { const start = (page - 1) * pageSize; return sortedRows.slice(start, start + pageSize); }, [sortedRows, page, pageSize]); function goToPage(next: number) { setPage(Math.min(totalPages, Math.max(1, next))); } return { columns, sort, page, totalPages, rows: paginatedRows, allRows: sortedRows, toggleSort, goToPage, }; } // components/TransactionsTable.tsx – presentational layer import React from "react"; import { useHeadlessTable, Column } from "./headless-table/useHeadlessTable"; type Transaction = { id: string; createdAt: string; customer: string; amount: number; status: "pending" | "completed" | "failed"; }; const columns: Column<Transaction>[] = [ { key: "createdAt", label: "Date", sortable: true, width: 140 }, { key: "customer", label: "Customer", sortable: true }, { key: "amount", label: "Amount", sortable: true, width: 120 }, { key: "status", label: "Status", sortable: true, width: 120 }, ]; export function TransactionsTable({ data }: { data: Transaction[] }) { const table = useHeadlessTable<Transaction>({ rows: data, columns, initialSort: { key: "createdAt", direction: "desc" }, pageSize: 25, }); return ( <div className="card"> <div className="card-header"> <h2>Transactions</h2> <p className="subtitle"> {data.length.toLocaleString()} total transactions </p> </div> <div className="table-wrapper"> <table className="data-table"> <thead> <tr> {table.columns.map((col) => { const isSorted = table.sort.key === col.key; const direction = table.sort.direction; return ( <th key={String(col.key)} style={{ width: col.width }} onClick={() => col.sortable && table.toggleSort(col.key) } className={col.sortable ? "sortable" : undefined} > <span>{col.label}</span> {col.sortable && ( <span className="sort-indicator"> {isSorted ? direction === "asc" ? "▲" : "▼" : "▵"} </span> )} </th> ); })} </tr> </thead> <tbody> {table.rows.length === 0 ? ( <tr> <td colSpan={columns.length} className="empty"> No transactions found </td> </tr> ) : ( table.rows.map((row) => ( <tr key={row.id}> <td>{new Date(row.createdAt).toLocaleString()}</td> <td>{row.customer}</td> <td>${row.amount.toFixed(2)}</td> <td> <StatusPill status={row.status} /> </td> </tr> )) )} </tbody> </table> </div> <footer className="table-footer"> <span> Page {table.page} of {table.totalPages} </span> <div className="pager"> <button onClick={() => table.goToPage(table.page - 1)} disabled={table.page === 1} > Previous </button> <button onClick={() => table.goToPage(table.page + 1)} disabled={table.page === table.totalPages} > Next </button> </div> </footer> </div> ); } function StatusPill({ status }: { status: Transaction["status"] }) { const color = status === "completed" ? "success" : status === "pending" ? "warning" : "danger"; return <span className={`status-pill ${color}`}>{status}</span>; } // Example usage (e.g. in a dashboard page) // <TransactionsTable data={transactions}> // Minimal CSS sketch (adapt as needed) /* .card { border-radius: 12px; border: 1px solid #e5e7eb; padding: 1rem 1.25rem; background: #ffffff; } .card-header { display: flex; flex-direction: column; gap: 0.25rem; margin-bottom: 0.75rem; } .table-wrapper { overflow-x: auto; } .data-table { width: 100%; border-collapse: collapse; font-size: 0.875rem; } .data-table th, .data-table td { padding: 0.5rem 0.75rem; text-align: left; border-bottom: 1px solid #f3f4f6; } .data-table th.sortable { cursor: pointer; user-select: none; } .data-table th .sort-indicator { margin-left: 0.25rem; font-size: 0.7rem; opacity: 0.6; } .data-table tbody tr:hover { background-color: #f9fafb; } .empty { text-align: center; color: #9ca3af; } .table-footer { margin-top: 0.75rem; display: flex; justify-content: space-between; align-items: center; } .pager button { margin-left: 0.5rem; } .status-pill { padding: 0.15rem 0.5rem; border-radius: 999px; font-size: 0.75rem; text-transform: capitalize; } .status-pill.success { background: #ecfdf3; color: #166534; } .status-pill.warning { background: #fffbeb; color: #92400e; } .status-pill.danger { background: #fef2f2; color: #b91c1c; } @media (max-width: 640px) { .card { padding: 0.75rem; } .data-table th, .data-table td { padding: 0.4rem 0.5rem; } } */ |