Shadcn CSV Import Review Table
Check a CSV before it becomes data: map its columns to your fields, see invalid cells and duplicate emails highlighted in place, fix them inline or skip the row, then import only what is ready.
$ npx shadcn add https://www.shad-table.dev/r/csv-import-table.jsoncustomers-export.csv14 rows
Line | Full name | E-mail Address | Organization | Country | Tier | Licenses | Notes | Status | |
|---|---|---|---|---|---|---|---|---|---|
| 2 | Renewal in March | Ready | |||||||
| 3 | Ready | ||||||||
| 4 | Lowercase plan is fine | Ready | |||||||
| 5 | Missing name | Needs fixing | |||||||
| 6 | Already a customer | Duplicate | |||||||
| 7 | Three-letter country | Needs fixing | |||||||
| 8 | Email has no domain | Needs fixing | |||||||
| 9 | Unknown plan | Needs fixing | |||||||
| 10 | Seats written out | Needs fixing | |||||||
| 11 | Same person twice | Duplicate | |||||||
| 12 | Ready | ||||||||
| 13 | Zero seats | Needs fixing | |||||||
| 14 | Ready | ||||||||
| 15 | Ready |
6 of 14 rows ready. 8 rows with problems won't be imported until fixed or skipped.
How it works
1.The table has the file’s columns, not your schema’s
Columns are built from the CSV header, and each header holds a select for the field it imports into. autoMap() matches headers like "E-mail Address" or "Licenses" by alias. Picking a field that another column already uses moves it, so a field is never imported twice. Columns set to "Don't import" stay visible, greyed out, for context.
export const assign = (mapping, column, field) =>mapping.map((current, i) =>i === column ? field : field && current === field ? null : current,)
2.One schema drives mapping, validation and the import
Each field declares its aliases, a validate() that returns a message, and a normalize() that produces the imported value. To import products instead of customers, replace the fields array; nothing else changes.
{id: 'plan',required: true,aliases: ['plan', 'tier', 'subscription'],validate: (v) => plans.some((p) => p.toLowerCase() === v.trim().toLowerCase())? null : 'Plan must be Free, Pro or Enterprise',normalize: (v) => plans.find((p) => p.toLowerCase() === v.trim().toLowerCase()),}
3.Edits change the raw file, and the review runs again
Fixing a cell writes into the CSV rows themselves, and review() is a useMemo over the file, the mapping and the skipped rows. A fix therefore survives remapping, and the row id is its position in the file, so editing never moves it.
const rows = useMemo(() => review(file.csv, file.mapping, skipped),[file, skipped],)
4.Duplicates are checked in the file and against existing records
Emails are compared case-insensitively. The first copy in the file wins and later ones point back to its line. Emails that already belong to a customer are flagged as well. Skipped rows don’t count, so skipping the first copy clears the second.
if (existingEmails.has(email)) issue('Already a customer in this workspace')else if (first !== undefined) issue(`Same email as line ${first}`)else firstLineByEmail.set(email, line)
5.Only ready rows are imported
Rows with problems never block the rest: the import button counts ready rows, and "Needs fixing" filters the table down to what is left. Skip a row, or every row with problems at once, to leave it out on purpose.
export const toRecords = (rows, mapping) =>rows.filter((row) => row.status === 'ready').map(normalizeRow)