Form unstable
A form that moves focus to its first invalid control after a submit, so a keyboard or screen reader user lands on the error without looking for it.
Form is a headless component: after a submit it runs
FormFocusInvalid, a Command that reads the form's element once the
render is committed. It keeps no state.
Anatomy
| Part | Element |
|---|---|
root |
<form> |
root takes the consumer's builder and returns it, so the consumer goes
on chaining and adds the fields and the submit button with
.children(...): form.root(h.form.class("signup")).children(...). It
adds no markup.
Data attributes
None.
Keyboard
Form adds no key handler. Enter in a text input and a press of a submit
button submit the form, as in any <form>.
Usage
Form doesn't validate and holds no errors. Validate in update, with
Validation from effect/ui, and show each outcome on its control: a
Field with invalid: true writes aria-invalid="true" on its control,
and a control drawn without a field takes the attribute directly. The root
writes novalidate, so the browser's own checks stay out of the way.
The mode given to Validation.make decides when a field starts showing
its error: "onSubmit" (the default), "onChange" or "onBlur". A
tried submit shows every field, and an error clears once the value is
corrected. Field shows the "onBlur" wiring.
Every submit emits Submitted with the form's named controls, and the
browser's own submission is prevented. A consumer that keeps the values in
its Model ignores the payload and reads the Model.
Once the render that follows a submit is committed, the form focuses the
first element with aria-invalid="true", in document order, inside it or
among the controls outside it that name it with a form attribute,
and selects the text of an <input>. An invalid element that can't take
focus itself, such as the root of a radio group, passes it to the first
element inside it, in document order, that Tab can reach. A native radio,
invalid itself or inside an invalid element, passes it to the checked
radio of its group, where Tab stops. An invalid element that neither
takes focus nor holds one that does, such as a disabled input, is
skipped. With no invalid element the focus stays where it was.
The consumer's update for Submitted runs before that render, so the
errors it records are the ones the form reads.
The message log records Submitted with every value in it. Leave name
off a secret control such as a password, so the form doesn't report it,
and carry its value as Schema.Redacted in the consumer's own messages.
Limits
Document order decides which element comes first, so a positive
tabindex moves none ahead.
Errors that arrive later, such as a server's, are focused only on
request: put the consumer's own Html.ref on the root and run
FormFocusInvalid from the update that records them. Disabled controls
are skipped, so re-enable them in that same update.
To send focus somewhere else after a failed submit, such as to an error
summary, draw a plain h.form with novalidate and a submit handler.
Example A sign-up form whose email field is invalid
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Field, Form } from "effect/ui/kit"import { Effect, Schema } from "effect"
const SignupMessage = UiSchema.TaggedEnum({ SubmittedForm: {} })
const Signup = Html.component({ name: "Signup", model: Schema.Struct({ submitted: Schema.Boolean }), message: SignupMessage}).make({ init: () => ({ submitted: true }), update: { SubmittedForm: () => ({ submitted: true }) }, view: (h, model) => Form(h, { onEmit: () => SignupMessage.SubmittedForm(), view: (form) => form.root(h.form.class("signup")).children( Field(h, { props: { invalid: model.submitted }, view: (field) => field.root(h.div).children( field.label(h.label).children("Email"), field.control(h.input).attr("name", "email"), field.error(h.div)?.children("Enter your email") ) }), h.button.attr("type", "submit").children("Sign up") ) })})
// The form keeps the consumer's class and turns the browser's checks off.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Signup, {}))if (!/<form class="signup" novalidate>/.test(html)) throw new Error("the form isn't a styled form with novalidate")Commands
FormFocusInvalid
unstableFocuses the first invalid control of the form ref names, as Form
does after a submit, once the issuing update's render is committed. It
sends no message.
Details
Run it from the update that records errors arriving after the submit.
A ref resolves within the component that runs the Command, so the
consumer puts its own ref on the root, drawn with Form or a plain
h.form.
Signature
declare const FormFocusInvalid: Command.Factory<{ readonly ref: Renderer.Ref<HTMLFormElement>;}, Component.Noop>Example Focusing the field a server rejected
import { Html } from "effect/ui/html"import { FormFocusInvalid } from "effect/ui/kit"
const SignupForm = Html.ref("SignupForm", "form")
// view: form.root(h.form.ref(SignupForm))// update: $.evolve({ form: Signup.reject(errors) }).run(command)const command = FormFocusInvalid.make({ ref: SignupForm })Components
A headless form. Call it with the consumer's h, an onEmit for
Submitted, and a view closure that draws the root with
form.root(h.form) around the fields and the submit button.
Details
root(form) returns the builder it was given, so the consumer chains
classes, attributes and children on it. It takes only a <form>.
The form's novalidate wins over the one the consumer set on the builder
before the part. The part writes it when it is applied, so an attribute the
consumer chains after form.root(...) is the consumer's to answer for.
root handles submit on a layer of its own, so a submit handler the
consumer adds to the form still runs, after the form's.
Every submit emits Submitted, and then, once the render is committed,
the form focuses the first control with aria-invalid="true", inside it or
naming it with form.
Signature
declare const Form: Headless<{}, { readonly _tag: "Submitted"; readonly entries: readonly Array<readonly [string, string]>; readonly fields: { [key: string]: string; };}, { root: Part<"form", [], never>;}, never, never, never, never, never, never, {}, { readonly _tag: "SubmittedRoot"; readonly entries: readonly Array<readonly [string, string]>; readonly fields: { [key: string]: string; };}, QueryEntries<{}, {}>>Models
What a Form emits.
Signature
type FormEvents = typeof FormEvents.TypeSchemas
FormEvents
unstableSubmitted reports a submit of the form, with its named controls:
fields holds the first value of each name, and entries every name and
value in document order, for names used more than once. Files are left
out.
Signature
declare const FormEvents: TaggedEnum<{ Submitted: TaggedStruct<"Submitted", { entries: $Array<Tuple<readonly [String, String]>>; fields: $Record<String, String>; }>;}>