Forms

Spread the input that @k8ordo/form derives for a field straight onto @k8ordo/ui’s inputs. You also learn which component fits each kind of field and what else to pass.

On this page

Spreading input

talk-form.tsx
const title = form.field('title');

<FormControl
  errorText={title.error}
  label="Title"
  required={title.required}
  renderInput={(props) => <TextField {...props} {...title.input} />}
/>

form.field(name).input holds the attributes @k8ordo/form derives from the declaration, such as type and required. How useForm works is covered in @k8ordo/form’s Get Started.

Give FormControl only label, errorText and required. A non-empty errorText marks the field invalid, and FormControl creates the id and the aria-* links itself. Spread input after the props FormControl hands over, with nothing taken out first. @k8ordo/form writes only attributes the native element takes: no type on a plain string field, and min and max as numbers. TextField renders the date and time types as well, and a plain string field spreads onto Textarea as it is.

Every component works uncontrolled, with its value held in the input element. A reset puts it back to defaultValue, and isDirty compares against defaultValue. A value a component changes in code, such as a stepper or a chosen option, fires an input event the way typing does.

Components by kind

For each kind of field, the components that fit and what to pass besides {...props} {...input}.

  • field.string(), field.email(), field.url(), field.tel() and the date and time kinds: TextField, with nothing else to pass. Only field.string(), which derives no type, spreads onto Textarea too.
  • field.password(): PasswordInput. Its show/hide toggle still works after type is spread.
  • field.number(), field.integer(): NumberField or Slider. They take step="any" and the defaultValue echoed as a string.
  • field.enum([…]): Select, Radio or RadioCard. Pass options, and labelAs="group" on FormControl for radios.
  • field.boolean(): Checkbox or Switch. Pass label and errorText; they need no FormControl and draw the error text themselves. The value="true" in input reaches the <input> as well.
  • field.list(field.enum([…])): CheckboxGroup, CheckboxCard or Autocomplete. Pass options, and labelAs="group" on FormControl for CheckboxGroup and CheckboxCard.
  • field.file(): FileField.Root, with a FileField.Trigger and a FileField.ItemList inside, and labelAs="group" on FormControl.

Choice components

tags-field.tsx
const tags = form.field('tags');

<FormControl
  errorText={tags.error}
  label="Tags"
  labelAs="group"
  renderInput={(props) => (
    <CheckboxCard
      {...props}
      {...tags.input}
      options={options}
    />
  )}
/>

For a checkbox group, what was checked comes back on input.defaultValue as an array after a submission. CheckboxGroup, CheckboxCard and Autocomplete take an array as defaultValue, so spreading input restores it. The count rule derived from the list’s min and max applies to Autocomplete too. It submits through a hidden <select multiple> that stays even with nothing selected, and the error is attached to that element. Focus sent there after a failure moves on to the text input.

Spread input onto Radio and RadioCard directly. They read defaultValue as the selected option and put required on every radio.

For required to take effect on a Select, pass placeholder: a <select> without an empty option always has a choice selected. placeholder puts an option whose value is the empty string first, and parseForm reads that as no choice, so a required enum reports it and an optional one parses to undefined.

Caveats

NumberField renders type="text" so it can format and step the value, and the browser does not check min and max on a text input. Instead, NumberField clamps an out-of-range value into range when the field is left and when Enter is pressed in it. An out-of-range value is submitted only without JavaScript, and then the server answers with the field’s rangeUnderflow or rangeOverflow message.

A regex on a field rendered as a Textarea reaches the markup as pattern, but a <textarea> ignores that attribute. The check runs on the server only: formFields does not know which element renders the field.

FileField submits what FileField.ItemList shows. A file removed from the list leaves the submission, and a reset empties the list.

Form-level error

talk-form.tsx
{form.formError.message !== undefined && (
  <Alert
    {...form.formError.props}
    message={form.formError.message}
    tone="error"
  />
)}

form.formError can be shown with Alert. Alert passes id and tabIndex through to its element, so spreading form.formError.props lets it take focus.

Alert has role="alert", so a screen reader may read the message twice: once when it appears, once when focus lands on it. An error summary with an alert role behaves the same way.

k8ordo

React libraries that use Baseline features without holding back.