CSV Import

View as Markdown

Bring rows in from a spreadsheet: match its columns to fields, preview, and see which rows are skipped and why.

Drop a CSV file here or choose one

CSV or TSV, up to 20 MB

No file to hand?

When to use

Bring rows in from a spreadsheet: match its columns to fields, preview, and see which rows are skipped and why. Use Dropzone instead when the file is kept as it is, not read into records.

Usage

import { CsvImport } from "./ui/uicontext/components/CsvImport.ts"
export const ImportProducts = <M>(h: Html.H<M>, onImport: (rows: ReadonlyArray<Record<string, string>>) => M) =>	CsvImport(h, {		props: {			noun: "product",			fields: [				{ key: "sku", label: "SKU", required: true },				{ key: "name", label: "Name", required: true },				{ key: "price", label: "Price", kind: "number" },			],		},		onEmit: { RequestedImport: ({ rows }) => onImport(rows) },	})

Examples

A required column to pick by hand

Import waits until every required field has a column. Values that don't fit their field are marked in the preview.

Drop a CSV file here or choose one

CSV or TSV, up to 20 MB

No file to hand?

API reference

Props

CsvImport props
NameTypeDefaultDescription
nounrequiredstring—What a row becomes, singular: "contact". The plural adds an s; give nouns otherwise.
nounsstring——
fieldsrequiredImportField[]—The app's fields, in the order they're matched and shown.
previewnumber5How many rows the preview shows; 5 by default.
maxSizenumber—The largest file taken, in bytes; 20 MB by default.
sample{ name, text }—A file to try it with, offered under the drop zone: a template with the app's columns.
idstring"csv"Starts the ids inside it, unique on the page; "csv" by default.
localestring—For the counts; the browser's by default.

Events

CsvImport events
EventPayloadDescription
RequestedImport{ rows: (Record<string, string>)[]; skipped: number }Import was pressed: the rows that passed, keyed by field, and how many were skipped.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/csv-import

Source