Forms

A form is a column of labelled fields with one submit. Labels stay visible, errors sit under their fields, and nothing is lost on the way out.

Lay a form out as one column: a visible label above each field, the error or hint under it, and one primary button at the end that names what it saves.

Like name@example.com.

Clear the email and press Save profile: the message lands under the field, focus moves to it, and the rest of the form keeps what was typed. Start typing an address and the message updates with each key, then goes once the address is whole. Discard changes goes back to what was last saved.

Do and don't#

Do

Give every field a Label that stays visible, joined through htmlFor. A placeholder can show an example value.

Don't

Use the placeholder as the label: it disappears with the first keystroke, and with it what the field was for.

Do

Check a field when the form is submitted, and again as it is corrected once it has been marked wrong.

No one here is called ali.

Don't

Mark a field wrong while it is still being typed for the first time, or disable the submit button without saying what is missing.

Fields#

The valueUse
One line of text, an email, a URLInput with the matching type and autoComplete
A passwordPasswordInput, with autoComplete current-password to sign in or new-password to choose one
A number: a count, a portNumberInput, with min and max where there are limits
Several lines of textTextarea
Several lines rendered as MarkdownMarkdownEditor, see Writing Markdown
One of a fixed listSelect, or a RadioGroup for two to five, see Choosing a control
Yes or no, applied on submitCheckbox
A file: an avatar, an icon, a file to importFileDrop with a FileDropTrigger
Text in a query language, with completionQueryBox

Press Create filter with Matching empty: the error lands under the QueryBox as under any other field. Type from: in it for the completions; Enter on the query as typed submits the form.

Mark the optional fields "(optional)" in the label rather than every required one with an asterisk: most fields in a short form are required, and a reader should not have to decode a symbol.

A file#

AL

Press Upload…, drop an image on the row, or copy one and paste it with the pointer on the row: each puts it in the avatar. A PDF gets the message under the row.

  • Always give the field a FileDropTrigger. A phone has no drag and drop, and mostly cannot paste a file either; the button is the one way in that works everywhere.
  • Make the zone the whole field, the row with the picture and its buttons, not the picture alone: an avatar is too small a place to aim a dragged file at.
  • Name the zone with aria-labelledby, pointing at its Label. That it takes a drop or a paste is said behind an InfoPopover after the label, not in a line beside the controls.
  • Pass onReject, and say what the field takes in a FieldError under it: "Choose a PNG, JPEG or WebP image." A file the app turns away after reading it, too large or not an image after all, is reported the same way, saying what happened (see Principles).
  • A viewer or an editor that opens the file dropped on it is a FileDrop with purpose="pane", put on the region with asChild; the Binary inspector takes a file dropped anywhere on its window.

Errors#

  • The message is a FieldError under its field, the field gets aria-invalid, and aria-describedby points at the message, so a screen reader reads it with the field. A limit the reader needs before anything goes wrong, a format or a length, is a FieldDescription, wired the same way. What the field is for, when its label does not say, goes behind an InfoPopover after the label instead; see Writing.
  • After a failed submit, move focus to the first invalid field.
  • Say how to fix it: "Enter an email address, like alice@example.com", not "Invalid input".
  • An error from the server about the whole form, a lost connection or a conflict, sits above the buttons, where people look before pressing them again.

Leaving with unsaved changes#

A form that holds typed work guards it. When people navigate away before saving, ask with UnsavedChangesDialog: Keep editing is its primary action, and Discard and leave is the alternative. It opens with focus on Keep editing, so a stray Enter keeps the work. The dialog takes no router; the app's own navigation blocker opens it.

Change the name and press Settings: the dialog asks, and Keep editing returns to the field with the name as typed. Save first, or change nothing, and Settings leaves at once.

Register the work with useDirtySource from @orz/ui/lib/unsaved-guard, and have the blocker ask hasUnsavedWork(), which reads every source the moment a navigation starts. A form that becomes clean and navigates in one callback (save and open the new record, delete and go to the list, discard and go back) calls the markClean that useDirtySource returns before it navigates. The state that callback sets has not rendered yet, so without it the source still reads dirty, and people who have just saved are asked whether to leave without saving.

tsx
const markClean = useDirtySource(() => draft !== saved);

async function saveAndOpen() {
  const { id } = await save(draft);
  markClean();
  setSaved(draft);
  navigate(`/items/${id}`);
}

Settings that apply as they change (Switches, see Choosing a control) have nothing unsaved, so a settings page of Switches needs no guard and no Save button.

On each platform#

Desktop & pointer#

  • Enter in a single-line field submits the form; Tab moves through the fields in reading order.
  • Two short related fields, a first and last name, may share a row on a wide screen. Everything else stays in one column.

Touch#

  • The type and inputMode pick the on-screen keyboard: email gets an @ key, numeric a digit pad. autoComplete lets the phone fill the field in.
  • Inputs use a 16px font on touch, so the phone does not zoom into the field on focus.
  • The on-screen keyboard covers the lower half of the screen. Keep the submit button in the flow under the last field, not pinned to the bottom, where the keyboard hides it.

In the demos