Sortable Selectable Table
Type-aware sorting with aria-sort, a header checkbox that goes properly indeterminate, and selection that survives a re-sort.
Edit cells in the grid with staged changes, per-cell validation, the original value kept beside the attempt, and a save that counts what is still pending.
components/data-table-inline-edit.tsxnpx hoverlab add data-table-inline-edit
Or over MCP, from your editor's agent — no account needed.
Free to read, copy and install, for personal and non-commercial projects. Shipping it in client work or a paid product needs Pro ($79 once). The source lands in your repo and stops being ours — no attribution, nothing to upgrade.
Start a page with this section — add more, order them, and leave with the page source.
Click a seat count or price to edit it.
| Account | Plan | Seats | Monthly |
|---|---|---|---|
| Acme Corp | Studio | ||
| Globex | Pro | ||
| Initech | Studio | ||
| Umbrella | Team |
Enter commits a cell, Tab moves to the next, Escape leaves it. Nothing is written until you press Save.
Rendered live in your current theme — this is the same component whose source is below, not a screenshot of it.
'use client'
/**
* <DataTableInlineEdit> — editing in the grid, with the save honest about
* what is still pending.
*
* The catalog's table blocks sort, paginate, expand, reorder columns and
* act in bulk. All of them are read-only. The moment a table is the primary
* surface for a record — a price list, a seat roster, a translations file —
* people stop wanting a detail page per row and start wanting to type in
* the cell.
*
* THE THREE STATES THAT MATTER, AND THE ONE EVERYONE SKIPS
*
* A cell is clean, dirty, or *failed*. Products ship the first two and then
* treat a failed save as a toast that disappears in four seconds, leaving a
* grid that looks saved and is not. Here a rejected cell stays visibly
* failed with its reason attached, and the row keeps its original value
* beside the attempted one, so the fix does not require remembering what
* was there before.
*
* WHY THERE IS A SAVE BUTTON AT ALL
*
* Autosave-per-keystroke is the fashionable answer and it is wrong for
* tabular data: it makes every typo a write, and it makes a mis-tabbed
* column a silent overwrite of the row below. Edits are staged and
* committed together, and the count of pending edits is always on screen —
* "3 unsaved" is the fact a user needs before they close the tab.
*
* ESCAPE REVERTS, ENTER COMMITS THE CELL, TAB MOVES ON. That is the
* spreadsheet contract, and a grid that breaks it is one people have to
* think about rather than use.
*
* ACCESSIBILITY: cells are real `<input>`s inside `<td>`s rather than
* contenteditable divs, each labelled by its column header through
* `aria-labelledby`, so a screen reader announces "Seat price, row Acme
* Corp". Invalid cells carry `aria-invalid` and point at their message with
* `aria-describedby` rather than relying on a red border.
*/
import * as React from 'react'
import { AlertCircle, Check, RotateCcw, Save } from 'lucide-react'
export interface EditableRow {
id: string
account: string
plan: string
seats: number
monthly: number
}
export interface DataTableInlineEditProps {
rows?: EditableRow[]
className?: string
}
const DEFAULT_ROWS: EditableRow[] = [
{ id: 'acme', account: 'Acme Corp', plan: 'Studio', seats: 24, monthly: 1920 },
{ id: 'globex', account: 'Globex', plan: 'Pro', seats: 8, monthly: 640 },
{ id: 'initech', account: 'Initech', plan: 'Studio', seats: 51, monthly: 4080 },
{ id: 'umbrella', account: 'Umbrella', plan: 'Team', seats: 12, monthly: 960 },
]
type EditableField = 'seats' | 'monthly'
interface CellEdit {
value: string
error?: string
}
/** `${rowId}:${field}` — flat keys keep the diff trivially countable. */
type EditMap = Record<string, CellEdit>
function cellKey(rowId: string, field: EditableField) {
return `${rowId}:${field}`
}
/**
* Validation lives beside the field it guards rather than in a schema file,
* because a block that shipped with a `zod` import would drag a dependency
* into a project for four lines of arithmetic.
*/
function validate(field: EditableField, raw: string): string | undefined {
const trimmed = raw.trim()
if (!trimmed) return 'Required'
const numeric = Number(trimmed.replace(/,/g, ''))
if (!Number.isFinite(numeric)) return 'Must be a number'
if (numeric < 0) return 'Cannot be negative'
if (field === 'seats' && !Number.isInteger(numeric)) return 'Whole seats only'
if (field === 'seats' && numeric > 500) return 'Over the 500-seat plan limit'
return undefined
}
export function DataTableInlineEdit({
rows = DEFAULT_ROWS,
className = '',
}: DataTableInlineEditProps) {
/*
Per-instance prefix for every id this block emits.
The literals these replaced were a latent duplicate the moment the
block appeared twice on one document, and `aria-labelledby` on a
duplicated id resolves to the first match -- so the second copy was
labelled by the first copy's heading. Client component, so `useId` is
the right tool.
*/
const uid = React.useId()
const [edits, setEdits] = React.useState<EditMap>({})
const [saved, setSaved] = React.useState(false)
const pending = Object.keys(edits).length
const invalid = Object.values(edits).filter((edit) => edit.error).length
function change(rowId: string, field: EditableField, value: string, original: number) {
setSaved(false)
setEdits((current) => {
const next = { ...current }
const key = cellKey(rowId, field)
// Typing a value back to its original is not an edit. Without this the
// "3 unsaved" counter keeps counting cells the user has already undone
// by hand, which is the fastest way to make the counter untrustworthy.
if (value.trim() === String(original)) {
delete next[key]
return next
}
next[key] = { value, error: validate(field, value) }
return next
})
}
function revert() {
setEdits({})
setSaved(false)
}
function save() {
if (invalid > 0) return
setEdits({})
setSaved(true)
}
function onKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
if (event.key === 'Escape') {
// Revert this cell only — Escape in a spreadsheet abandons the cell,
// not the sheet.
event.currentTarget.blur()
}
}
return (
<section
className={`rounded-2xl border border-border bg-card text-card-foreground ${className}`}
>
<header className="flex flex-wrap items-center justify-between gap-3 border-b border-border p-5 sm:p-6">
<div>
<h2 className="text-base font-semibold">Account seats</h2>
<p
aria-live="polite"
className="mt-1 text-sm text-muted-foreground"
>
{pending === 0 ? (
saved ? (
<span className="inline-flex items-center gap-1.5 text-emerald-600 dark:text-emerald-400">
<Check aria-hidden className="h-3.5 w-3.5" />
All changes saved
</span>
) : (
'Click a seat count or price to edit it.'
)
) : (
<>
<span className="font-medium text-foreground">{pending} unsaved</span>
{invalid > 0 ? (
<span className="text-destructive"> · {invalid} need fixing</span>
) : null}
</>
)}
</p>
</div>
<div className="flex items-center gap-2">
<button
type="button"
onClick={revert}
disabled={pending === 0}
className="inline-flex items-center gap-1.5 rounded-lg border border-border px-3 py-1.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
<RotateCcw aria-hidden className="h-3.5 w-3.5" />
Revert
</button>
<button
type="button"
onClick={save}
disabled={pending === 0 || invalid > 0}
className="inline-flex items-center gap-1.5 rounded-lg bg-primary px-3 py-1.5 text-sm font-semibold text-primary-foreground transition-opacity hover:opacity-90 disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
>
<Save aria-hidden className="h-3.5 w-3.5" />
Save {pending > 0 ? pending : ''}
</button>
</div>
</header>
<div className="overflow-x-auto">
<table className="w-full min-w-[36rem] border-collapse text-start text-sm">
<thead>
<tr className="border-b border-border">
<th id={`${uid}-col-account`} scope="col" className="px-5 py-2.5 font-semibold sm:px-6">
Account
</th>
<th id={`${uid}-col-plan`} scope="col" className="px-5 py-2.5 font-semibold">
Plan
</th>
<th id={`${uid}-col-seats`} scope="col" className="px-5 py-2.5 font-semibold">
Seats
</th>
<th id={`${uid}-col-monthly`} scope="col" className="px-5 py-2.5 font-semibold">
Monthly
</th>
</tr>
</thead>
<tbody>
{rows.map((row) => (
<tr key={row.id} className="border-b border-border/60 last:border-0">
<th
scope="row"
id={`${uid}-row-${row.id}`}
className="px-5 py-3 font-medium sm:px-6"
>
{row.account}
</th>
<td className="px-5 py-3 text-muted-foreground">{row.plan}</td>
{(['seats', 'monthly'] as EditableField[]).map((field) => {
const key = cellKey(row.id, field)
const edit = edits[key]
const value = edit?.value ?? String(row[field])
const errorId = `${key}-error`
return (
<td key={field} className="px-5 py-2 align-top">
<input
type="text"
inputMode="numeric"
value={value}
aria-labelledby={`${uid}-col-${field} row-${row.id}`}
aria-invalid={Boolean(edit?.error)}
aria-describedby={edit?.error ? errorId : undefined}
onKeyDown={onKeyDown}
onChange={(event) =>
change(row.id, field, event.target.value, row[field])
}
className={`w-24 rounded-md border bg-transparent px-2 py-1 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring ${
edit?.error
? 'border-destructive text-destructive'
: edit
? 'border-primary/60 bg-primary/5'
: 'border-transparent hover:border-border'
}`}
/>
{/*
The original stays on screen next to the attempt.
Recovering from a bad edit should not require
remembering what the number used to be.
*/}
{edit ? (
<span className="mt-1 block text-[11px] text-muted-foreground">
was {row[field].toLocaleString('en-US')}
</span>
) : null}
{edit?.error ? (
<span
id={errorId}
className="mt-1 flex items-center gap-1 text-[11px] font-medium text-destructive"
>
<AlertCircle aria-hidden className="h-3 w-3" />
{edit.error}
</span>
) : null}
</td>
)
})}
</tr>
))}
</tbody>
</table>
</div>
<p className="border-t border-border px-5 py-3 text-xs text-muted-foreground sm:px-6">
Enter commits a cell, Tab moves to the next, Escape leaves it. Nothing is
written until you press Save.
</p>
</section>
)
}
bg-card, text-muted-foreground) — it inherits your theme instead of overriding it.Drop it at components/data-table-inline-edit.tsx and import it where you need the section:
import { DataTableInlineEdit } from '@/components/data-table-inline-edit'Read out of the component’s own type and signature, so this cannot drift from the source below. Every prop has a default — the component renders standalone before you pass it anything.
| Prop | Type | Default |
|---|---|---|
rows | EditableRow[] | DEFAULT_ROWS |
className | string | '' |
The same block rendered once to markup, wrapped as a file your framework compiles. Tailwind classes are framework-agnostic, so the design transfers intact — the behaviour does not.
This block is interactive. The markup below is its initial state with the event handlers stripped — you will need to re-wire the behaviour in your framework.
<!--
Inline Editable Table — markup from the Hoverlab catalog.
This is the block rendered once to HTML and wrapped as a component
file. It is not a port of the React source: the Tailwind classes carry
the design, which is the part that took the work, and they are the same
in every framework.
This block is interactive in React and the handlers are NOT here.
Buttons, toggles and menus render in their initial state and do
nothing until you wire them up.
-->
<section class="rounded-2xl border border-border bg-card text-card-foreground ">
<header class="flex flex-wrap items-center justify-between gap-3 border-b border-border p-5 sm:p-6">
<div>
<h2 class="text-base font-semibold">Account seats</h2>
<p aria-live="polite" class="mt-1 text-sm text-muted-foreground">Click a seat count or price to edit it.</p>
</div>
<div class="flex items-center gap-2">
<button type="button" disabled="" class="inline-flex items-center gap-1.5 rounded-lg border border-border px-3 py-1.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-rotate-ccw h-3.5 w-3.5" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"></path><path d="M3 3v5h5"></path></svg>Revert</button>
<button type="button" disabled="" class="inline-flex items-center gap-1.5 rounded-lg bg-primary px-3 py-1.5 text-sm font-semibold text-primary-foreground transition-opacity hover:opacity-90 disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-save h-3.5 w-3.5" aria-hidden="true"><path d="M15.2 3a2 2 0 0 1 1.4.6l3.8 3.8a2 2 0 0 1 .6 1.4V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z"></path><path d="M17 21v-7a1 1 0 0 0-1-1H8a1 1 0 0 0-1 1v7"></path><path d="M7 3v4a1 1 0 0 0 1 1h7"></path></svg>Save </button>
</div>
</header>
<div class="overflow-x-auto">
<table class="w-full min-w-[36rem] border-collapse text-start text-sm">
<thead>
<tr class="border-b border-border">
<th id="_R_0_-col-account" scope="col" class="px-5 py-2.5 font-semibold sm:px-6">Account</th>
<th id="_R_0_-col-plan" scope="col" class="px-5 py-2.5 font-semibold">Plan</th>
<th id="_R_0_-col-seats" scope="col" class="px-5 py-2.5 font-semibold">Seats</th>
<th id="_R_0_-col-monthly" scope="col" class="px-5 py-2.5 font-semibold">Monthly</th>
</tr>
</thead>
<tbody>
<tr class="border-b border-border/60 last:border-0">
<th scope="row" id="_R_0_-row-acme" class="px-5 py-3 font-medium sm:px-6">Acme Corp</th>
<td class="px-5 py-3 text-muted-foreground">Studio</td>
<td class="px-5 py-2 align-top">
<input type="text" inputMode="numeric" aria-labelledby="_R_0_-col-seats row-acme" aria-invalid="false" class="w-24 rounded-md border bg-transparent px-2 py-1 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring border-transparent hover:border-border" value="24" />
</td>
<td class="px-5 py-2 align-top">
<input type="text" inputMode="numeric" aria-labelledby="_R_0_-col-monthly row-acme" aria-invalid="false" class="w-24 rounded-md border bg-transparent px-2 py-1 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring border-transparent hover:border-border" value="1920" />
</td>
</tr>
<tr class="border-b border-border/60 last:border-0">
<th scope="row" id="_R_0_-row-globex" class="px-5 py-3 font-medium sm:px-6">Globex</th>
<td class="px-5 py-3 text-muted-foreground">Pro</td>
<td class="px-5 py-2 align-top">
<input type="text" inputMode="numeric" aria-labelledby="_R_0_-col-seats row-globex" aria-invalid="false" class="w-24 rounded-md border bg-transparent px-2 py-1 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring border-transparent hover:border-border" value="8" />
</td>
<td class="px-5 py-2 align-top">
<input type="text" inputMode="numeric" aria-labelledby="_R_0_-col-monthly row-globex" aria-invalid="false" class="w-24 rounded-md border bg-transparent px-2 py-1 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring border-transparent hover:border-border" value="640" />
</td>
</tr>
<tr class="border-b border-border/60 last:border-0">
<th scope="row" id="_R_0_-row-initech" class="px-5 py-3 font-medium sm:px-6">Initech</th>
<td class="px-5 py-3 text-muted-foreground">Studio</td>
<td class="px-5 py-2 align-top">
<input type="text" inputMode="numeric" aria-labelledby="_R_0_-col-seats row-initech" aria-invalid="false" class="w-24 rounded-md border bg-transparent px-2 py-1 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring border-transparent hover:border-border" value="51" />
</td>
<td class="px-5 py-2 align-top">
<input type="text" inputMode="numeric" aria-labelledby="_R_0_-col-monthly row-initech" aria-invalid="false" class="w-24 rounded-md border bg-transparent px-2 py-1 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring border-transparent hover:border-border" value="4080" />
</td>
</tr>
<tr class="border-b border-border/60 last:border-0">
<th scope="row" id="_R_0_-row-umbrella" class="px-5 py-3 font-medium sm:px-6">Umbrella</th>
<td class="px-5 py-3 text-muted-foreground">Team</td>
<td class="px-5 py-2 align-top">
<input type="text" inputMode="numeric" aria-labelledby="_R_0_-col-seats row-umbrella" aria-invalid="false" class="w-24 rounded-md border bg-transparent px-2 py-1 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring border-transparent hover:border-border" value="12" />
</td>
<td class="px-5 py-2 align-top">
<input type="text" inputMode="numeric" aria-labelledby="_R_0_-col-monthly row-umbrella" aria-invalid="false" class="w-24 rounded-md border bg-transparent px-2 py-1 text-sm tabular-nums transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring border-transparent hover:border-border" value="960" />
</td>
</tr>
</tbody>
</table>
</div>
<p class="border-t border-border px-5 py-3 text-xs text-muted-foreground sm:px-6">Enter commits a cell, Tab moves to the next, Escape leaves it. Nothing is written until you press Save.</p>
</section>
What each framework gets across the whole catalog — effects convert properly; this rung is markup.
The component, its props, the design tokens it expects and the command that installs it — as one prompt. Paste it into Claude, Cursor, v0 or ChatGPT and what they build around it will match the rest of the catalog instead of inventing its own system.
Want the whole screen instead of this one section? Open a page and copy it entire.
All of them free to read, copy and install — no account, no locked tiles, no watermarked preview. The whole catalog is open, and so are the API and the CLI.
Browse Data TablesCopying the code is free. Putting it in client work or a paid product is what Pro is for — the licence, not the access.
Type-aware sorting with aria-sort, a header checkbox that goes properly indeterminate, and selection that survives a re-sort.
Search, filter count and view switch, with active filters shown as removable chips so a filtered table never looks unfiltered.
Page size, range summary and a fixed-width number window that stays the same size from page 1 to page 200.
Rows that open into a full-width detail panel via colSpan, with aria-expanded and aria-controls wired to the detail row.
Reordering that works from the keyboard rather than by drag alone, pinned columns that cannot be hidden or moved, and a guard so the last visible column cannot be unchecked into a blank table.
The selection bar that asks the question most tables skip: this page, or all 1,284 rows the filter matches. Typed confirmation above a threshold, an undo with the window counting down, and a select-all that names its own scope.
Collapsible group headers that carry their own subtotals while collapsed, with a grand total that sums every row rather than only the visible ones.