gridcn2.0.0
Basic examples

Custom cell type example

Register a currency cell type with its own display, editor, and text parsing, and use it in two locales.

A custom currency type stores a plain number | null, shows it through Intl.NumberFormat, and edits it as text. Type $1,234.56 into a price cell and press Enter. The type strips the symbol and the cell stores 1234.56.

Edit a USD or EUR price cell and type a value with a currency symbol (e.g. "$1,234.56") and press Enter: the custom cell type strips the symbol back to a plain number before it commits.

Product
USD
EUR
Mechanical Keyboard
$129.99
119,50 €
4K Monitor
$449.00
415,00 €
USB-C Dock
$79.50
74,00 €
Webcam
$59.00
pnpm dlx shadcn@latest add @gridcn/data-grid-custom-cell-demo

What the demo shows

  • The CellType object sets Cell, Editor, toText, toDisplayText, fromText, clearValue, isEmpty, compare, and align. toText returns the unformatted number for copy and export. toDisplayText returns the formatted text.
  • fromText strips every character except digits, ., ,, and -. It calls rejectText() for text with no amount: the cell keeps its value, and the grid reports the text, as the built-in types do. Return clearValue() only to empty the cell on purpose. The table below lists the separator rules.
  • useCellEditor supplies the editor with its draft text and the caret at the end of the text. It guards the commit so that it runs once, and it binds Enter, Escape, and blur. The demo passes it a toDraft and a toValue function.
  • Cell and Editor receive the column options with the type CurrencyOptions, so reading options.currency needs no cast.
  • The demo keeps one Intl.NumberFormat for each locale and currency pair, so a cell reuses a formatter.
  • cellTypes={{ currency: currencyCellType }} on DataGridProvider merges over the built-in types. The text column of the demo keeps working.
  • One type serves two columns through options: { currency: "USD" } and { currency: "EUR", locale: "de-DE" }. A column with no locale uses en-US.

Separator rules

fromText reads the separators . and , with these rules:

InputStoredRule
1.234,56 EUR1234.56With both separators, the last one is the decimal separator.
$1,2341234One separator followed by exactly three final digits groups thousands.
1,51.5Any other single separator is the decimal separator.

A single separator that matches the decimal separator of the column locale is always the decimal separator, so 1,234 in de-DE stores 1.234. A repeated separator groups thousands, so 1,234,567 stores 1234567.

Check the type in defineColumns

The demo types its columns as ColumnDef because currency is not a key of GridCellTypes. To make defineColumns check type: "currency" and its options, add the key to GridCellTypes:

export {};

declare module "@/components/data-grid/types" {
  interface GridCellTypes {
    currency: {
      value: number | null;
      options: { currency: string; locale?: string };
    };
  }
}

Without the augmentation, pass the types as the second type argument of defineColumns. defineCellType(type) keeps the value and options types of the type it receives:

const extraTypes = { currency: defineCellType(currencyCellType) };
const columns = defineColumns<Order, typeof extraTypes>()([
  {
    id: "price",
    header: "Price",
    accessorKey: "price",
    type: "currency",
    options: { currency: "EUR" },
  },
]);

On this page