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.
pnpm dlx shadcn@latest add @gridcn/data-grid-custom-cell-demo
What the demo shows
- The
CellTypeobject setsCell,Editor,toText,toDisplayText,fromText,clearValue,isEmpty,compare, andalign.toTextreturns the unformatted number for copy and export.toDisplayTextreturns the formatted text. fromTextstrips every character except digits,.,,, and-. It callsrejectText()for text with no amount: the cell keeps its value, and the grid reports the text, as the built-in types do. ReturnclearValue()only to empty the cell on purpose. The table below lists the separator rules.useCellEditorsupplies 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 atoDraftand atoValuefunction.CellandEditorreceive the columnoptionswith the typeCurrencyOptions, so readingoptions.currencyneeds no cast.- The demo keeps one
Intl.NumberFormatfor each locale and currency pair, so a cell reuses a formatter. cellTypes={{ currency: currencyCellType }}onDataGridProvidermerges over the built-in types. Thetextcolumn of the demo keeps working.- One type serves two columns through
options:{ currency: "USD" }and{ currency: "EUR", locale: "de-DE" }. A column with nolocaleusesen-US.
Separator rules
fromText reads the separators . and , with these rules:
| Input | Stored | Rule |
|---|---|---|
1.234,56 EUR | 1234.56 | With both separators, the last one is the decimal separator. |
$1,234 | 1234 | One separator followed by exactly three final digits groups thousands. |
1,5 | 1.5 | Any 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" },
},
]);Related
- Write a cell type: the steps to write a type and its
GridCellTypeskey - CellType contract: every member of
CellType - Cell types example: the built-in types