Inventory with reviewed batch saves
Use the same typed source controller for another domain: inventory quantities, dates, approval and multiple owners. Changes across pages stay in one draft until review.
Keyboard
- Arrow keys move between grid cells. Enter opens the appropriate editor; Escape cancels it.
- Open record edits the complete item, including multiple owners and approval.
- Review changes opens one batch confirmation; Undo draft reverses the last draft operation.
- Save current view stores view settings only. Share current view puts filters and layout in the URL, never draft values.
States covered
- typed inventory schema
- paged source
- canonical dates and booleans
- multiple owners
- explicit batch review
- host-owned saved views
- safe share URL
- one-step draft undo
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-resource-inventory-drafts.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 { useEffect, useState } from 'react';
import { ResourceWorkspace, createMemoryResourceDataSource, parseResourceWorkspaceView, serializeResourceWorkspaceView, resourceWorkspaceViewUrl, type ResourceFilterField, type ResourceWorkspaceView, type SavedResourceView } from '@bld8/blocks';
import { UIProvider, type DataColumn } from '@bld8/ui';
type Item = { id: string; revision: number; name: string; stock: number; restock: string; approved: boolean; owners: string[] };
const owners = [{ value: 'maya', label: 'Maya Chen' }, { value: 'sam', label: 'Sam Rivera' }, { value: 'oliver', label: 'Oliver Reed' }];
const columns: DataColumn<Item>[] = [
{ key: 'id', label: 'SKU', type: 'text', hideable: false, minWidth: 120 },
{ key: 'name', label: 'Product', type: 'text', editable: true, required: true, minLength: 3, minWidth: 230 },
{ key: 'stock', label: 'Stock', type: 'number', editable: true, min: 0, max: 10000 },
{ key: 'restock', label: 'Restock date', type: 'date', editable: true },
{ key: 'approved', label: 'Approved', type: 'boolean', editable: true },
{ key: 'owners', label: 'Owners', type: 'multi-select', editable: true, options: owners, maxSelections: 3, minWidth: 220 }
];
const fields: ResourceFilterField<Item>[] = [
{ id: 'name', label: 'Product', kind: 'text' }, { id: 'stock', label: 'Stock', kind: 'number' },
{ id: 'restock', label: 'Restock date', kind: 'date' },
{ id: 'approved', label: 'Approved', kind: 'select', options: [{ value: 'true', label: 'Approved' }, { value: 'false', label: 'Needs approval' }] }
];
const initial: Item[] = Array.from({ length: 16 }, (_, index) => ({ id: 'SKU-' + (101 + index), revision: 1, name: ['Studio headphones', 'Desk speakers', 'Portable display', 'USB microphone'][index % 4] + (index > 3 ? ' ' + (index + 1) : ''), stock: 12 + index * 3, restock: '2026-12-' + String(index + 1).padStart(2, '0'), approved: index % 2 === 0, owners: index % 3 ? ['sam'] : ['maya', 'oliver'] }));
export default function InventoryDraftsExample() {
const [source] = useState(() => createMemoryResourceDataSource({ rows: initial, columns, fields, searchKeys: ['id', 'name'] }));
const [views, setViews] = useState<SavedResourceView[]>([]), [restored, setRestored] = useState<{ view?: ResourceWorkspaceView } | null>(null);
useEffect(() => {
const query = new URL(window.location.href).searchParams.get('inventoryView');
const view = query ? parseResourceWorkspaceView(query, columns, fields) ?? undefined : undefined;
try { const saved = localStorage.getItem('inventory-example-view'); if (saved) { const parsed = parseResourceWorkspaceView(saved, columns, fields); if (parsed) setViews([{ id: 'saved', label: 'My inventory view', view: parsed }]); } } catch { /* Host storage can be unavailable. */ }
setRestored({ view });
}, []);
if (!restored) return <p role="status">Preparing inventory view…</p>;
return <UIProvider><ResourceWorkspace dataSource={source} columns={columns} fields={fields} defaultView={restored.view} savedViews={views}
labels={{ label: 'Inventory workspace', title: 'Keep stock changes together.', searchLabel: 'Search inventory', searchPlaceholder: 'SKU or product…', gridLabel: 'Inventory data grid', recordName: 'Item', emptyTitle: 'No matching inventory.' }}
onSaveView={view => { localStorage.setItem('inventory-example-view', serializeResourceWorkspaceView(view, columns, fields)); setViews([{ id: 'saved', label: 'My inventory view', view }]); }}
onShareView={view => { const href = resourceWorkspaceViewUrl(window.location.href, view, columns, fields, 'inventoryView'); window.history.replaceState(null, '', href); }}
/></UIProvider>;
}