Data grid
Rows of a database table: typed headers, constraints, sort, NULL and masked cells.
import { DataGrid, ConstraintChip } from "@forgeron/ui";| idintegerPrimary | emailtextUnique | phonetext |
|---|---|---|
| 1 | camille@roy.fr | +33 6 12 34 56 78 |
| 2 | hugo@roy.fr | NULL |
| 3 | lea@roy.fr | NULL |
import { DataGrid } from "@forgeron/ui";
export default function Default() {
return (
<div style={{ width: 380 }}>
<DataGrid
label="contacts"
columns={[
{ name: "id", type: "integer", chips: [{ kind: "primary" }], numeric: true },
{ name: "email", type: "text", chips: [{ kind: "unique" }] },
{ name: "phone", type: "text" },
]}
rows={[
[1, "camille@roy.fr", "+33 6 12 34 56 78"],
[2, "hugo@roy.fr", null],
[3, "lea@roy.fr", null],
]}
/>
</div>
);
}| cardtextMasked | ||||
|---|---|---|---|---|
| 1045 | 7 | overdue | 129 | masked |
| 1044 | 31 | paid | 455.2 | masked |
| 1043 | 12 | sent | 3912 | masked |
| 1042 | 7 | draft | 86.5 | masked |
| 1041 | 12 | paid | 1240 | masked |
import { DataGrid, type GridCell, type GridSort } from "@forgeron/ui";
import { useState } from "react";
const columns = [
{ name: "id", type: "integer", chips: [{ kind: "primary" as const }], numeric: true },
{
name: "customer_id",
type: "integer",
chips: [{ kind: "foreignKey" as const, title: "→ customers.id" }],
numeric: true,
},
{ name: "status", type: "text", chips: [{ kind: "notNull" as const }] },
{ name: "total", type: "numeric(10,2)", numeric: true },
{ name: "card", type: "text", masked: "Payment data: masked for your role" },
];
const data: GridCell[][] = [
[1041, 12, "paid", 1240, { masked: "Payment data" }],
[1042, 7, "draft", 86.5, { masked: "Payment data" }],
[1043, 12, "sent", 3912, { masked: "Payment data" }],
[1044, 31, "paid", 455.2, { masked: "Payment data" }],
[1045, 7, "overdue", 129, { masked: "Payment data" }],
];
// The sort and the open row belong to the page: the grid shows them and asks for changes. The
// open row is kept by its id, so it stays open when the sort moves it.
export default function SortAndOpen() {
const [sort, setSort] = useState<GridSort>({ column: "id", descending: true });
const [open, setOpen] = useState<GridCell>(null);
const at = columns.findIndex((c) => c.name === sort.column);
const rows = [...data].sort((a, b) => {
const x = a[at] as string | number;
const y = b[at] as string | number;
return (x < y ? -1 : x > y ? 1 : 0) * (sort.descending ? -1 : 1);
});
return (
<div style={{ width: "100%" }}>
<DataGrid
label="invoices"
columns={columns}
rows={rows}
sort={sort}
onSort={(column) =>
setSort((s) => ({ column, descending: s.column === column ? !s.descending : false }))
}
selectedRow={open === null ? undefined : rows.findIndex((row) => row[0] === open)}
onOpenRow={(index) => setOpen(rows[index]![0]!)}
/>
</div>
);
}| idintegerPrimary | invoice_idintegerForeign key | reasontext |
|---|---|---|
| No refund matches these filters | ||
import { DataGrid } from "@forgeron/ui";
export default function Empty() {
return (
<div style={{ width: "100%", maxWidth: 520 }}>
<DataGrid
label="refunds"
columns={[
{ name: "id", type: "integer", chips: [{ kind: "primary" }], numeric: true },
{ name: "invoice_id", type: "integer", chips: [{ kind: "foreignKey" }] },
{ name: "reason", type: "text" },
]}
rows={[]}
empty="No refund matches these filters"
/>
</div>
);
}API → Props
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | Names the grid; it scrolls on the keyboard. |
columns | { name, type, chips?, masked?, numeric? }[] | — | chips: Primary, Not null, Unique, Foreign key. masked says why. |
rows | (string | number | null | { masked })[][] | — | null reads NULL; a masked cell says it is masked. |
sort / onSort | { column, descending } / (column) => void | — | The sort is the page's: the grid only shows it and asks for it. |
onOpenRow | (index) => void | — | Click or Enter on a row. |
onCopyCell | (text, column) => void | — | A double click on a cell gives its text. |
selectedRow | number | — | The open row, marked in ember. |
empty | ReactNode | "No rows" | No row. |
className | string | — | Extra classes. |