File Field

A file input that fits a form: one file held in the field, or several listed under it.

Choose fileNo receipt yet

A photo or a PDF, up to 10 MB.

Add the receipt to submit.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/file-field

Usage

import type { UploadItem } from "./ui/uicontext/components/domain/Upload.ts"import { FormField } from "./ui/uicontext/components/FormControls.ts"import { FileField } from "./ui/uicontext/components/upload/FileField.ts"
const Message = UiSchema.TaggedEnum({	PickedResume: { file: Schema.instanceOf(File) },	RequestedRemoveResume: { id: Schema.String },})// One file, in a form: the label opens the picker, and the field shows the file once chosenexport const ResumeField = (h: Html.H<typeof Message.Type>, value: ReadonlyArray<UploadItem>) =>	FormField(h, { id: "resume", label: "Résumé", description: "PDF, up to 5 MB." }, (field) =>		FileField(h, {			props: { field, value, accept: { types: [".pdf"], typeLabel: "PDF", typeNoun: "a PDF", maxSize: 5 * 1024 * 1024 } },			onEmit: {				Picked: ({ files }) => (files[0] === undefined ? undefined : Message.PickedResume({ file: files[0] })),				Refused: () => undefined,				RequestedRemove: ({ id }) => Message.RequestedRemoveResume({ id }),				RequestedRetry: () => undefined,			},		}))

Examples

Several files, kept for the form

Up to five signed contracts, as PDFs. They're listed under the field and go with the form when it's sent; a sixth file or a Word document is turned away.

Choose filesAdd more
  • Northwind — Master services agreement (signed).pdf1.2 MB

PDFs, up to 5 files and 20 MB each.

API reference

Props

FileField props
NameTypeDefaultDescription
labelstring—The control's name, when no Form Field's label names it
field{ id, aria-describedby, aria-invalid, … }—The attributes a Form Field hands its control: id, description and error wiring
valuerequiredUploadItem[]—The chosen files, with their upload's progress and Outcome when they upload at once
acceptUploadAccept——
multiplebooleanoneTake several files; one by default
placeholderstring—The text in the empty field; the accepted types and size by default
disabledboolean——
localestring——

Events

FileField events
EventPayloadDescription
Picked{ files: instanceOf[] }Files that passed the rules; with one file, the new one replaces the old
Refused{ refused: UploadRefusal[] }Files the rules turned away; the field says why under itself
RequestedRemove{ id: string }—
RequestedRetry{ id: string }—

Source