Form

A form from one Effect Schema: errors in plain words on blur and submit, an error summary, async checks, server errors on their fields, and focus on the first field to fix.

Create a workspace

Your team's projects, docs and chat live here.

0/48

Lowercase letters, numbers and dashes. You can change it later.

Data region
Virginia. Fastest for the Americas.
Frankfurt. Data stays in the EU.
Singapore.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/form

Usage

import { FormField, Input } from "./ui/uicontext/components/FormControls.ts"import { fieldId, make } from "./ui/uicontext/components/forms/Form.ts"
export const Profile = make(	Schema.Struct({		name: Schema.Trim.check(Schema.isNonEmpty({ message: "Enter your name" })).annotate({ title: "Name" }),		email: Schema.String.check(Schema.isPattern(/^[^\s@]+@[^\s@]+$/u, { message: "Enter an email address, like [email protected]" })),		team: Schema.optionalKey(Schema.String),	}),)// in the Model: `form: Profile.State`; in update: Profile.change, Profile.blur, Profile.submitexport const NameField = <M>(h: Html.H<M>, form: typeof Profile.State.Type, onInput: (value: string) => M, onBlur: M) => {	const name = Profile.validate(form).field("name")	return FormField(h, { id: fieldId("profile", "name"), label: "Name", required: name.required, ...(name.error === undefined ? {} : { error: name.error }) }, (attrs) =>		Input(h, { invalid: name.error !== undefined }).attrs(attrs).value(form.values.name)			.on("input", ({ value }) => onInput(value))			.on("blur", onBlur))}

Examples

Billing address by country

Switch the country: the labels, the required fields, their order and the postal format follow it. Try ZIP 00000 to see the server refuse it.

Billing address

API reference

FormView function

Options

Form options
NameTypeDefaultDescription
mode"onBlur" | "onChange" | "onSubmit"onBlurWhen a field starts showing its errors; onBlur by default

FormSectionView function

Options

FormSection options
NameTypeDescription
titlerequiredstring—
descriptionstringA sentence on what the fields are for
childrenrequiredView.Children—
styleStyle—

ErrorSummaryView function

Options

ErrorSummary options
NameTypeDescription
formrequiredstringThe form's id, which the fields' ids start with
errorsrequiredFormError[]—
submitsrequirednumberSubmits tried: the summary shows after the first, and is read out again on each
formErrorstringThe server's error about the whole form
title(count: number) => string"Fix 2 fields to continue" by default

SaveBarView function

Options

SaveBar options
NameTypeDescription
staterequired"dirty" | "saving" | "saved" | "failed" | "clean"Where the changes stand; nothing shows while there are none
changesnumberHow many fields changed: "3 unsaved changes"
errorstringWhy the save failed, and what to do
onDiscardrequiredMessage—
saveLabelstring"Save changes" by default

LeaveGuardView function

Options

LeaveGuard options
NameTypeDefaultDescription
whenrequiredboolean——
onLeaverequired(href: string) => Message——
scope"document" | "parent"documentGuard links in the whole document, or only in the guard's parent element; document by default

LeaveDialogView function

Options

LeaveDialog options
NameTypeDescription
openrequiredboolean—
subjectstringWhat's being edited: "workspace settings"
onStayrequiredMessage—
onLeaverequiredMessage—

Source