Custom Editors
Implement cellEditor on ColumnDef<T> to mount custom inputs, dropdowns, datepickers, or multiline <textarea> editors inside the cell.
import { Grid, type ColumnDef } from 'next-grid-js';import { createOrders, type OrderRow } from './data/orders';import { syncGridTheme } from './theme';import type { DemoMountContext } from './types';
export function mount({ stage, meta }: DemoMountContext): () => void { const statuses: OrderRow['status'][] = [ 'Paid', 'Pending', 'Shipped', 'Refunded', ];
const columnDefs: readonly ColumnDef<OrderRow>[] = [ { key: 'id', name: 'ID', width: 96, }, { key: 'customer', name: 'Customer (Textarea)', width: 200, flex: 1, isEditable: true, cellEditor: ({ value, document: doc }) => { const ta = doc.createElement('textarea'); ta.rows = 2; ta.value = String(value ?? ''); return { element: ta, getValue: () => ta.value, focus: () => ta.focus(), destroy: () => ta.remove(), }; }, }, { key: 'status', name: 'Status (Select)', width: 135, isEditable: true, cellEditor: ({ value, document: doc }) => { const select = doc.createElement('select'); for (const opt of statuses) { select.add(new Option(opt, opt, false, opt === value)); } return { element: select, getValue: () => select.value, focus: () => { select.focus(); try { select.showPicker?.(); } catch { // Ignore when browser blocks programmatic picker outside user gesture } }, destroy: () => select.remove(), }; }, }, ];
const grid = new Grid<OrderRow>({ container: stage, columnDefs, rowData: createOrders(20), getRowId: (row) => row.id, rowHeight: 34, }); const stopTheme = syncGridTheme(stage); meta.textContent = 'Ctrl/Cmd+Enter commits textarea; Enter commits select';
return () => { grid.destroy(); stopTheme(); };}CellEditorInstance Contract
Section titled “CellEditorInstance Contract”{ key: 'status', name: 'Status', isEditable: true, cellEditor: ({ value, document }) => { const select = document.createElement('select'); for (const opt of ['Paid', 'Pending', 'Shipped', 'Refunded']) { select.add(new Option(opt, opt, false, opt === value)); } return { element: select, getValue: () => select.value, focus: () => select.focus(), validate: (v) => (v ? null : 'Status is required'), destroy: () => select.remove(), }; },}Focus, Multiline & Lifecycle Rules
Section titled “Focus, Multiline & Lifecycle Rules”- Focus Order: When
focus()is omitted or delegates insideelement, focus moves to the first focusable control (skipping hidden inputs, disabled controls, and unrendered nodes; elements withtabindex="-1"come last). - Multiline
<textarea>: Focusing a<textarea>switches commit keys soEnterinserts a newline andCtrl/Cmd + Entercommits. - Synchronous Ownership:
cellEditorand all lifecycle hooks (mount,start,focus,getValue,validate,commit,cancel,destroy) must be synchronous (returning aPromisethrows) andelementmust be a detached same-documentHTMLElement. - Simplified
editor: For quick native controls without a full lifecycle object,editor: (value, row) => HTMLElementaccepts a detached<input>,<select>, or<textarea>. - Framework Adapters: See React, Vue, Svelte, and Angular for component-based editors.