Cómo funciona
A card with no outer border, a muted 12px header over the only hairline and rows as soft pills on hover, with an optional title row and footer panel. The table is one grid with a single Tab stop and arrow-key movement. Double-click, Enter or F2 starts editing with the old content selected; typing replaces it. Each cell has a ring layer under its text, inset from the cell edges: a faint ring on hover over an editable cell, a 2px accent ring that pops in when selected, and while editing a surface-coloured field with a soft accent halo. The editor is absolutely positioned over the cell while the old text stays hidden but in place, so row heights never change. Enter saves and moves down, Tab saves and moves to the next editable cell, Esc cancels. A failed validation keeps editing, turns the ring danger-coloured, shakes and shows the message in a tinted pill below the cell. The component stores no data.
Úsalo para
- A batch of similar data edited cell by cell: quote and invoice lines, bulk price changes, stock levels, config tables.
- Quick runs of edits: Enter goes down and Tab goes right, with no Edit and Save buttons per row.
- Simple per-cell rules (range, length, number) whose errors fit in one line.
Busca otra opción cuando
- Rows with many related fields, image uploads or date ranges: edit the whole row in a form in a drawer or dialog.
- Changes that need review before taking effect: use an edit-and-submit flow so nobody assumes it is live.
- An occasional single edit: an Edit button on a detail page is enough.
Uso
import { useState } from 'react'
import { EditTable } from './EditTable.jsx'
const money = (n) => `$${n.toLocaleString('en-US')}`
const columns = [
{ key: 'item', title: 'Item' },
{
key: 'qty',
title: 'Qty',
width: 56,
align: 'right',
editor: 'number',
// Return text = invalid, with that message; return null = valid
validate: (v) => (Number.isInteger(v) && v >= 1 && v <= 99 ? null : 'Quantity is a whole number from 1 to 99'),
},
{ key: 'rate', title: 'Rate', width: 88, align: 'right', editor: 'number', format: money },
{ key: 'amount', title: 'Amount', width: 96, align: 'right', editable: false, render: (r) => money(r.qty * r.rate) },
]
export function QuoteEditor() {
const [rows, setRows] = useState([
{ id: 'l1', item: 'Brand workshop', qty: 1, rate: 1200 },
{ id: 'l2', item: 'Icon set', qty: 24, rate: 45 },
])
const total = rows.reduce((s, r) => s + r.qty * r.rate, 0)
return (
<EditTable
title="Quote"
subtitle="Q-2041"
columns={columns}
rows={rows}
rowKey={(r) => r.id}
// The component stores no data: update rows here (and call a save API if needed)
onCellChange={(id, key, value) => setRows((list) => list.map((r) => (r.id === id ? { ...r, [key]: value } : r)))}
footer={<strong style={{ marginLeft: 'auto' }}>{money(total)}</strong>}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
columns | Column[] | — | Column definitions: key, title, width, align, editable (default true), editor ('text' | 'number' | 'select'), options, validate (error text or null), format, parse and render. |
rows | T[] | — | Data rows. The component stores no data; update them after onCellChange and pass them back. |
rowKey | (row) => string | number | — | A unique id for each row. |
onCellChange | (rowKey, columnKey, value, row) => void | — | Called when a cell changes and passes validation; not called if the value is unchanged. number columns give a number, select columns the option's original value. |
flashOnSave | boolean | true | Flashes a wash of the accent on a cell after saving. |
emptyText | ReactNode | 'No data' | Text shown when there are no rows. |
title | ReactNode | — | Card title shown in the title row. A string title also names the table for screen readers when label is not given. |
subtitle | ReactNode | — | Muted qualifier after the title, such as a period ("This week"). |
icon | ReactNode | — | Icon before the title, drawn in a tinted 32px rounded square. Decorative (aria-hidden). |
actions | ReactNode | — | Controls at the right end of the title row (buttons, a switch, a menu). |
footer | ReactNode | — | Content for a tinted panel at the bottom of the card, such as a total that follows the edits. |
label | string | — | Table name for screen readers (the <caption>). Defaults to title when title is a string. |
labels | object | — | Replaces built-in text (notNumber, readOnly, editing, unchanged, saved, cancelled, editLabel). Shallow-merged over the English defaults; most are functions of the column title. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root <div>. |
Accesibilidad
- The table is role="grid" with gridcells and a single Tab stop; arrows move, Home and End go to row ends, Ctrl+Home and Ctrl+End to the table ends.
- Enter or F2 starts editing, Enter saves, Tab and Shift+Tab save and move on, Esc cancels without closing an outer dialog.
- Read-only columns have aria-readonly and weaker text; trying to edit one announces that it is read-only.
- A failed validation sets aria-invalid with aria-describedby on the error; entering edit mode, saving and errors are announced through aria-live.
- States differ by ring thickness, halo and message text as well as color; an Enter that picks an IME candidate does not save. Reduced motion skips the shake, the ring pop and the flash; on touch the header is 44px and tapping a selected cell again starts editing.
Movimiento
Selection ring: opacity 160ms and scale 0.94 to 1 with a light spring over 360ms. Validation shake: ±4px over 240ms. Saved: an accent wash swells in and fades out over 900ms (opacity and transform only). Error tip: fades in over 240ms. In the demo the total counts up from 0 on first view and counts across after each edit.
Añádelo a tu proyecto
Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.
EditTable.jsxedit-table.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.





