Form Layout

Labels with hints and required or optional marks, sections that sit beside or above their fields, two-column rows, and a save bar that guards unsaved changes.

General

Changes apply to everyone in Northwind Freight once you save.

Workspace

How the workspace shows to members and in links.

Changing it breaks old links.

54/280

Defaults

What new members start with. They can change their own time zone.

Notifications

Sent to every member unless they turn them off.

A summary of activity every Monday morning.

An email when someone mentions you and you're away.

Contacts

Invoices and receipts go here.

Shown to guests who need help.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/form-layout

Usage

import { FieldGrid, FormBody, FormSection, LeaveGuard, SaveBar } from "./ui/uicontext/components/forms/FormLayout.ts"
export const ProfileForm = <M>(	h: Html.H<M>,	options: { readonly name: View.Content<M>; readonly email: View.Content<M>; readonly dirty: boolean; readonly changes: number; readonly saving: boolean; readonly onSubmit: M; readonly onDiscard: M; readonly onLeave: (href: string) => M },) =>	h.form.attrs({ novalidate: "" }).on("submit", options.onSubmit).children(		LeaveGuard(h, { when: options.dirty, onLeave: options.onLeave }),		FormBody(h, [			FormSection(h, { title: "Profile", description: "Shown to everyone in the workspace.", children: [FieldGrid(h, [options.name, options.email])] }),		]),		SaveBar(h, { state: options.saving ? "saving" : options.dirty ? "dirty" : "clean", changes: options.changes, onDiscard: options.onDiscard }),	)

API reference

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