Form

A form with a vertical layout that validates its Field components on submit.

Import

import { Form } from '@matix/components/form';

Usage

Form wraps Base UI Form in a vertical stack layout. On submit, it validates every Field. If a field is invalid, it stops the submit and moves focus to the first invalid control.

We'll never share your email.

With Validation

The browser constraints (required, type="email", minLength and others) run on submit, and Field shows their messages. To show your own messages, pass error to each Field. A validation library such as react-hook-form can supply them:

<Form>
  <Field label="Email" error="Please enter a valid email.">
    <Input type="email" />
  </Field>
  <Field label="Password" error="Password must be at least 8 characters.">
    <Input type="password" />
  </Field>
  <Button type="submit" variant="solid">
    Sign in
  </Button>
</Form>

Server Errors

Pass errors with one key for each Field name. A field clears its error when the user changes it.

<Form errors={{ email: 'This email is already registered.' }} onFormSubmit={save}>
  <Field label="Email" name="email">
    <Input type="email" />
  </Field>
</Form>

Custom Styles

import * as stylex from '@stylexjs/stylex';

const custom = stylex.create({
  horizontal: { flexDirection: 'row', alignItems: 'flex-end', gap: '12px' },
});

<Form sx={custom.horizontal}>
  <Field label="Search">
    <Input placeholder="Search…" />
  </Field>
  <Button type="submit">Go</Button>
</Form>;

API Reference

PropTypeDefaultDescription
sxStyleXStyles—StyleX style override
childrenReactNode—Form content — typically Field components
onSubmitReact.FormEventHandler<HTMLFormElement>—Submit handler. Runs only when all fields are valid
onFormSubmit(values, eventDetails) => void—Gets the values by Field name, and prevents the native submit
errorsRecord<string, string | string[]>—External errors by Field name, for example from a server
validationMode'onSubmit' | 'onBlur' | 'onChange''onSubmit'When the fields validate
actionsRefRefObject<{ validate(name?: string): void }>—Validates all fields, or one field, on demand

All other props are forwarded to the underlying Base UI Form.

Accessibility

  • Renders a native <form> element — full semantic form behavior.
  • Sets noValidate, so Field shows the validation messages instead of the browser tooltips.
  • An invalid submit moves focus to the first invalid control, and the control gets aria-invalid="true".
  • Submit via Enter key works automatically on form controls.