Import wizard
A CSV into a table: the file, the columns, every row checked, the import.
import { ImportWizard } from "@forgeron/ui";Import into cheeses
- 1File
- 2Columns
- 3Check
- 4Import
// A CSV into a table: drop a file or paste rows, match the columns, check every row, import.
import { ImportWizard, type DataField } from "@forgeron/ui";
import { useState } from "react";
const fields: DataField[] = [
{ id: "id", name: "id", type: "number", primary: true, decimals: 0 },
{ id: "name", name: "name", type: "text", required: true },
{ id: "price", name: "price", type: "number", decimals: 2 },
{ id: "organic", name: "organic", type: "boolean" },
{
id: "family",
name: "family",
type: "select",
choices: [
{ value: "soft", label: "Soft" },
{ value: "hard", label: "Hard" },
{ value: "blue", label: "Blue" },
],
},
];
const sample = `Name;Price;Organic;Family
Brie de Meaux;12,50;yes;Soft
Comté 18 mois;24,90;no;Hard
Roquefort;19,00;yes;Blue
Tomme de Savoie;abc;no;Hard
;8,40;no;Soft`;
export default function Default() {
const [log, setLog] = useState("");
return (
<div style={{ width: "min(560px, 100%)", display: "grid", gap: 8 }}>
<ImportWizard
key={log}
fields={fields}
tableName="cheeses"
defaultText={sample}
onImport={async (records) => {
await new Promise((r) => setTimeout(r, 400));
return { inserted: records.length };
}}
onClose={() => setLog(String(Date.now()))}
/>
</div>
);
}API → Props
| Prop | Type | Default | Description |
|---|---|---|---|
fields | DataField[] | — | { id, name, type, primary?, required?, unique?, readOnly?, defaultValue?, description?, choices?, relation?: { table, multiple? }, decimals? }. |
onImport | (records) => ImportResult | void | Promise<…> | — | Writes the valid records, keyed by field id. Return { inserted, failed?, message? }; a throw is shown. |
relations | Record<table, { id, label, hint? }[]> | — | The records relation fields can point at, by table: their labels and the picker. |
tableName | string | — | The table written to, in the result. |
onClose | () => void | — | Cancel, and Done at the end. |
previewRows | number | 8 | Rows shown in the check step. |
defaultText | string | — | Pasted text to start from. |
className | string | — | Extra classes. |