A public form made from a collection's fields, with validation and a thank-you state.
Report a bug
Tell us what broke. We triage new reports every morning.
Fields marked with an asterisk are required.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import { Schema } from "effect"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import type { FormConfig } from "./ui/uicontext/components/domain/Record.ts"import { FormView } from "./ui/uicontext/components/kit/FormView.ts"import { Item, Value } from "./ui/uicontext/components/kit/Property.ts"import { issueFields, issues } from "./issues.ts"// A "Report a bug" form over the issue fields. The view's form config picks the questions; Labels// and Status are hidden, so every response arrives as a Todo bug. Each response emits RequestedSubmit// with all its values; the app creates the issue and answers through `outcome`.const form: FormConfig = { title: "Report a bug", description: "Tell us what broke. We triage new reports every morning.", fields: [ { key: "title", label: "What went wrong?", required: true, help: "One line, like a headline: what you did and what happened." }, { key: "priority", label: "How bad is it?", required: true }, { key: "customer", label: "Which customer hit it?", help: "Leave it empty if you found it yourself." }, { key: "due", label: "Needed by" }, { key: "labels", hidden: true }, { key: "status", hidden: true }, ], defaults: { labels: ["bug"], status: "todo" }, submitLabel: "Send report", success: "Thanks, it's in the backlog. You'll get an email when someone picks it up.",}const Message = UiSchema.TaggedEnum({ RequestedSubmitReport: { request: Schema.String, values: Schema.Record(Schema.String, Value) },})export const BugReportForm = Html.component({ name: "BugReportForm", model: Schema.Struct({ records: Schema.Array(Item), outcome: Schema.NullOr(Outcome) }), message: Message,}).make({ init: () => ({ records: issues, outcome: null }), update: { RequestedSubmitReport: ($, { request, values }) => { const { title, ...rest } = values const n = 150 + $.model.records.length return $.evolve({ records: (records) => [...records, { id: `eng-${n}`, prefix: `ENG-${n}`, title: String(title ?? ""), values: rest }], outcome: { _tag: "Accepted", request }, }) }, }, view: (h, { records, outcome }) => FormView(h, { props: { label: "Report a bug", records, fields: issueFields, view: { layout: "form", form }, outcome }, onEmit: { RequestedSubmit: (e) => Message.RequestedSubmitReport(e), RequestedEdit: () => undefined, RequestedOpen: () => undefined, RequestedCreate: () => undefined, RequestedAction: () => undefined, RequestedReorder: () => undefined, RequestedView: () => undefined, Selected: () => undefined, }, }),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/form-viewUsage
import type { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { type FormConfig, type Item, type Property, Value } from "./ui/uicontext/components/domain/Record.ts"import { FormView } from "./ui/uicontext/components/kit/FormView.ts"const Message = UiSchema.TaggedEnum({ RequestedSubmit: { request: Schema.String, values: Schema.Record(Schema.String, Value) },})const form: FormConfig = { title: "Request a feature", fields: [{ key: "title", label: "What would help?", required: true }, { key: "priority" }], submitLabel: "Send request",}// Create the record, then answer with { _tag: "Accepted" | "Refused", request } as `outcome`.export const FeatureRequest = ( h: Html.H<typeof Message.Type>, records: ReadonlyArray<Item>, fields: ReadonlyArray<Property>, outcome: Outcome | null,) => FormView(h, { props: { label: "Request a feature", records, fields, view: { layout: "form", form }, outcome }, onEmit: { RequestedSubmit: (response) => Message.RequestedSubmit(response), RequestedEdit: () => undefined, RequestedOpen: () => undefined, RequestedCreate: () => undefined, RequestedAction: () => undefined, RequestedReorder: () => undefined, RequestedView: () => undefined, Selected: () => undefined, }, })Examples
A refused response
Send it with a date in the past: the app refuses, and the form keeps the answers and says why.
Book a fix
Ask the support team to fix something by a date.
Fields marked with an asterisk are required.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import { Schema } from "effect"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import type { FormConfig } from "./ui/uicontext/components/domain/Record.ts"import { FormView } from "./ui/uicontext/components/kit/FormView.ts"import { Value } from "./ui/uicontext/components/kit/Property.ts"import { issueFields, issues } from "./issues.ts"// The app can turn a response down. The form keeps every answer, shows the reason, and lets the// person fix it and send again. This one refuses due dates in the past.const form: FormConfig = { title: "Book a fix", description: "Ask the support team to fix something by a date.", fields: [ { key: "title", label: "What needs fixing?", required: true }, { key: "customer", label: "For which customer?", required: true }, { key: "due", label: "Needed by", required: true, help: "Pick a date after today." }, ], submitLabel: "Book it", success: "Booked. The support team will confirm the date within a day.",}const Message = UiSchema.TaggedEnum({ RequestedSubmit: { request: Schema.String, values: Schema.Record(Schema.String, Value) },})const today = () => new Date().toISOString().slice(0, 10)export const BookAFix = Html.component({ name: "BookAFix", model: Schema.Struct({ outcome: Schema.NullOr(Outcome) }), message: Message,}).make({ init: () => ({ outcome: null }), update: { RequestedSubmit: ($, { request, values }) => { const due = values["due"] return typeof due === "string" && due <= today() ? $.evolve({ outcome: { _tag: "Refused", request, reason: "That date has passed. Pick one after today." } }) : $.evolve({ outcome: { _tag: "Accepted", request } }) }, }, view: (h, { outcome }) => FormView(h, { props: { label: "Book a fix", records: issues, fields: issueFields, view: { layout: "form", form }, outcome }, onEmit: { RequestedSubmit: (e) => Message.RequestedSubmit(e), RequestedEdit: () => undefined, RequestedOpen: () => undefined, RequestedCreate: () => undefined, RequestedAction: () => undefined, RequestedReorder: () => undefined, RequestedView: () => undefined, Selected: () => undefined, }, }),})Questions that depend on an answer
Pick "Something's broken" and the form asks for steps and a browser; pick Billing and it asks for your plan.
Contact support
Tell us what you need. A person reads every request, usually within a working day.
Fields marked with an asterisk are required.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import { Schema } from "effect"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import type { FormConfig, Item, Property } from "./ui/uicontext/components/domain/Record.ts"import { FormView } from "./ui/uicontext/components/kit/FormView.ts"import { Value } from "./ui/uicontext/components/kit/Property.ts"// A support request that asks only what each kind of request needs. "Steps to reproduce" and// "Which browser?" appear for a bug, "Your plan" for a billing question. A question its condition// hides isn't checked and isn't sent, even if it was answered before the person changed their mind.const fields: ReadonlyArray<Property> = [ { key: "kind", label: "Kind", kind: "select", options: [ { value: "bug", label: "Something's broken", tone: "red" }, { value: "billing", label: "Billing", tone: "green" }, { value: "question", label: "A question", tone: "blue" }, ], }, { key: "steps", label: "Steps to reproduce", kind: "text" }, { key: "browser", label: "Browser", kind: "select", options: [ { value: "chromium", label: "Chromium-based" }, { value: "firefox", label: "Firefox" }, { value: "safari", label: "Safari" }, { value: "other", label: "Something else" }, ], }, { key: "plan", label: "Plan", kind: "select", options: [ { value: "starter", label: "Starter" }, { value: "team", label: "Team" }, { value: "business", label: "Business" }, ], }, { key: "email", label: "Email", kind: "email" }, { key: "channel", label: "Channel", kind: "select", options: [{ value: "web", label: "Web form" }] },]const form: FormConfig = { title: "Contact support", description: "Tell us what you need. A person reads every request, usually within a working day.", fields: [ { key: "kind", label: "What do you need help with?", required: true }, { key: "title", label: "Summary", required: true, help: "One line, like a subject." }, { key: "steps", label: "Steps to reproduce", required: true, help: "What you did, what you expected, and what happened.", when: { key: "kind", op: "is", value: ["bug"] }, }, { key: "browser", label: "Which browser?", when: { key: "kind", op: "is", value: ["bug"] } }, { key: "plan", label: "Your plan", required: true, when: { key: "kind", op: "is", value: ["billing"] } }, { key: "email", label: "Where should we reply?", required: true }, { key: "channel", hidden: true }, ], defaults: { channel: "web" }, submitLabel: "Send request", success: "We've got it. You'll hear from us at the address you gave.",}const Message = UiSchema.TaggedEnum({ RequestedSubmitTicket: { request: Schema.String, values: Schema.Record(Schema.String, Value) },})export const SupportForm = Html.component({ name: "SupportForm", model: Schema.Struct({ outcome: Schema.NullOr(Outcome) }), message: Message,}).make({ init: () => ({ outcome: null }), update: { RequestedSubmitTicket: ($, { request }) => $.evolve({ outcome: { _tag: "Accepted", request } }), }, view: (h, { outcome }) => FormView(h, { props: { label: "Contact support", agentId: "support-form", records: [] as ReadonlyArray<Item>, fields, view: { layout: "form", form }, outcome, }, onEmit: { RequestedSubmit: (e) => Message.RequestedSubmitTicket(e), RequestedEdit: () => undefined, RequestedOpen: () => undefined, RequestedCreate: () => undefined, RequestedAction: () => undefined, RequestedReorder: () => undefined, RequestedView: () => undefined, Selected: () => undefined, }, }),})Closed
A form that takes no responses for now, with the reason in place of its questions.
Beta sign-up
Try the new triage view before everyone else.
This form is closed
The beta is full. Sign-ups open again in January.
import { Html } from "@effect/ui/html"import { Schema } from "effect"import type { FormConfig } from "./ui/uicontext/components/domain/Record.ts"import { FormView } from "./ui/uicontext/components/kit/FormView.ts"import { issueFields, issues } from "./issues.ts"// `closed` stops a form taking responses and tells visitors why, without taking it down.const form: FormConfig = { title: "Beta sign-up", description: "Try the new triage view before everyone else.", fields: [{ key: "title", label: "Your team's name", required: true }], closed: "The beta is full. Sign-ups open again in January.",}export const ClosedForm = Html.component({ name: "ClosedForm", model: Schema.Struct({}), message: Schema.Never,}).make({ init: () => ({}), update: ($) => $.model, view: (h) => FormView(h, { props: { label: "Beta sign-up", records: issues, fields: issueFields, view: { layout: "form", form } }, onEmit: { RequestedSubmit: () => undefined, RequestedEdit: () => undefined, RequestedOpen: () => undefined, RequestedCreate: () => undefined, RequestedAction: () => undefined, RequestedReorder: () => undefined, RequestedView: () => undefined, Selected: () => undefined, }, }),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
agentId | string | form:<label> | This form's id for agents, unique on the page; form:<label> by default |
labelrequired | string | — | The form's accessible name, and its title when the view has no form config |
recordsrequired | Item[] | — | The collection's records; a form doesn't show them, but takes them like every record view |
fieldsrequired | Property[] | — | The collection's fields; the form asks for the ones its view lists |
view | ViewInput | — | The view; view.form says what the form asks |
outcome | Outcome | null | — | The app's answer to the latest RequestedSubmit; null until there is one |
locale | string | — | The locale numbers and dates are formatted in; the browser's by default |
Events
| Event | Payload | Description |
|---|---|---|
RequestedEdit | { request: string; item: string; key: string; value: Value } | A value the person changed; answer through outcomes with the same request |
RequestedOpen | { item: string } | Open the record's page |
RequestedCreate | { group?: string; title?: string; values?: Record<string, Value> } | A new record, in the group it was added from ("" for the empty group) |
RequestedAction | { action: string; items: string[] } | One of the view's actions, on these records |
RequestedReorder | { item: string; before: string | null } | Move item before before, or last when it's null |
RequestedView | { view: View } | The person changed how the view shows records (its sort, grouping, widths); pass it back as view |
Selected | { items: string[] } | The selection changed |
RequestedSubmit | { request: string; values: Record<string, Value> } | One response: every asked value and the form's defaults; answer through outcome |
Slots
share
Agent actions
| Action | Description |
|---|---|
ChangedField | Fill one field: { field: its key, value }. Text, email, URL, phone and date take a string; number and rating a number; checkbox a boolean; select and person one choice value; multi-select, relation and attachment a list. |
SubmittedForm | Submit the response. Errors, if any, show in the snapshot's errors. |
ClickedSubmitAnother | Start another response after one went in. |
Accessibility
- Each field ties its label, help and error to the control, and marks it
aria-requiredandaria-invalid. - Choice cards and stars are radio groups: one Tab stop, and the arrow keys, Home and End move the choice.
- A field shows its error once you leave it. Submitting with errors lists them in an alert and moves focus to the first one; a refused response keeps what was typed.
- Agents fill fields by key, submit, and read any errors back from the page.