Edit rows in forms
Save several fields of one row from a form as one validated, undoable grid change.
Inline editors change one cell. A form changes several fields of one row. Call actions.editRows with the form values to write them as one grid change. The keys of changes are column ids. Each column's validate runs, validateRow runs on the row, onDataChange and onSave fire once, and undo records one entry.
Before you start:
- Give the grid a
ref. The handle holds theactionsobject. - Mount the history add-on to undo form saves. See Undo and redo.
Steps
-
Put each column rule in one place and use it in the column. This file is
columns.ts:import { defineColumns } from "@/components/data-grid/data-grid"; export type Order = { id: string; customerName: string; price: number }; export const priceRule = (value: unknown) => typeof value === "number" && value < 0 ? "Price cannot be negative" : null; export const columns = defineColumns<Order>()([ { id: "customerName", header: "Customer", accessorKey: "customerName" }, { id: "price", header: "Price", accessorKey: "price", type: "number", validate: priceRule, }, ]); -
Build the form from the row in
order-form.tsx. Run the same rule on the field, so the form shows the error before the grid sees the value:"use client"; import { useState, type FormEvent } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { priceRule, type Order } from "./columns"; export function OrderForm({ order, onSave, }: { order: Order; onSave: (changes: Partial<Order>) => void; }) { const [customerName, setCustomerName] = useState(order.customerName); const [price, setPrice] = useState(String(order.price)); const error = price.trim() === "" ? "Required" : priceRule(Number(price)); function submit(event: FormEvent) { event.preventDefault(); if (!error) onSave({ customerName, price: Number(price) }); } return ( <form onSubmit={submit}> <Input aria-label="Customer" value={customerName} onChange={(e) => setCustomerName(e.target.value)} /> <Input aria-label="Price" type="number" value={price} onChange={(e) => setPrice(e.target.value)} /> {error && <p>{error}</p>} <Button type="submit">Save</Button> </form> ); } -
Call
editRowsfrom the submit handler. This file isorders.tsx:"use client"; import { useRef, useState } from "react"; import { DataGrid, type DataGridApi } from "@/components/data-grid/data-grid"; import { columns, type Order } from "./columns"; import { OrderForm } from "./order-form"; export function Orders({ initial }: { initial: Order[] }) { const gridRef = useRef<DataGridApi<Order> | null>(null); const [orders, setOrders] = useState<readonly Order[]>(initial); const [activeId, setActiveId] = useState<string | null>(null); const active = orders.find((order) => order.id === activeId); return ( <> <DataGrid ref={gridRef} data={orders} columns={columns} onDataChange={setOrders} onActiveCellChange={(cell) => setActiveId(cell?.rowId ?? null)} /> {active && ( <OrderForm key={active.id} order={active} onSave={(changes: Partial<Order>) => gridRef.current?.actions.editRows([ { rowId: active.id, changes }, ]) } /> )} </> ); }
Result
Submit writes every accepted field in one change. editRows writes as a user edit: undo reverts the whole form save, and onSave sees it like an inline edit. The row moves to its sorted position at once.
A field that fails a blocking validate is not written. A column with validationMode: "warn" writes the value and flags the cell. The grid applies the other fields and keeps the old value in the failed one. The returned verdict lists each skipped field with a reason, such as "invalid" or "readonly", and onValidationError reports each refused cell with source: "edit". A readOnly grid writes nothing. If a column has an async schema, the call returns pending: true and a settled promise.
Related
- Validation:
validateandvalidateRow. - Undo and redo: one entry per
editRowscall. - State and view index: why
editRowsaddresses rows by row id. - Stream updates: the full verdict that
editRowsreturns. - Open a detail panel: put the form in a side sheet.