Inventory Grid
An editable product inventory on Niko Grid: typed cells, inline validation, low-stock markers, live totals, an unsaved changes bar with Save and Discard, add, duplicate and delete product, undo and redo, and a virtualized body.
Built only from Niko Table pieces and shadcn/ui components. Install it, then swap the sample data for your own.
Installation
pnpm dlx shadcn@latest add @niko-table/inventory-gridPreview
"use client"
import * as React from "react"import { Copy, Loader2, PackageSearch, TriangleAlert, Trash2, MoreHorizontal,} from "lucide-react"import { DataTableRoot } from "@/components/niko-table/core/data-table-root"import { DataTable } from "@/components/niko-table/core/data-table"import { DataTableVirtualizedBody, DataTableVirtualizedEmptyBody, DataTableVirtualizedHeader,} from "@/components/niko-table/core/data-table-virtualized-structure"import { DataTableToolbarSection } from "@/components/niko-table/components/data-table-toolbar-section"import { DataTableSearchFilter } from "@/components/niko-table/components/data-table-search-filter"import { DataTableFacetedFilter } from "@/components/niko-table/components/data-table-faceted-filter"import { DataTableClearFilter } from "@/components/niko-table/components/data-table-clear-filter"import { DataTableColumnResize } from "@/components/niko-table/components/data-table-column-resize"import { DataTableEmptyDescription, DataTableEmptyFilteredMessage, DataTableEmptyIcon, DataTableEmptyMessage, DataTableEmptyTitle,} from "@/components/niko-table/components/data-table-empty-state"import { DataTableRowMenuScope, RowMenuItem, useDataTableRow,} from "@/components/niko-table/components/data-table-row-menu"import { DataTableRowContextMenuSlot } from "@/components/niko-table/components/data-table-row-context-menu-slot"import type { DataTableColumnDef } from "@/components/niko-table/types"import type { CellEditorProps } from "@/components/niko-table/grid/cells/cell-props"import { GridCheckboxCell } from "@/components/niko-table/grid/cells/grid-checkbox-cell"import { GridDateCell } from "@/components/niko-table/grid/cells/grid-date-cell"import { GridSelectCell } from "@/components/niko-table/grid/cells/grid-select-cell"import { GridNumberCell, GridTextCell,} from "@/components/niko-table/grid/cells/grid-text-cell"import { DataGridClipboard } from "@/components/niko-table/grid/components/grid-clipboard"import { DataGridFillHandle } from "@/components/niko-table/grid/components/grid-fill-handle"import { DataGridStatusBar } from "@/components/niko-table/grid/components/grid-status-bar"import { DataGridAddRows, DataGridRedo, DataGridToolbar, DataGridUndo,} from "@/components/niko-table/grid/components/grid-toolbar"import { DataGrid } from "@/components/niko-table/grid/core/data-grid"import { DataGridCell, useDataGridContext,} from "@/components/niko-table/grid/core/data-grid-context"import { useDataGrid } from "@/components/niko-table/grid/hooks/use-data-grid"import { useGridChanges } from "@/components/niko-table/grid/hooks/use-grid-changes"import type { GridRow } from "@/components/niko-table/grid/types/grid-cell"import { Button } from "@/components/niko-table/primitives/button"import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger,} from "@/components/niko-table/primitives/dropdown-menu"import { cn } from "@/lib/utils"import { categoryOptions, products } from "./inventory-data"import { COLUMN_IDS, createEmptyRow, hasInvalidCell, isLowStock, rawOf, resolveCell, rowsEqual, summarize, toRow, usd, type ColumnId,} from "./inventory-format"
type ChangeSet = { created: GridRow[]; updated: GridRow[]; deleted: string[] }
/** Pretend to POST the change set. Pass your own `persist` that calls your API; reject to fail. */async function fakePersist(changeSet: ChangeSet) { void changeSet await new Promise(resolve => setTimeout(resolve, 600))}
const INITIAL_ROWS = products.map(toRow)
// Every body row is 37px: the cell controls are h-9 (36px) plus the 1px row// border. Keep in step if you change the cell height.const ROW_HEIGHT = 37
const gridCellClassName = () => "border-border border-r p-0 align-middle last:border-r-0"
const COLUMNS: { id: ColumnId; label: string; width: number }[] = [ { id: "sku", label: "SKU", width: 110 }, { id: "name", label: "Product", width: 220 }, { id: "category", label: "Category", width: 130 }, { id: "price", label: "Price", width: 110 }, { id: "stock", label: "In stock", width: 120 }, { id: "reorderAt", label: "Reorder at", width: 120 }, { id: "active", label: "Active", width: 90 }, { id: "restockDate", label: "Restock date", width: 150 },]
function ProductCell({ id, label, ...p}: CellEditorProps & { id: ColumnId; label: string }) { const resolve = (raw: string) => resolveCell(id, raw) switch (id) { case "category": return ( <GridSelectCell {...p} options={categoryOptions} placeholder="Category" displayLabel={p.cell.value ?? undefined} /> ) case "price": return ( <GridNumberCell {...p} resolve={resolve} placeholder={label} format={raw => usd.format(Number(raw))} /> ) case "stock": case "reorderAt": return <GridNumberCell {...p} resolve={resolve} placeholder={label} /> case "active": return <GridCheckboxCell {...p} aria-label={label} /> case "restockDate": return <GridDateCell {...p} placeholder="No date" /> default: return <GridTextCell {...p} resolve={resolve} placeholder={label} /> }}
/* * Row actions: one small piece per action, each reading its row from context. * `InventoryGrid` composes them into ONE fragment that both the "⋯" dropdown * and the right-click menu render. */function DuplicateItem({ makeSku, announce,}: { makeSku: () => string announce: (note: string) => void}) { const row = useDataTableRow<GridRow>() const { grid } = useDataGridContext<GridRow>() return ( <RowMenuItem disabled={grid.rows.length >= grid.maxRows} onClick={() => { const sku = makeSku() const name = rawOf(row, "name") const copy: GridRow = { ...row, id: sku, sku: resolveCell("sku", sku), name: resolveCell("name", name ? `${name} (copy)` : ""), } grid.updateRows(rows => { const at = rows.findIndex(r => r.id === row.id) const next = rows.slice() next.splice(at === -1 ? rows.length : at + 1, 0, copy) return next }) announce(`Duplicated ${name} as ${sku}`) }} > <Copy className="size-4" /> Duplicate </RowMenuItem> )}
function DeleteItem({ announce }: { announce: (note: string) => void }) { const row = useDataTableRow<GridRow>() const { grid } = useDataGridContext<GridRow>() return ( <RowMenuItem variant="destructive" // The grid always keeps one row. disabled={grid.rows.length <= 1} onClick={() => { grid.removeRow(row.id) announce(`Deleted ${rawOf(row, "name") || row.id}`) }} > <Trash2 className="size-4" /> Delete product </RowMenuItem> )}
function useInventoryColumns(rowMenu: React.ReactNode) { return React.useMemo<DataTableColumnDef<GridRow>[]>( () => [ ...COLUMNS.map(({ id, label, width }): DataTableColumnDef<GridRow> => ({ id, accessorFn: (row: GridRow) => rawOf(row, id), header: label, size: width, enableColumnFilter: id === "category", // Search matches name and SKU only, like the placeholder says. enableGlobalFilter: id === "name" || id === "sku", meta: { label, ...(id === "category" ? { options: categoryOptions } : {}), }, cell: ctx => id === "sku" ? ( <span className="flex h-9 items-center px-2 font-mono text-xs text-muted-foreground"> {rawOf(ctx.row.original, "sku")} </span> ) : ( <DataGridCell row={ctx.row.original} columnId={id}> {(p: CellEditorProps) => id === "stock" ? ( <div className="relative"> <ProductCell id={id} label={label} {...p} /> {isLowStock(ctx.row.original) ? ( <span className="pointer-events-none absolute inset-y-0 left-2 flex items-center text-warning"> <TriangleAlert className="size-3.5" aria-hidden="true" /> <span className="sr-only">Low stock</span> </span> ) : null} </div> ) : ( <ProductCell id={id} label={label} {...p} /> ) } </DataGridCell> ), })), { id: "actions", size: 56, header: () => <span className="sr-only">Actions</span>, enableSorting: false, enableHiding: false, cell: ({ row }) => ( <div className="flex justify-center"> <DropdownMenu> <DropdownMenuTrigger asChild> <Button variant="ghost" size="icon" className="size-8" aria-label={`Open actions for ${row.original.id}`} > <MoreHorizontal className="size-4" /> </Button> </DropdownMenuTrigger> <DropdownMenuContent align="end"> <DataTableRowMenuScope row={row.original} surface="dropdown"> {rowMenu} </DataTableRowMenuScope> </DropdownMenuContent> </DropdownMenu> </div> ), }, ], [rowMenu], )}
function Stat({ label, value, warn,}: { label: string value: string warn?: boolean}) { return ( <div className="flex flex-col"> <span className="text-xs text-muted-foreground">{label}</span> <span className={cn( "text-sm font-semibold tabular-nums", warn && "text-warning", )} > {value} </span> </div> )}
export default function InventoryGrid({ persist = fakePersist,}: { persist?: (changeSet: ChangeSet) => Promise<void>}) { const skuCounter = React.useRef(0) const makeSku = React.useCallback( () => `NEW-${String(++skuCounter.current).padStart(3, "0")}`, [], ) const grid = useDataGrid<GridRow>({ // SKU is left out so it is a plain, read-only table cell: navigation, // paste, fill and clear all skip it. columnIds: COLUMN_IDS.filter(id => id !== "sku"), createEmptyRow, initialRows: INITIAL_ROWS, makeId: makeSku, }) const changes = useGridChanges(grid, { initialRows: INITIAL_ROWS, isEqual: rowsEqual, }) const [saving, setSaving] = React.useState(false) const [note, setNote] = React.useState("") // What "Discard" goes back to: the seed, then the rows as of the last save. const baseline = React.useRef<GridRow[]>(INITIAL_ROWS)
const summary = React.useMemo(() => summarize(grid.rows), [grid.rows]) const invalidCells = React.useMemo( () => grid.rows.reduce( (sum, row) => sum + (hasInvalidCell(row) ? COLUMN_IDS.filter(id => { const cell = row[id] return typeof cell === "object" && cell.status === "invalid" }).length : 0), 0, ), [grid.rows], ) const unsaved = React.useMemo(() => { const { created, updated, deleted } = changes.getChangeSet() return created.length + updated.length + deleted.length // `changes` also changes whenever the dirty set does. }, [changes, grid.rows]) // eslint-disable-line react-hooks/exhaustive-deps
const containerRef = React.useRef<HTMLDivElement>(null)
const onSave = React.useCallback(async () => { const { created, updated, deleted } = changes.getChangeSet() const count = created.length + updated.length + deleted.length setSaving(true) setNote(`Saving ${count} ${count === 1 ? "change" : "changes"}…`) try { await persist({ created, updated, deleted }) // The server now holds exactly what was sent. Make that the baseline and // let the grid re-compare: anything changed, undone or edited while // saving shows up as an unsaved change again. const sent = new Map(baseline.current.map(row => [row.id, row])) for (const row of [...created, ...updated]) sent.set(row.id, row) for (const id of deleted) sent.delete(id) baseline.current = [...sent.values()] changes.reset(baseline.current) // `reset` clears dirty state; a no-history commit with fresh row objects // makes the hook re-derive it from the current rows. grid.updateRows(rows => rows.map(row => ({ ...row })), { history: false, }) setNote(`Saved ${count} ${count === 1 ? "change" : "changes"}`) } catch { // Nothing was reconciled, so every change stays unsaved. setNote("Save failed. Your changes are still here.") } finally { setSaving(false) } }, [changes, grid, persist])
// The Save button unmounts when the bar clears; keep focus in the grid. const wasSaving = React.useRef(false) React.useEffect(() => { if (wasSaving.current && !saving) { const active = document.activeElement if (!active || active === document.body) { containerRef.current ?.querySelector<HTMLElement>('[role="grid"]') ?.focus() } } wasSaving.current = saving }, [saving])
const onDiscard = React.useCallback(() => { grid.updateRows(() => baseline.current) setNote("Discarded unsaved changes") }, [grid])
const rowMenu = React.useMemo( () => ( <> <DuplicateItem makeSku={makeSku} announce={setNote} /> <DeleteItem announce={setNote} /> </> ), [makeSku], ) const columns = useInventoryColumns(rowMenu)
return ( <div ref={containerRef} className="w-full rounded-xl border bg-card p-4 text-card-foreground shadow-sm" > <DataTableRoot data={grid.rows} columns={columns} getRowId={row => row.id} > <DataGrid grid={grid} className="flex flex-col gap-4 outline-none"> <DataTableColumnResize /> <DataGridClipboard resolveCell={resolveCell} /> <DataGridFillHandle /> <div className="flex flex-wrap items-end justify-between gap-3"> <h2 className="text-base font-semibold">Inventory</h2> <div className="flex gap-6"> <Stat label="Products" value={String(summary.products)} /> <Stat label="Low stock" value={String(summary.lowStock)} warn={summary.lowStock > 0} /> <Stat label="Inventory value" value={usd.format(summary.cents / 100)} /> </div> </div> <DataTableToolbarSection className="flex-wrap px-0"> <DataTableSearchFilter placeholder="Search by name or SKU..." className="min-w-48 flex-none sm:w-64" /> <DataTableFacetedFilter accessorKey="category" multiple /> <DataTableClearFilter /> <DataGridToolbar className="ml-auto"> <DataGridUndo /> <DataGridRedo /> <DataGridAddRows count={1} label="Add product" /> </DataGridToolbar> </DataTableToolbarSection> {unsaved > 0 ? ( <div className="flex flex-wrap items-center gap-2 rounded-lg border bg-muted/40 px-3 py-2"> <span className="text-sm font-medium tabular-nums"> {unsaved} unsaved {unsaved === 1 ? "change" : "changes"} </span> {invalidCells > 0 ? ( <span className="text-sm text-destructive"> Fix {invalidCells} invalid{" "} {invalidCells === 1 ? "cell" : "cells"} to save </span> ) : null} <div className="ml-auto flex gap-2"> <Button size="sm" variant="outline" onClick={onDiscard} disabled={saving} > Discard </Button> <Button size="sm" onClick={onSave} disabled={saving || invalidCells > 0 || unsaved === 0} > {saving ? <Loader2 className="size-4 animate-spin" /> : null} Save </Button> </div> </div> ) : null} <DataTable maxHeight={520}> <DataTableVirtualizedHeader /> <DataTableVirtualizedBody<GridRow> estimateSize={ROW_HEIGHT} fixedRowHeight getCellClassName={gridCellClassName} > <DataTableRowContextMenuSlot> {rowMenu} </DataTableRowContextMenuSlot> <DataTableVirtualizedEmptyBody> <DataTableEmptyMessage> <DataTableEmptyIcon> <PackageSearch className="size-12" /> </DataTableEmptyIcon> <DataTableEmptyTitle>No products yet</DataTableEmptyTitle> <DataTableEmptyDescription> Add a product to get started. </DataTableEmptyDescription> </DataTableEmptyMessage> <DataTableEmptyFilteredMessage> <DataTableEmptyIcon> <PackageSearch className="size-12" /> </DataTableEmptyIcon> <DataTableEmptyTitle> No matching products </DataTableEmptyTitle> <DataTableEmptyDescription> Try another search, or clear the filters. </DataTableEmptyDescription> </DataTableEmptyFilteredMessage> </DataTableVirtualizedEmptyBody> </DataTableVirtualizedBody> </DataTable> <DataGridStatusBar /> <p className="text-xs text-muted-foreground" aria-live="polite"> {note} </p> </DataGrid> </DataTableRoot> </div> )}import type { CellState, GridRow,} from "@/components/niko-table/grid/types/grid-cell"import { categories, type Product } from "./inventory-data"
export const COLUMN_IDS = [ "sku", "name", "category", "price", "stock", "reorderAt", "active", "restockDate",] as constexport type ColumnId = (typeof COLUMN_IDS)[number]
export const usd = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD",})
const WHOLE_NUMBER = /^\d+$/const ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})$/
function isRealDate(raw: string) { const m = ISO_DATE.exec(raw) if (!m) return false const [y, mo, d] = [Number(m[1]), Number(m[2]), Number(m[3])] const date = new Date(Date.UTC(y, mo - 1, d)) return ( date.getUTCFullYear() === y && date.getUTCMonth() === mo - 1 && date.getUTCDate() === d )}
const invalid = (raw: string, error: string): CellState<string> => ({ raw, value: null, status: "invalid", error,})const valid = (raw: string): CellState<string> => ({ raw, value: raw, status: "valid",})
/** * The one function the grid asks for: raw text in, a validated cell out. * Rules: name required, price above 0, stock and reorder level whole numbers * of 0 or more. Used by every editor, by paste, and for new rows. */export function resolveCell(columnId: string, raw: string): CellState<string> { const text = raw.trim() switch (columnId as ColumnId) { case "name": return text ? valid(raw) : invalid(raw, "Name is required") case "category": if (text === "") return { raw, value: null, status: "empty" } return (categories as string[]).includes(text) ? valid(text) : invalid(raw, `Pick one of: ${categories.join(", ")}`) case "price": { if (!/^\d+(\.\d+)?$/.test(text) || Number(text) <= 0) return invalid(raw, "Price must be above $0") return /^\d+(\.\d{1,2})?$/.test(text) ? valid(text) : invalid(raw, "Use at most 2 decimals") } case "stock": case "reorderAt": return WHOLE_NUMBER.test(text) && Number.isSafeInteger(Number(text)) ? valid(text) : invalid(raw, "Enter a whole number, 0 or more") case "active": { const on = ["true", "1", "yes"].includes(text.toLowerCase()) return valid(String(on)) } case "restockDate": if (text === "") return { raw, value: null, status: "empty" } return isRealDate(text) ? valid(text) : invalid(raw, "Use a real date as YYYY-MM-DD") default: return valid(raw) }}
export function toRow(product: Product): GridRow { return { id: product.sku, sku: valid(product.sku), name: resolveCell("name", product.name), category: resolveCell("category", product.category), price: resolveCell("price", String(product.price)), stock: resolveCell("stock", String(product.stock)), reorderAt: resolveCell("reorderAt", String(product.reorderAt)), active: resolveCell("active", String(product.active)), restockDate: resolveCell("restockDate", product.restockDate ?? ""), }}
/** A new product: a generated SKU, active, no stock, name and price to fill in. */export function createEmptyRow(sku: string): GridRow { return { id: sku, sku: valid(sku), name: resolveCell("name", ""), category: resolveCell("category", categories[0]), price: resolveCell("price", ""), stock: resolveCell("stock", "0"), reorderAt: resolveCell("reorderAt", "0"), active: resolveCell("active", "true"), restockDate: resolveCell("restockDate", ""), }}
export const rawOf = (row: GridRow, id: ColumnId) => { const cell = row[id] return typeof cell === "object" ? cell.raw : ""}
/** Rows are equal when every cell's raw text matches, so a revert is not a change. */export const rowsEqual = (a: GridRow, b: GridRow) => COLUMN_IDS.every(id => rawOf(a, id) === rawOf(b, id))
export const isLowStock = (row: GridRow) => { const stock = row.stock const reorderAt = row.reorderAt return ( typeof stock === "object" && typeof reorderAt === "object" && stock.status === "valid" && reorderAt.status === "valid" && Number(stock.raw) <= Number(reorderAt.raw) )}
export const hasInvalidCell = (row: GridRow) => COLUMN_IDS.some(id => { const cell = row[id] return typeof cell === "object" && cell.status === "invalid" })
/** Live header numbers, computed from the rows as they are edited. */export function summarize(rows: GridRow[]) { let lowStock = 0 let cents = 0 for (const row of rows) { if (isLowStock(row)) lowStock++ const price = row.price const stock = row.stock if ( typeof price === "object" && typeof stock === "object" && price.status === "valid" && stock.status === "valid" ) { cents += Math.round(Number(price.raw) * 100) * Number(stock.raw) } } return { products: rows.length, lowStock, cents }}export type Category = "Kitchen" | "Office" | "Outdoor" | "Apparel"
export type Product = { sku: string name: string category: Category /** Unit price in USD. */ price: number stock: number /** Stock level at or below which the product counts as low. */ reorderAt: number active: boolean /** ISO date (YYYY-MM-DD) of the next delivery, or null. */ restockDate: string | null}
export const categories: Category[] = [ "Kitchen", "Office", "Outdoor", "Apparel",]
export const categoryOptions = categories.map(category => ({ label: category, value: category,}))
/** Fixed "today" for the sample data, so restock dates never drift. */const TODAY = Date.UTC(2026, 10, 2)
const names: Record<Category, string[]> = { Kitchen: [ "Cast Iron Skillet", "Bamboo Cutting Board", "Ceramic Mixing Bowl", "Glass Storage Jar", "Enamel Stockpot", "Wooden Spice Rack", "Linen Tea Towel", "Steel Mixing Whisk", "Pour-Over Kettle", "Stoneware Mug", "Slate Cheese Board", "Copper Measuring Cups", "Silicone Spatula Set", "Marble Mortar", "Folding Dish Rack", ], Office: [ "Walnut Desk Tray", "Dot Grid Notebook", "Felt Desk Mat", "Brass Pen Holder", "Standing Desk Mat", "Cable Organizer", "Wire Letter Sorter", "Gel Pen Pack", "Clip Desk Lamp", "Recycled Paper Ream", "Monitor Riser", "Leather Folio", "Magnetic Whiteboard", "Paper Clip Dish", "Wall Planner", ], Outdoor: [ "Canvas Daypack", "Folding Camp Stool", "Insulated Bottle", "Compact Headlamp", "Trail Rain Poncho", "Wool Camp Blanket", "Titanium Spork", "Hammock Strap Set", "Mesh Gear Bag", "Pocket Compass", "Dry Sack", "Tent Stake Kit", "Lantern Hook", "Trekking Pole Pair", "Seat Cushion", ], Apparel: [ "Cotton Crew Tee", "Fleece Zip Jacket", "Merino Beanie", "Canvas Work Apron", "Ripstop Shorts", "Wool Hiking Socks", "Waxed Cap", "Linen Button Shirt", "Quilted Vest", "Knit Scarf", "Twill Chore Coat", "Organic Sweatshirt", "Rain Shell", "Denim Overshirt", "Thermal Gloves", ],}
const prefix: Record<Category, string> = { Kitchen: "KIT", Office: "OFC", Outdoor: "OUT", Apparel: "APP",}
const priceRange: Record<Category, [number, number]> = { Kitchen: [8, 90], Office: [4, 120], Outdoor: [12, 140], Apparel: [15, 160],}
/** * Sample data, generated rather than hand-written. A seeded PRNG (mulberry32) * keeps it random-looking but identical on every render. Swap `products` for * your own rows. */function seededRandom(seed: number) { let a = seed return () => { a = (a + 0x6d2b79f5) | 0 let t = Math.imul(a ^ (a >>> 15), 1 | a) t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t return ((t ^ (t >>> 14)) >>> 0) / 4294967296 }}
function buildProducts(): Product[] { const random = seededRandom(20261102) const between = (min: number, max: number) => min + Math.floor(random() * (max - min + 1)) const isoDay = (offset: number) => new Date(TODAY + offset * 86_400_000).toISOString().slice(0, 10)
const rows: Product[] = [] categories.forEach(category => { names[category].forEach((name, i) => { const n = rows.length const [min, max] = priceRange[category] const reorderAt = between(5, 25) // Most products are healthy; every 7th runs low (one of them sold out). const low = n % 7 === 3 const stock = low ? n === 3 ? 0 : between(0, reorderAt) : reorderAt + between(8, 140) rows.push({ sku: `${prefix[category]}-${String(1001 + i).padStart(4, "0")}`, name, category, price: between(min * 100, max * 100) / 100, stock, reorderAt, // Every 9th product is discontinued. active: n % 9 !== 4, // Low-stock products have a delivery booked; a few others do too. restockDate: low || n % 11 === 0 ? isoDay(between(3, 40)) : null, }) }) }) return rows}
export const products: Product[] = buildProducts()Usage
The files land in components/inventory-grid/. Render the block anywhere:
import InventoryGrid from "@/components/inventory-grid/inventory-grid"
export default function Page() { return <InventoryGrid />}Then replace the generated rows in inventory-data.ts with your own data, or load it from your API and pass it in.