Text inputs, selects and textareas in a labeled field with a hint and an error.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { FormField, Input, Select, Textarea } from "./ui/uicontext/components/FormControls.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ form: { display: "grid", gap: 16, inlineSize: "min(100%, 24rem)", justifyItems: "start" }, field: { justifySelf: "stretch" },})const Message = UiSchema.TaggedEnum({ UpdatedName: { name: Schema.String }, Submitted: {},})export const NewProjectForm = Html.component({ name: "NewProjectForm", model: Schema.Struct({ name: Schema.String, tried: Schema.Boolean }), message: Message,}).make({ init: () => ({ name: "", tried: false }), update: { UpdatedName: ($, { name }) => $.evolve({ name }), Submitted: ($) => $.evolve({ tried: true }), }, view: (h, { name, tried }) => { const error = tried && name.trim().length < 2 ? "Give the project a name of at least two letters." : undefined return h.form.attrs({ novalidate: "", ...sx(styles.form) }).on("submit", Message.Submitted()).children( FormField(h, { id: "project-name", label: "Name", style: styles.field, ...(error === undefined ? {} : { error }) }, (attrs) => Input(h, { invalid: error !== undefined }) .attrs({ ...attrs, placeholder: "Website refresh", autocomplete: "off" }) .value(name) .on("input", ({ value }) => Message.UpdatedName({ name: value }))), FormField(h, { id: "project-access", label: "Who can see it", description: "You can change this later.", style: styles.field }, (attrs) => Select(h).attrs(attrs).children( h.option.attr("value", "team").children("Everyone on the team"), h.option.attr("value", "private").children("Only people I invite"), )), FormField(h, { id: "project-summary", label: "Summary", style: styles.field }, (attrs) => Textarea(h).attrs({ ...attrs, rows: "3", placeholder: "What is this project for?" })), Button(h, { tone: "primary", type: "submit" }).children("Create project"), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/form-controlsUsage
import { FormField, Input } from "./ui/uicontext/components/FormControls.ts"const Message = UiSchema.TaggedEnum({ ChangedEmail: { email: Schema.String } })export const EmailField = (h: Html.H<typeof Message.Type>, email: string) => FormField(h, { id: "invite-email", label: "Email", description: "We'll send the invitation here." }, (attrs) => Input(h) .attrs({ ...attrs, type: "email", autocomplete: "email" }) .value(email) .on("input", ({ value }) => Message.ChangedEmail({ email: value })))Examples
With a hint and an error
The hint stays when an error appears. The input is marked invalid, and both are read with it.
Teammates mention you with @handle.
A handle can't have spaces.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { FormField, Input } from "./ui/uicontext/components/FormControls.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ form: { display: "grid", gap: 16, inlineSize: "min(100%, 22rem)" },})const Message = UiSchema.TaggedEnum({ ChangedHandle: { handle: Schema.String } })/** The app's rule for a handle, checked as the person types. */const problemWith = (handle: string): string | undefined => handle === "" ? "Choose a handle." : /\s/u.test(handle) ? "A handle can't have spaces." : handle.length < 3 ? "Use at least three characters." : undefinedexport const HandleField = Html.component({ name: "HandleField", model: Schema.Struct({ handle: Schema.String }), message: Message,}).make({ init: () => ({ handle: "maya chen" }), update: { ChangedHandle: ($, { handle }) => $.evolve({ handle }) }, view: (h, { handle }) => { const error = problemWith(handle) return h.div.attrs(sx(styles.form)).children( // the hint and the error both describe the input, so a screen reader reads them with it FormField(h, { id: "profile-handle", label: "Handle", description: "Teammates mention you with @handle.", ...(error === undefined ? {} : { error }) }, (attrs) => Input(h, { invalid: error !== undefined }) .attrs({ ...attrs, autocomplete: "off", spellcheck: "false" }) .value(handle) .on("input", ({ value }) => Message.ChangedHandle({ handle: value }))), ) },})Select
A native select, so the phone's own picker opens on touch screens.
Due dates and digests use it.
Saved as Europe/Lisbon.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { FormField, Select } from "./ui/uicontext/components/FormControls.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ form: { display: "grid", gap: 12, inlineSize: "min(100%, 22rem)" }, note: { margin: 0, color: color.muted, fontSize: font.sm },})const zones = [ { value: "America/Los_Angeles", label: "Pacific Time (Los Angeles)" }, { value: "America/New_York", label: "Eastern Time (New York)" }, { value: "Europe/Lisbon", label: "Western European Time (Lisbon)" }, { value: "Asia/Singapore", label: "Singapore Time" },] as constconst Message = UiSchema.TaggedEnum({ PickedZone: { zone: Schema.String } })export const TimeZoneSelect = Html.component({ name: "TimeZoneSelect", model: Schema.Struct({ zone: Schema.String }), message: Message,}).make({ init: () => ({ zone: "Europe/Lisbon" }), update: { PickedZone: ($, { zone }) => $.evolve({ zone }) }, view: (h, { zone }) => h.div.attrs(sx(styles.form)).children( FormField(h, { id: "workspace-zone", label: "Time zone", description: "Due dates and digests use it." }, (attrs) => Select(h).attrs(attrs).value(zone).on("change", ({ value }) => Message.PickedZone({ zone: value })).children( zones.map((option) => h.option.key(option.value).attr("value", option.value).children(option.label)), )), h.p.attrs(sx(styles.note)).children(`Saved as ${zone}.`), ),})Read-only and disabled
Read-only for a value people may copy but not change; disabled for one that doesn't apply right now.
Only an owner can change it.
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { FormField, Input, Textarea } from "./ui/uicontext/components/FormControls.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ form: { display: "grid", gap: 16, inlineSize: "min(100%, 22rem)" },})export const LockedFields = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.form)).children( // read-only: still focusable and copyable, and sent with the form FormField(h, { id: "workspace-url", label: "Workspace URL", description: "Only an owner can change it." }, (attrs) => Input(h).attrs({ ...attrs, readonly: "" }).value("northpeak.app/acme-studio")), // disabled: skipped by Tab and left out of the form FormField(h, { id: "billing-note", label: "Billing note" }, (attrs) => Textarea(h).attrs({ ...attrs, rows: "2", disabled: "" }).value("Invoices go to [email protected].")), )API reference
InputView function
Options
| Name | Type | Description |
|---|---|---|
invalid | boolean | Marks the value as wrong: a red border, and aria-invalid for assistive technology. |
style | Style | StyleX styles applied after the control's own. |
SelectView function
Options
| Name | Type | Description |
|---|---|---|
invalid | boolean | Marks the value as wrong: a red border, and aria-invalid for assistive technology. |
style | Style | StyleX styles applied after the control's own. |
TextareaView function
Options
| Name | Type | Description |
|---|---|---|
invalid | boolean | Marks the value as wrong: a red border, and aria-invalid for assistive technology. |
style | Style | StyleX styles applied after the control's own. |
InputGroupView function
Options
| Name | Type | Description |
|---|---|---|
start | string | — |
end | string | — |
style | Style | — |
FormFieldView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
idrequired | string | — | The control's id. The label, description and error take ids made from it. |
labelrequired | string | — | The label's text, which names the control. |
description | string | — | A hint under the control, read out with it. |
error | string | — | What's wrong with the value. Shown under the control, announced, and marks the control invalid. |
hint | string | — | A short note at the end of the label's line: a count such as "12/48", or "Max 5 MB". |
hintOver | boolean | — | The hint warns, as a count past its limit does. |
required | boolean | — | An empty value is an error: a mark after the label, and aria-required on the control. |
optional | boolean | — | May stay empty, said after the label: for a form where most fields are required. |
status | { readonly tone: "pending" | "success"; readonly text: string } | — | Where an async check stands, in the error's place while there's no error: "Checking…", "acme is available". |
layout | "stacked" | "inline" | "inline-end" | stacked | Where the label sits: above the control (text fields, selects, a radio group), or beside it, with the control first (a checkbox) or last (a switch in a settings row); stacked by default |
group | boolean | — | The control is a group of controls (a radio group, a segmented control): a group can't be the target of a <label for>, so the label names it through aria-labelledby instead |
labelStyle | Style | — | StyleX styles for the label: a row field's label that hides behind column headings. |
style | Style | — | StyleX styles for the field's wrapper. |
Accessibility
- Each field connects its label, hint and error to the control with
for,aria-describedbyandaria-invalid. - Errors are announced as they appear. Forms set
novalidate, so your validation decides what to show.