Search docs

Jump to any table example

Discord—

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.json

customers-export.csv14 rows

Line
Full name
E-mail Address
Organization
Country
Tier
Licenses
Notes
Status
2Renewal in MarchReady
3Ready
4Lowercase plan is fineReady
5Missing nameNeeds fixing
6Already a customerDuplicate
7Three-letter countryNeeds fixing
8Email has no domainNeeds fixing
9Unknown planNeeds fixing
10Seats written outNeeds fixing
11Same person twiceDuplicate
12Ready
13Zero seatsNeeds fixing
14Ready
15Ready

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.

src/components/csv-import/review.ts
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.

src/components/csv-import/schema.ts
{
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.

src/components/csv-import/index.tsx
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.

src/components/csv-import/review.ts
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.

src/components/csv-import/review.ts
export const toRecords = (rows, mapping) =>
rows.filter((row) => row.status === 'ready').map(normalizeRow)