Skip to content

Custom Editors

Implement cellEditor on ColumnDef<T> to mount custom inputs, dropdowns, datepickers, or multiline <textarea> editors inside the cell.

Custom Select & Multiline Textarea Editors
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();
};
}
{
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 Order: When focus() is omitted or delegates inside element, focus moves to the first focusable control (skipping hidden inputs, disabled controls, and unrendered nodes; elements with tabindex="-1" come last).
  • Multiline <textarea>: Focusing a <textarea> switches commit keys so Enter inserts a newline and Ctrl/Cmd + Enter commits.
  • Synchronous Ownership: cellEditor and all lifecycle hooks (mount, start, focus, getValue, validate, commit, cancel, destroy) must be synchronous (returning a Promise throws) and element must be a detached same-document HTMLElement.
  • Simplified editor: For quick native controls without a full lifecycle object, editor: (value, row) => HTMLElement accepts a detached <input>, <select>, or <textarea>.
  • Framework Adapters: See React, Vue, Svelte, and Angular for component-based editors.