Dates, approval and people in one draft
Date-only values, native checkbox edits and multiple assignees share validation, history and one host-owned save boundary.
Keyboard
- Enter or F2 opens a cell. Dates and assignees keep a local buffer until Apply changes.
- In an approval cell, Space toggles the checkbox; Enter or Tab commits and Escape cancels.
- Escape dismisses an inner calendar or option list first. Cancel or Escape from the editor discards its buffer.
- Copy and paste use canonical date strings, true/false and JSON arrays of option IDs, not display names.
States covered
- date editor
- boolean editor
- multiple assignees
- local buffers
- undo and redo
- canonical clipboard
- atomic local save
- revision conflict
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-grid-editor-mixed.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import { useState } from 'react';
import { Button, DataGrid, UIProvider, dataValuesEqual, parseDataValue, validateDataChanges, type DataCellError, type DataColumn, type DataChange, type DataEdit } from '@bld8/ui';
type Delivery = { id: string; revision: number; title: string; due: string; approved: boolean; assignees: string[] };
const people = [
{ value: 'maya', label: 'Maya Chen', description: 'Interface systems' },
{ value: 'oliver', label: 'Oliver Reed', description: 'Frontend platform' },
{ value: 'sam', label: 'Sam Rivera', description: 'Accessibility' },
{ value: 'retired', label: 'Former contributor', disabled: true }
];
const columns: DataColumn<Delivery>[] = [
{ key: 'id', label: 'Record', type: 'text', hideable: false, width: 104, minWidth: 104 },
{ key: 'title', label: 'Delivery title', type: 'text', editable: true, required: true, maxLength: 100, minWidth: 220 },
{ key: 'due', label: 'Due date', type: 'date', editable: true, minDate: '2026-01-01', maxDate: '2027-12-31', width: 170 },
{ key: 'approved', label: 'Approved', type: 'boolean', editable: true, width: 128 },
{ key: 'assignees', label: 'Assignees', type: 'multi-select', editable: true, required: true, maxSelections: 3, options: people, minWidth: 260 }
];
const initial: Delivery[] = [
{ id: 'REC-1', revision: 1, title: 'Keyboard focus review', due: '2026-11-20', approved: false, assignees: ['maya'] },
{ id: 'REC-2', revision: 1, title: 'Responsive field spacing', due: '', approved: true, assignees: ['oliver', 'sam'] }
];
const clone = (rows: Delivery[]) => rows.map(row => ({ ...row, assignees: [...row.assignees] }));
export default function DeliveryEditorExample() {
const [saved, setSaved] = useState(() => clone(initial)), [rows, setRows] = useState(() => clone(initial));
const [past, setPast] = useState<Delivery[][]>([]), [future, setFuture] = useState<Delivery[][]>([]);
const [errors, setErrors] = useState<DataCellError[]>([]), [message, setMessage] = useState('Changes stay in this example.');
const changes: DataChange<Delivery>[] = rows.flatMap(row => {
const base = saved.find(item => item.id === row.id)!;
const values: Partial<Omit<Delivery, 'id' | 'revision'>> = {};
for (const column of columns.filter(column => column.editable)) if (!dataValuesEqual(row[column.key], base[column.key])) Object.assign(values, { [column.key]: row[column.key] });
return Object.keys(values).length ? [{ id: row.id, expectedRevision: row.revision, values }] : [];
});
function edit(edits: DataEdit<Delivery>[]) {
const next = clone(rows), invalid: DataCellError[] = [];
for (const edit of edits) {
const row = next.find(row => row.id === edit.id), column = columns.find(column => column.key === edit.key);
if (!row || !column) { invalid.push({ id: edit.id, key: edit.key, message: 'Choose a loaded editable field.' }); continue; }
const parsed = parseDataValue(column, edit.value);
if (parsed.error) invalid.push({ id: edit.id, key: edit.key, message: parsed.error }); else Object.assign(row, { [edit.key]: parsed.value });
}
setErrors(invalid); if (invalid.length) { setMessage('The whole edit was kept out of the draft. Correct its values and retry.'); return; }
if (next.every((row, index) => columns.every(column => dataValuesEqual(row[column.key], rows[index][column.key])))) return;
setPast(current => [...current.slice(-49), clone(rows)]); setFuture([]); setRows(next); setMessage('Draft updated. Review before saving.');
}
function undo() { const previous = past.at(-1); if (!previous) return; setFuture(current => [clone(rows), ...current]); setRows(clone(previous)); setPast(current => current.slice(0, -1)); setErrors([]); }
function redo() { const next = future[0]; if (!next) return; setPast(current => [...current, clone(rows)]); setRows(clone(next)); setFuture(current => current.slice(1)); setErrors([]); }
function save() {
const invalid = validateDataChanges(saved, columns, changes); setErrors(invalid); if (invalid.length) { setMessage('Correct the draft before saving.'); return; }
if (changes.some(change => saved.find(row => row.id === change.id)?.revision !== change.expectedRevision)) { setMessage('A revision changed. Restore the saved records before retrying; nothing was saved.'); return; }
const next = clone(rows).map(row => changes.some(change => change.id === row.id) ? { ...row, revision: row.revision + 1 } : row);
setSaved(clone(next)); setRows(next); setPast([]); setFuture([]); setMessage('Saved together in this local example.');
}
const dirty = changes.flatMap(change => Object.keys(change.values).map(key => ({ id: change.id, key })));
return <UIProvider><div style={{ display: 'grid', gap: 16, minWidth: 0 }}>
<DataGrid label="Delivery editor grid" rows={rows} columns={columns} onEdit={edit} cellErrors={errors} dirtyCells={dirty} onUndo={undo} onRedo={redo} canUndo={!!past.length} canRedo={!!future.length} layoutControls resizableColumns />
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
<Button size="sm" disabled={!changes.length} onClick={save}>Save delivery draft</Button>
<Button size="sm" variant="secondary" onClick={() => { setRows(clone(saved)); setPast([]); setFuture([]); setErrors([]); setMessage('Restored the saved records.'); }}>Restore saved records</Button>
<Button size="sm" variant="ghost" onClick={() => { setSaved(current => current.map((row, index) => index === 0 ? { ...row, revision: row.revision + 1 } : row)); setMessage('A local external revision was simulated. Save will reject stale changes atomically.'); }}>Simulate external revision</Button>
</div>
<p role="status" className="bui-hint">{message}</p>
<output aria-label="Delivery canonical values" style={{ fontSize: 12, overflowWrap: 'anywhere' }}>{JSON.stringify(rows.map(({ id, due, approved, assignees }) => ({ id, due, approved, assignees })))}</output>
<p className="bui-hint">This example keeps records in memory. Your application owns authentication, durable storage, revision checks and atomic writes.</p>
</div></UIProvider>;
}