Data grid example

A hierarchical custom cell editor.

Compose TreeSelect through the public render hooks. A custom popup keeps canonical leaf IDs and the same validation, focus and dismissal boundary.

Complete React client module · MIT · Next.js and Vite

Interactive example

A hierarchical custom cell editor

Compose TreeSelect through the public render hooks. A custom popup keeps canonical leaf IDs and the same validation, focus and dismissal boundary.

Keyboard

  • Enter opens the custom team editor. Search or expand the tree and choose loaded leaf teams.
  • TreeSelect handles its own keyboard navigation; Tab reaches Apply teams and Cancel.
  • Apply teams validates canonical leaf IDs, finishes the exit motion and returns focus to the cell.
  • Escape or outside dismissal discards only the temporary editor value; outside focus is preserved.

States covered

  • custom cell display
  • custom editor hooks
  • TreeSelect composition
  • canonical leaf IDs
  • typed context
  • exit motion
  • validation
  • outside focus

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-grid-editor-custom.json
Complete source
React / TypeScript
/*!
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 { useRef, useState } from 'react';
import { Popover } from '@base-ui/react/popover';
import { Button, DataGrid, TreeSelect, UIProvider, parseDataValue, type DataColumn, type DataEdit, type DataGridEditorContext, type TreeSelectNode } from '@bld8/ui';

type Ownership = { id: string; revision: number; title: string; teams: string[] };
const teams: TreeSelectNode[] = [
  { id: 'design', label: 'Design', children: [{ id: 'design-ui', label: 'Interface systems' }, { id: 'design-motion', label: 'Motion' }] },
  { id: 'platform', label: 'Platform', children: [{ id: 'platform-web', label: 'Web platform' }, { id: 'platform-a11y', label: 'Accessibility' }] }
];
const teamOptions = [{ value: 'design-ui', label: 'Interface systems' }, { value: 'design-motion', label: 'Motion' }, { value: 'platform-web', label: 'Web platform' }, { value: 'platform-a11y', label: 'Accessibility' }];
const columns: DataColumn<Ownership>[] = [
  { key: 'id', label: 'Record', type: 'text', width: 104, minWidth: 104 },
  { key: 'title', label: 'Ownership title', type: 'text', editable: true, required: true, minWidth: 220 },
  { key: 'teams', label: 'Teams', type: 'multi-select', editable: true, options: teamOptions, required: true, minWidth: 300 }
];
function TeamCellEditor({ context }: { context: DataGridEditorContext<Ownership> }) {
  const [open, setOpen] = useState(true), [error, setError] = useState('');
  const current = useRef(context); current.current = context;
  const pending = useRef<{ apply: boolean; value?: unknown; restore: boolean } | null>(null);
  function close(apply: boolean, restore = true) {
    if (pending.current) return;
    if (apply) { const parsed = parseDataValue(context.column, context.draftValue); if (parsed.error) { setError(parsed.error); return; } pending.current = { apply, value: parsed.value, restore }; }
    else pending.current = { apply, restore };
    setOpen(false);
  }
  return <Popover.Root open={open} onOpenChange={(next, details) => { if (!next) close(false, details.reason !== 'outside-press' && details.reason !== 'focus-out'); }} onOpenChangeComplete={next => {
    if (next || !pending.current) return;
    const action = pending.current; pending.current = null;
    if (action.apply) { if (!current.current.commit(action.value)) setOpen(true); } else current.current.cancel(action.restore);
  }}>
    <Popover.Trigger render={<Button variant="ghost" size="sm" aria-label={context.label}>Choose teams</Button>} />
    <Popover.Portal><Popover.Positioner className="bui-positioner" sideOffset={6} collisionPadding={12} align="start"><Popover.Popup {...context.portalProps} className="bui-popup" finalFocus={false} style={{ width: 'min(360px, calc(100vw - 24px))', maxHeight: 'calc(100dvh - 24px)', overflow: 'auto', display: 'grid', gap: 16, padding: 20 }}>
      <Popover.Title style={{ margin: 0, fontSize: 15 }}>Choose delivery teams</Popover.Title>
      <Popover.Description className="bui-hint" style={{ margin: 0 }}>Changes stay temporary until you apply this cell.</Popover.Description>
      <TreeSelect label={context.label} nodes={teams} value={Array.isArray(context.draftValue) ? context.draftValue.filter((value): value is string => typeof value === 'string') : []} onValueChange={value => { setError(''); context.onValueChange(value); }} inputRef={context.inputRef} popupDataAttributes={context.portalProps} density="compact" clearable disabled={context.disabled} error={error || context.error} />
      <div style={{ display: 'flex', justifyContent: 'flex-end', gap: 8 }}><Button variant="ghost" size="sm" onClick={() => close(false)}>Cancel</Button><Button size="sm" onClick={() => close(true)}>Apply teams</Button></div>
    </Popover.Popup></Popover.Positioner></Popover.Portal>
  </Popover.Root>;
}
export default function CustomTeamEditorExample() {
  const [rows, setRows] = useState<Ownership[]>([{ id: 'OWN-1', revision: 1, title: 'Delivery checklist', teams: ['design-ui'] }, { id: 'OWN-2', revision: 1, title: 'Release handoff', teams: ['platform-web'] }]);
  const [message, setMessage] = useState('Edits stay in this local example.');
  function edit(edits: DataEdit<Ownership>[]) {
    const parsed = edits.map(edit => ({ ...edit, parsed: parseDataValue(columns.find(column => column.key === edit.key)!, edit.value) }));
    if (parsed.some(edit => edit.parsed.error)) { setMessage('No edits were applied: a value did not match its column.'); return; }
    setRows(current => current.map(row => parsed.filter(edit => edit.id === row.id).reduce((next, edit) => ({ ...next, [edit.key]: edit.parsed.value }), row))); setMessage('Canonical IDs applied to the local draft.');
  }
  return <UIProvider><div style={{ display: 'grid', gap: 16, minWidth: 0 }}>
    <DataGrid label="Custom team editor grid" rows={rows} columns={columns} onEdit={edit} renderCell={context => context.column.key === 'teams' ? <span className="bui-data-grid-tags">{context.row.teams.map(id => <span className="bui-data-grid-tag" key={id}>{teamOptions.find(option => option.value === id)?.label ?? id}</span>)}</span> : undefined} renderEditor={context => context.column.key === 'teams' ? <TeamCellEditor context={context} /> : undefined} />
    <Button variant="secondary" size="sm" onClick={() => setMessage('Outside action kept focus.')}>Outside editor action</Button>
    <p role="status" className="bui-hint">{message}</p><output aria-label="Custom team canonical values" style={{ fontSize: 12, overflowWrap: 'anywhere' }}>{JSON.stringify(rows.map(row => ({ id: row.id, teams: row.teams })))}</output>
    <p className="bui-hint">Custom editors render UI; the grid validates column values. The host still owns saved records and service writes.</p>
  </div></UIProvider>;
}