---
title: Form
subtitle: A native form element with consolidated error handling.
description: A high-quality, unstyled React form component with consolidated error handling.
---

> FineSoft Components documentation. Independent Stencil port; not the official Base UI website.
>
> React API text and `@base-ui/react` examples are upstream references. FineSoft Stencil examples use `finesoft-components`. Do not treat the two packages as interchangeable.

# Form

A high-quality, unstyled React form component with consolidated error handling.

## Demo

### Tailwind

This example shows how to implement the component using Tailwind CSS.

```tsx
/* docs-comparison.tsx */
import { StencilComparison } from 'docs/src/components/StencilComparison';
import ReactForm from './react-form';
import StencilForm from './stencil-form';
import { StencilFormView } from './stencil-form-view';

export default function FormComparison() {
  return <StencilComparison component="form-hero-tailwind" react={<ReactForm />} stencil={<StencilFormView />} clientStencil={<StencilForm />} />;
}
```

```tsx
/* react-form.tsx */
'use client';
import * as React from 'react';
import { Field } from '@base-ui/react/field';
import { Form } from '@base-ui/react/form';
import { Button } from '@base-ui/react/button';

export default function ExampleForm() {
  const [errors, setErrors] = React.useState({});
  const [loading, setLoading] = React.useState(false);

  return (
    <Form
      className="flex w-full max-w-64 flex-col gap-4"
      errors={errors}
      onSubmit={async event => {
        event.preventDefault();
        const formData = new FormData(event.currentTarget);
        const value = formData.get('url') as string;

        setLoading(true);
        const response = await submitForm(value);
        const serverErrors = {
          url: response.error,
        };

        setErrors(serverErrors);
        setLoading(false);
      }}
    >
      <Field.Root name="url" className="flex flex-col items-start gap-1">
        <Field.Label className="text-sm font-bold text-neutral-950 dark:text-white">Homepage</Field.Label>
        <Field.Control
          type="url"
          required
          defaultValue="https://example.com"
          placeholder="https://example.com"
          pattern="https?://.*"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
        <Field.Error className="text-sm text-red-700 dark:text-red-400" />
      </Field.Root>
      <Button
        disabled={loading}
        focusableWhenDisabled
        type="submit"
        className="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-data-disabled:bg-neutral-100 active:not-data-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-data-disabled:bg-neutral-800 dark:active:not-data-disabled:bg-neutral-700 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400"
      >
        Submit
      </Button>
    </Form>
  );
}

async function submitForm(value: string) {
  // Mimic a server response
  await new Promise(resolve => {
    setTimeout(resolve, 1000);
  });

  try {
    const url = new URL(value);

    if (url.hostname.endsWith('example.com')) {
      return { error: 'The example domain is not allowed' };
    }
  } catch {
    return { error: 'This is not a valid URL' };
  }

  return { success: true };
}
```

```tsx
/* stencil-form.tsx */
'use client';
import * as React from 'react';
import { StencilFormView } from './stencil-form-view';

export default function StencilForm() {
  const [errors, setErrors] = React.useState({});
  const [loading, setLoading] = React.useState(false);

  return (
    <StencilFormView
      errors={errors}
      onSubmit={async event => {
        event.preventDefault();
        const formData = new FormData(event.target as HTMLFormElement);
        const value = formData.get('url') as string;

        setLoading(true);
        const response = await submitForm(value);
        const serverErrors = {
          url: response.error,
        };

        setErrors(serverErrors);
        setLoading(false);
      }}
      loading={loading}
    />
  );
}

async function submitForm(value: string) {
  // Mimic a server response
  await new Promise(resolve => {
    setTimeout(resolve, 1000);
  });

  try {
    const url = new URL(value);

    if (url.hostname.endsWith('example.com')) {
      return { error: 'The example domain is not allowed' };
    }
  } catch {
    return { error: 'This is not a valid URL' };
  }

  return { success: true };
}
```

```tsx
/* stencil-form-view.tsx */
import type * as React from 'react';
import type { FormErrors } from 'finesoft-components/define-custom-elements';

interface StencilFormViewProps {
  errors?: FormErrors;
  loading?: boolean;
  onSubmit?: React.FormEventHandler<HTMLElement>;
}

export function StencilFormView({ errors, loading = false, onSubmit }: StencilFormViewProps) {
  return (
    <fs-form className="flex w-full max-w-64 flex-col gap-4" errors={errors} onSubmit={onSubmit}>
      <fs-field-root
        name="url"
        className="[&::part(field)]:box-border [&::part(field)]:border-0 [&::part(field)]:border-solid [&::part(field)]:flex [&::part(field)]:flex-col [&::part(field)]:items-start [&::part(field)]:gap-1"
      >
        <fs-field-label className="text-sm font-bold text-neutral-950 dark:text-white">Homepage</fs-field-label>
        <fs-field-control
          type="url"
          required
          defaultValue="https://example.com"
          placeholder="https://example.com"
          pattern="https?://.*"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
        <fs-field-error className="text-sm text-red-700 dark:text-red-400" />
      </fs-field-root>
      <fs-button
        disabled={loading}
        focusableWhenDisabled
        type="submit"
        className="[&::part(control)]:p-0 [&::part(control)]:[font-family:inherit] [&::part(control)]:flex [&::part(control)]:h-8 [&::part(control)]:items-center [&::part(control)]:justify-center [&::part(control)]:gap-2 [&::part(control)]:rounded-none [&::part(control)]:border [&::part(control)]:border-neutral-950 [&::part(control)]:bg-white [&::part(control)]:px-3 [&::part(control)]:text-sm [&::part(control)]:leading-none [&::part(control)]:whitespace-nowrap [&::part(control)]:font-normal [&::part(control)]:text-neutral-950 [&::part(control)]:select-none not-data-disabled:[&::part(control)]:hover:bg-neutral-100 not-data-disabled:[&::part(control)]:active:bg-neutral-200 [&::part(control):focus-visible]:outline-2 [&::part(control):focus-visible]:-outline-offset-1 [&::part(control):focus-visible]:outline-neutral-950 dark:[&::part(control):focus-visible]:outline-white data-disabled:[&::part(control)]:border-neutral-500 data-disabled:[&::part(control)]:text-neutral-500 disabled:[&::part(control)]:border-neutral-500 disabled:[&::part(control)]:text-neutral-500 dark:[&::part(control)]:border-white dark:[&::part(control)]:bg-neutral-950 dark:[&::part(control)]:text-white dark:not-data-disabled:[&::part(control)]:hover:bg-neutral-800 dark:not-data-disabled:[&::part(control)]:active:bg-neutral-700 dark:data-disabled:[&::part(control)]:border-neutral-400 dark:data-disabled:[&::part(control)]:text-neutral-400"
      >
        Submit
      </fs-button>
    </fs-form>
  );
}
```

### CSS Modules

This example shows how to implement the component using CSS Modules.

```css
/* index.module.css */
.Form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 100%;
  max-width: 16rem;
}

.Field {
  display: flex;
  flex-direction: column;
  align-items: start;
  gap: 0.25rem;
}

.Label {
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 100%;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Error {
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: oklch(50.5% 0.213 27.518deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.4% 0.191 22.216deg);
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  outline: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
```

```tsx
/* docs-comparison.tsx */
import { StencilComparison } from 'docs/src/components/StencilComparison';
import ReactForm from './react-form';
import StencilForm from './stencil-form';
import { StencilFormView } from './stencil-form-view';

export default function FormComparison() {
  return <StencilComparison component="form-hero-css-modules" react={<ReactForm />} stencil={<StencilFormView />} clientStencil={<StencilForm />} />;
}
```

```tsx
/* react-form.tsx */
'use client';
import * as React from 'react';
import { Field } from '@base-ui/react/field';
import { Form } from '@base-ui/react/form';
import { Button } from '@base-ui/react/button';
import styles from './index.module.css';

export default function ExampleForm() {
  const [errors, setErrors] = React.useState({});
  const [loading, setLoading] = React.useState(false);

  return (
    <Form
      className={styles.Form}
      errors={errors}
      onSubmit={async event => {
        event.preventDefault();
        const formData = new FormData(event.currentTarget);
        const value = formData.get('url') as string;

        setLoading(true);
        const response = await submitForm(value);
        const serverErrors = {
          url: response.error,
        };

        setErrors(serverErrors);
        setLoading(false);
      }}
    >
      <Field.Root name="url" className={styles.Field}>
        <Field.Label className={styles.Label}>Homepage</Field.Label>
        <Field.Control type="url" required defaultValue="https://example.com" placeholder="https://example.com" pattern="https?://.*" className={styles.Input} />
        <Field.Error className={styles.Error} />
      </Field.Root>
      <Button type="submit" disabled={loading} focusableWhenDisabled className={styles.Button}>
        Submit
      </Button>
    </Form>
  );
}

async function submitForm(value: string) {
  // Mimic a server response
  await new Promise(resolve => {
    setTimeout(resolve, 1000);
  });

  try {
    const url = new URL(value);

    if (url.hostname.endsWith('example.com')) {
      return { error: 'The example domain is not allowed' };
    }
  } catch {
    return { error: 'This is not a valid URL' };
  }

  return { success: true };
}
```

```tsx
/* stencil-form.tsx */
'use client';
import * as React from 'react';
import { StencilFormView } from './stencil-form-view';

export default function StencilForm() {
  const [errors, setErrors] = React.useState({});
  const [loading, setLoading] = React.useState(false);

  return (
    <StencilFormView
      errors={errors}
      onSubmit={async event => {
        event.preventDefault();
        const formData = new FormData(event.target as HTMLFormElement);
        const value = formData.get('url') as string;

        setLoading(true);
        const response = await submitForm(value);
        const serverErrors = {
          url: response.error,
        };

        setErrors(serverErrors);
        setLoading(false);
      }}
      loading={loading}
    />
  );
}

async function submitForm(value: string) {
  // Mimic a server response
  await new Promise(resolve => {
    setTimeout(resolve, 1000);
  });

  try {
    const url = new URL(value);

    if (url.hostname.endsWith('example.com')) {
      return { error: 'The example domain is not allowed' };
    }
  } catch {
    return { error: 'This is not a valid URL' };
  }

  return { success: true };
}
```

```tsx
/* stencil-form-view.tsx */
import type * as React from 'react';
import type { FormErrors } from 'finesoft-components/define-custom-elements';

interface StencilFormViewProps {
  errors?: FormErrors;
  loading?: boolean;
  onSubmit?: React.FormEventHandler<HTMLElement>;
}

export function StencilFormView({ errors, loading = false, onSubmit }: StencilFormViewProps) {
  return (
    <fs-form errors={errors} onSubmit={onSubmit}>
      <fs-field-root name="url">
        <fs-field-label>Homepage</fs-field-label>
        <fs-field-control type="url" required defaultValue="https://example.com" placeholder="https://example.com" pattern="https?://.*" />
        <fs-field-error />
      </fs-field-root>
      <fs-button type="submit" disabled={loading} focusableWhenDisabled>
        Submit
      </fs-button>
    </fs-form>
  );
}
```

The Stencil comparison uses `fs-form` together with the migrated
[`fs-field-*`](/react/components/field.md) and [`fs-button`](/react/components/button.md)
elements. `fs-form` is a transparent custom-element host: it renders the native
`<form>` that owns submission, native attributes, `FormData`, validation, and focus.
Consumer classes, styles, global attributes, ARIA attributes, and `data-*` attributes
are forwarded to that native form.

Pass form-level `errors` directly to `fs-form`; descendant fields resolve their messages
by `name` and clear only the field being edited. `validationMode` is inherited by fields,
and `actionsRef.current.validate()` validates all fields or one named field. A string
`action` uses native browser submission. A function `action` receives `FormData`, while
Stencil's `formSubmit` property receives the consolidated JavaScript value object and the
original submit event details. It is named without the React `on` prefix because React treats
every `on*` prop on a custom element as a DOM event listener instead of assigning the function
property. As in Base UI, either function API prevents the native submission after validation
succeeds. `formRef` exposes the rendered native form.

The code panels below are the files used by the running Stencil examples. They contain
all consumer state and behavior; there is no component-specific docs controller or hidden
structure.

## Anatomy

Form is composed together with [Field](/react/components/field.md). Import the components and place them together:

```jsx title="Anatomy"
import { Field } from '@base-ui/react/field';
import { Form } from '@base-ui/react/form';

<Form>
  <Field.Root>
    <Field.Label />
    <Field.Control />
    <Field.Error />
  </Field.Root>
</Form>;
```

## Examples

### Submit with a Server Function

Forms using `useActionState` can be submitted with a [Server Function](https://react.dev/reference/react-dom/components/form#handle-form-submission-with-a-server-function) instead of `onSubmit`.

## Demo

### Tailwind

This example shows how to implement the component using Tailwind CSS.

```tsx
/* docs-comparison.tsx */
import { StencilComparison } from 'docs/src/components/StencilComparison';
import ReactForm from './react-form';
import StencilForm from './stencil-form';
import { StencilFormView } from './stencil-form-view';

export default function FormActionComparison() {
  return <StencilComparison component="form-action-tailwind" react={<ReactForm />} stencil={<StencilFormView />} clientStencil={<StencilForm />} />;
}
```

```tsx
/* react-form.tsx */
'use client';
import * as React from 'react';
import { Field } from '@base-ui/react/field';
import { Form } from '@base-ui/react/form';
import { Button } from '@base-ui/react/button';

interface FormState {
  serverErrors?: Form.Props['errors'];
}

export default function ActionStateForm() {
  const [state, formAction, loading] = React.useActionState<FormState, FormData>(submitForm, {});

  return (
    <Form action={formAction} errors={state.serverErrors} className="flex w-full max-w-64 flex-col gap-4">
      <Field.Root name="username" className="flex flex-col items-start gap-1">
        <Field.Label className="text-sm font-bold text-neutral-950 dark:text-white">Username</Field.Label>
        <Field.Control
          type="text"
          autoComplete="username"
          required
          defaultValue="admin"
          placeholder="e.g. alice132"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
        <Field.Error className="text-sm text-red-700 dark:text-red-400" />
      </Field.Root>
      <Button
        type="submit"
        disabled={loading}
        focusableWhenDisabled
        className="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-data-disabled:bg-neutral-100 active:not-data-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-data-disabled:bg-neutral-800 dark:active:not-data-disabled:bg-neutral-700 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400"
      >
        Submit
      </Button>
    </Form>
  );
}

// Mark this as a Server Function with `'use server'` in a supporting framework like Next.js
async function submitForm(_previousState: FormState, formData: FormData) {
  // Mimic a server response
  await new Promise(resolve => {
    setTimeout(resolve, 1000);
  });

  try {
    const username = formData.get('username') as string | null;

    if (username === 'admin') {
      return { success: false, serverErrors: { username: "'admin' is reserved for system use" } };
    }

    // 50% chance the username is taken
    const success = Math.random() > 0.5;

    if (!success) {
      return {
        serverErrors: { username: `${username} is unavailable` },
      };
    }
  } catch {
    return { serverErrors: { username: 'A server error has occurred' } };
  }

  return {};
}
```

```tsx
/* stencil-form.tsx */
'use client';
import * as React from 'react';
import type { FormErrors } from 'finesoft-components/define-custom-elements';
import { StencilFormView } from './stencil-form-view';

interface FormState {
  serverErrors?: FormErrors;
}

export default function StencilForm() {
  const [state, formAction, loading] = React.useActionState<FormState, FormData>(submitForm, {});

  return <StencilFormView action={formData => React.startTransition(() => formAction(formData))} errors={state.serverErrors} loading={loading} />;
}

// Mark this as a Server Function with `'use server'` in a supporting framework like Next.js
async function submitForm(_previousState: FormState, formData: FormData) {
  // Mimic a server response
  await new Promise(resolve => {
    setTimeout(resolve, 1000);
  });

  try {
    const username = formData.get('username') as string | null;

    if (username === 'admin') {
      return { success: false, serverErrors: { username: "'admin' is reserved for system use" } };
    }

    // 50% chance the username is taken
    const success = Math.random() > 0.5;

    if (!success) {
      return {
        serverErrors: { username: `${username} is unavailable` },
      };
    }
  } catch {
    return { serverErrors: { username: 'A server error has occurred' } };
  }

  return {};
}
```

```tsx
/* stencil-form-view.tsx */
import type { FormAction, FormErrors } from 'finesoft-components/define-custom-elements';

interface StencilFormViewProps {
  action?: FormAction;
  errors?: FormErrors;
  loading?: boolean;
}

export function StencilFormView({ action, errors, loading = false }: StencilFormViewProps) {
  return (
    <fs-form action={action} errors={errors} className="flex w-full max-w-64 flex-col gap-4">
      <fs-field-root
        name="username"
        className="[&::part(field)]:box-border [&::part(field)]:border-0 [&::part(field)]:border-solid [&::part(field)]:flex [&::part(field)]:flex-col [&::part(field)]:items-start [&::part(field)]:gap-1"
      >
        <fs-field-label className="text-sm font-bold text-neutral-950 dark:text-white">Username</fs-field-label>
        <fs-field-control
          type="text"
          autoComplete="username"
          required
          defaultValue="admin"
          placeholder="e.g. alice132"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
        <fs-field-error className="text-sm text-red-700 dark:text-red-400" />
      </fs-field-root>
      <fs-button
        type="submit"
        disabled={loading}
        focusableWhenDisabled
        className="[&::part(control)]:p-0 [&::part(control)]:[font-family:inherit] [&::part(control)]:flex [&::part(control)]:h-8 [&::part(control)]:items-center [&::part(control)]:justify-center [&::part(control)]:gap-2 [&::part(control)]:rounded-none [&::part(control)]:border [&::part(control)]:border-neutral-950 [&::part(control)]:bg-white [&::part(control)]:px-3 [&::part(control)]:text-sm [&::part(control)]:leading-none [&::part(control)]:whitespace-nowrap [&::part(control)]:font-normal [&::part(control)]:text-neutral-950 [&::part(control)]:select-none not-data-disabled:[&::part(control)]:hover:bg-neutral-100 not-data-disabled:[&::part(control)]:active:bg-neutral-200 [&::part(control):focus-visible]:outline-2 [&::part(control):focus-visible]:-outline-offset-1 [&::part(control):focus-visible]:outline-neutral-950 dark:[&::part(control):focus-visible]:outline-white data-disabled:[&::part(control)]:border-neutral-500 data-disabled:[&::part(control)]:text-neutral-500 disabled:[&::part(control)]:border-neutral-500 disabled:[&::part(control)]:text-neutral-500 dark:[&::part(control)]:border-white dark:[&::part(control)]:bg-neutral-950 dark:[&::part(control)]:text-white dark:not-data-disabled:[&::part(control)]:hover:bg-neutral-800 dark:not-data-disabled:[&::part(control)]:active:bg-neutral-700 dark:data-disabled:[&::part(control)]:border-neutral-400 dark:data-disabled:[&::part(control)]:text-neutral-400"
      >
        Submit
      </fs-button>
    </fs-form>
  );
}
```

### CSS Modules

This example shows how to implement the component using CSS Modules.

```css
/* index.module.css */
.Form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 100%;
  max-width: 16rem;
}

.Field {
  display: flex;
  flex-direction: column;
  align-items: start;
  gap: 0.25rem;
}

.Label {
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 100%;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Error {
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: oklch(50.5% 0.213 27.518deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.4% 0.191 22.216deg);
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  outline: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
```

```tsx
/* docs-comparison.tsx */
import { StencilComparison } from 'docs/src/components/StencilComparison';
import ReactForm from './react-form';
import StencilForm from './stencil-form';
import { StencilFormView } from './stencil-form-view';

export default function FormActionComparison() {
  return <StencilComparison component="form-action-css-modules" react={<ReactForm />} stencil={<StencilFormView />} clientStencil={<StencilForm />} />;
}
```

```tsx
/* react-form.tsx */
'use client';
import * as React from 'react';
import { Field } from '@base-ui/react/field';
import { Form } from '@base-ui/react/form';
import { Button } from '@base-ui/react/button';
import styles from './index.module.css';

interface FormState {
  serverErrors?: Form.Props['errors'];
}

export default function ActionStateForm() {
  const [state, formAction, loading] = React.useActionState<FormState, FormData>(submitForm, {});

  return (
    <Form errors={state.serverErrors} action={formAction} className={styles.Form}>
      <Field.Root name="username" className={styles.Field}>
        <Field.Label className={styles.Label}>Username</Field.Label>
        <Field.Control type="text" autoComplete="username" required defaultValue="admin" placeholder="e.g. alice132" className={styles.Input} />
        <Field.Error className={styles.Error} />
      </Field.Root>
      <Button type="submit" disabled={loading} focusableWhenDisabled className={styles.Button}>
        Submit
      </Button>
    </Form>
  );
}

// Mark this as a Server Function with `'use server'` in a supporting framework like Next.js
async function submitForm(_previousState: FormState, formData: FormData) {
  // Mimic a server response
  await new Promise(resolve => {
    setTimeout(resolve, 1000);
  });

  try {
    const username = formData.get('username') as string | null;

    if (username === 'admin') {
      return { success: false, serverErrors: { username: "'admin' is reserved for system use" } };
    }

    // 50% chance the username is taken
    const success = Math.random() > 0.5;

    if (!success) {
      return {
        serverErrors: { username: `${username} is unavailable` },
      };
    }
  } catch {
    return { serverErrors: { username: 'A server error has occurred' } };
  }

  return {};
}
```

```tsx
/* stencil-form.tsx */
'use client';
import * as React from 'react';
import type { FormErrors } from 'finesoft-components/define-custom-elements';
import { StencilFormView } from './stencil-form-view';

interface FormState {
  serverErrors?: FormErrors;
}

export default function StencilForm() {
  const [state, formAction, loading] = React.useActionState<FormState, FormData>(submitForm, {});

  return <StencilFormView errors={state.serverErrors} action={formData => React.startTransition(() => formAction(formData))} loading={loading} />;
}

// Mark this as a Server Function with `'use server'` in a supporting framework like Next.js
async function submitForm(_previousState: FormState, formData: FormData) {
  // Mimic a server response
  await new Promise(resolve => {
    setTimeout(resolve, 1000);
  });

  try {
    const username = formData.get('username') as string | null;

    if (username === 'admin') {
      return { success: false, serverErrors: { username: "'admin' is reserved for system use" } };
    }

    // 50% chance the username is taken
    const success = Math.random() > 0.5;

    if (!success) {
      return {
        serverErrors: { username: `${username} is unavailable` },
      };
    }
  } catch {
    return { serverErrors: { username: 'A server error has occurred' } };
  }

  return {};
}
```

```tsx
/* stencil-form-view.tsx */
import type { FormAction, FormErrors } from 'finesoft-components/define-custom-elements';

interface StencilFormViewProps {
  action?: FormAction;
  errors?: FormErrors;
  loading?: boolean;
}

export function StencilFormView({ action, errors, loading = false }: StencilFormViewProps) {
  return (
    <fs-form errors={errors} action={action}>
      <fs-field-root name="username">
        <fs-field-label>Username</fs-field-label>
        <fs-field-control type="text" autoComplete="username" required defaultValue="admin" placeholder="e.g. alice132" />
        <fs-field-error />
      </fs-field-root>
      <fs-button type="submit" disabled={loading} focusableWhenDisabled>
        Submit
      </fs-button>
    </fs-form>
  );
}
```

### Submit form values as a JavaScript object

You can use `onFormSubmit` instead of the native `onSubmit` to access form values as a JavaScript object. This is useful when you need to transform the values before submission, or integrate with 3rd party APIs.

```tsx title="Submission using onFormSubmit"
<Form
  onFormSubmit={async (formValues: { id: string; quantity: number }) => {
    const payload = {
      product_id: formValues.id,
      order_quantity: formValues.quantity,
    };

    const response = await fetch('https://api.example.com', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(payload),
    });
  }}
/>
```

When used, `preventDefault` is called on the native submit event.

### Using with Zod

When parsing the schema using `schema.safeParse()`, the `z.flattenError(result.error).fieldErrors` data can be used to map the errors to each field's `name`.

## Demo

### Tailwind

This example shows how to implement the component using Tailwind CSS.

```tsx
/* docs-comparison.tsx */
import { StencilComparison } from 'docs/src/components/StencilComparison';
import ReactForm from './react-form';
import StencilForm from './stencil-form';
import { StencilFormView } from './stencil-form-view';

export default function FormComparison() {
  return <StencilComparison component="form-zod-tailwind" react={<ReactForm />} stencil={<StencilFormView />} clientStencil={<StencilForm />} />;
}
```

```tsx
/* react-form.tsx */
'use client';
import * as React from 'react';
import { z } from 'zod';
import { Field } from '@base-ui/react/field';
import { Form } from '@base-ui/react/form';
import { Button } from '@base-ui/react/button';

const schema = z.object({
  name: z.string().min(1, 'Name is required'),
  age: z.coerce.number('Age must be a number').positive('Age must be a positive number'),
});

async function submitForm(formValues: Form.Values) {
  const result = schema.safeParse(formValues);

  if (!result.success) {
    return {
      errors: z.flattenError(result.error).fieldErrors,
    };
  }

  return {
    errors: {},
  };
}

export default function Page() {
  const [errors, setErrors] = React.useState({});

  return (
    <Form
      className="flex w-full max-w-64 flex-col gap-4"
      errors={errors}
      onFormSubmit={async formValues => {
        const response = await submitForm(formValues);
        setErrors(response.errors);
      }}
    >
      <Field.Root name="name" className="flex flex-col items-start gap-1">
        <Field.Label className="text-sm font-bold text-neutral-950 dark:text-white">Name</Field.Label>
        <Field.Control
          placeholder="Enter name"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
        <Field.Error className="text-sm text-red-700 dark:text-red-400" />
      </Field.Root>
      <Field.Root name="age" className="flex flex-col items-start gap-1">
        <Field.Label className="text-sm font-bold text-neutral-950 dark:text-white">Age</Field.Label>
        <Field.Control
          placeholder="Enter age"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
        <Field.Error className="text-sm text-red-700 dark:text-red-400" />
      </Field.Root>
      <Button
        type="submit"
        className="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-data-disabled:bg-neutral-100 active:not-data-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-data-disabled:bg-neutral-800 dark:active:not-data-disabled:bg-neutral-700 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400"
      >
        Submit
      </Button>
    </Form>
  );
}
```

```tsx
/* stencil-form.tsx */
'use client';
import * as React from 'react';
import { z } from 'zod';
import type { FormValues } from 'finesoft-components/define-custom-elements';
import { StencilFormView } from './stencil-form-view';

const schema = z.object({
  name: z.string().min(1, 'Name is required'),
  age: z.coerce.number('Age must be a number').positive('Age must be a positive number'),
});

async function submitForm(formValues: FormValues) {
  const result = schema.safeParse(formValues);

  if (!result.success) {
    return {
      errors: z.flattenError(result.error).fieldErrors,
    };
  }

  return {
    errors: {},
  };
}

export default function StencilForm() {
  const [errors, setErrors] = React.useState({});

  return (
    <StencilFormView
      errors={errors}
      formSubmit={async formValues => {
        const response = await submitForm(formValues);
        setErrors(response.errors);
      }}
    />
  );
}
```

```tsx
/* stencil-form-view.tsx */
import type { FormErrors, FormSubmitHandler } from 'finesoft-components/define-custom-elements';

interface StencilFormViewProps {
  errors?: FormErrors;
  formSubmit?: FormSubmitHandler;
}

export function StencilFormView({ errors, formSubmit }: StencilFormViewProps) {
  return (
    <fs-form className="flex w-full max-w-64 flex-col gap-4" errors={errors} formSubmit={formSubmit}>
      <fs-field-root
        name="name"
        className="[&::part(field)]:box-border [&::part(field)]:border-0 [&::part(field)]:border-solid [&::part(field)]:flex [&::part(field)]:flex-col [&::part(field)]:items-start [&::part(field)]:gap-1"
      >
        <fs-field-label className="text-sm font-bold text-neutral-950 dark:text-white">Name</fs-field-label>
        <fs-field-control
          placeholder="Enter name"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
        <fs-field-error className="text-sm text-red-700 dark:text-red-400" />
      </fs-field-root>
      <fs-field-root
        name="age"
        className="[&::part(field)]:box-border [&::part(field)]:border-0 [&::part(field)]:border-solid [&::part(field)]:flex [&::part(field)]:flex-col [&::part(field)]:items-start [&::part(field)]:gap-1"
      >
        <fs-field-label className="text-sm font-bold text-neutral-950 dark:text-white">Age</fs-field-label>
        <fs-field-control
          placeholder="Enter age"
          className="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
        />
        <fs-field-error className="text-sm text-red-700 dark:text-red-400" />
      </fs-field-root>
      <fs-button
        type="submit"
        className="[&::part(control)]:p-0 [&::part(control)]:[font-family:inherit] [&::part(control)]:flex [&::part(control)]:h-8 [&::part(control)]:items-center [&::part(control)]:justify-center [&::part(control)]:gap-2 [&::part(control)]:rounded-none [&::part(control)]:border [&::part(control)]:border-neutral-950 [&::part(control)]:bg-white [&::part(control)]:px-3 [&::part(control)]:text-sm [&::part(control)]:leading-none [&::part(control)]:whitespace-nowrap [&::part(control)]:font-normal [&::part(control)]:text-neutral-950 [&::part(control)]:select-none not-data-disabled:[&::part(control)]:hover:bg-neutral-100 not-data-disabled:[&::part(control)]:active:bg-neutral-200 [&::part(control):focus-visible]:outline-2 [&::part(control):focus-visible]:-outline-offset-1 [&::part(control):focus-visible]:outline-neutral-950 dark:[&::part(control):focus-visible]:outline-white data-disabled:[&::part(control)]:border-neutral-500 data-disabled:[&::part(control)]:text-neutral-500 disabled:[&::part(control)]:border-neutral-500 disabled:[&::part(control)]:text-neutral-500 dark:[&::part(control)]:border-white dark:[&::part(control)]:bg-neutral-950 dark:[&::part(control)]:text-white dark:not-data-disabled:[&::part(control)]:hover:bg-neutral-800 dark:not-data-disabled:[&::part(control)]:active:bg-neutral-700 dark:data-disabled:[&::part(control)]:border-neutral-400 dark:data-disabled:[&::part(control)]:text-neutral-400"
      >
        Submit
      </fs-button>
    </fs-form>
  );
}
```

### CSS Modules

This example shows how to implement the component using CSS Modules.

```css
/* index.module.css */
.Form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 100%;
  max-width: 16rem;
}

.Field {
  display: flex;
  flex-direction: column;
  align-items: start;
  gap: 0.25rem;
}

.Label {
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 100%;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Error {
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: oklch(50.5% 0.213 27.518deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.4% 0.191 22.216deg);
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  outline: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
```

```tsx
/* docs-comparison.tsx */
import { StencilComparison } from 'docs/src/components/StencilComparison';
import ReactForm from './react-form';
import StencilForm from './stencil-form';
import { StencilFormView } from './stencil-form-view';

export default function FormComparison() {
  return <StencilComparison component="form-zod-css-modules" react={<ReactForm />} stencil={<StencilFormView />} clientStencil={<StencilForm />} />;
}
```

```tsx
/* react-form.tsx */
'use client';
import * as React from 'react';
import { z } from 'zod';
import { Field } from '@base-ui/react/field';
import { Form } from '@base-ui/react/form';
import { Button } from '@base-ui/react/button';
import styles from './index.module.css';

const schema = z.object({
  name: z.string().min(1, 'Name is required'),
  age: z.coerce.number('Age must be a number').positive('Age must be a positive number'),
});

async function submitForm(formValues: Form.Values) {
  const result = schema.safeParse(formValues);

  if (!result.success) {
    return {
      errors: z.flattenError(result.error).fieldErrors,
    };
  }

  return {
    errors: {},
  };
}

export default function Page() {
  const [errors, setErrors] = React.useState({});

  return (
    <Form
      className={styles.Form}
      errors={errors}
      onFormSubmit={async formValues => {
        const response = await submitForm(formValues);
        setErrors(response.errors);
      }}
    >
      <Field.Root name="name" className={styles.Field}>
        <Field.Label className={styles.Label}>Name</Field.Label>
        <Field.Control placeholder="Enter name" className={styles.Input} />
        <Field.Error className={styles.Error} />
      </Field.Root>
      <Field.Root name="age" className={styles.Field}>
        <Field.Label className={styles.Label}>Age</Field.Label>
        <Field.Control placeholder="Enter age" className={styles.Input} />
        <Field.Error className={styles.Error} />
      </Field.Root>
      <Button type="submit" className={styles.Button}>
        Submit
      </Button>
    </Form>
  );
}
```

```tsx
/* stencil-form.tsx */
'use client';
import * as React from 'react';
import { z } from 'zod';
import type { FormValues } from 'finesoft-components/define-custom-elements';
import { StencilFormView } from './stencil-form-view';

const schema = z.object({
  name: z.string().min(1, 'Name is required'),
  age: z.coerce.number('Age must be a number').positive('Age must be a positive number'),
});

async function submitForm(formValues: FormValues) {
  const result = schema.safeParse(formValues);

  if (!result.success) {
    return {
      errors: z.flattenError(result.error).fieldErrors,
    };
  }

  return {
    errors: {},
  };
}

export default function StencilForm() {
  const [errors, setErrors] = React.useState({});

  return (
    <StencilFormView
      errors={errors}
      formSubmit={async formValues => {
        const response = await submitForm(formValues);
        setErrors(response.errors);
      }}
    />
  );
}
```

```tsx
/* stencil-form-view.tsx */
import type { FormErrors, FormSubmitHandler } from 'finesoft-components/define-custom-elements';

interface StencilFormViewProps {
  errors?: FormErrors;
  formSubmit?: FormSubmitHandler;
}

export function StencilFormView({ errors, formSubmit }: StencilFormViewProps) {
  return (
    <fs-form errors={errors} formSubmit={formSubmit}>
      <fs-field-root name="name">
        <fs-field-label>Name</fs-field-label>
        <fs-field-control placeholder="Enter name" />
        <fs-field-error />
      </fs-field-root>
      <fs-field-root name="age">
        <fs-field-label>Age</fs-field-label>
        <fs-field-control placeholder="Enter age" />
        <fs-field-error />
      </fs-field-root>
      <fs-button type="submit">Submit</fs-button>
    </fs-form>
  );
}
```

## API reference

### Form

A native form element with consolidated error handling.
Renders a `<form>` element.

**Form Props:**

| Prop           | Type                                                                                                                                                | Default      | Description                                                                                                                                                                                                                                                                                                                                                |
| :------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------- | :----------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| errors         | `Errors`                                                                                                                                            | -            | Validation errors returned externally, typically after submission by a server or a form action.&#xA;This should be an object where keys correspond to the `name` attribute on `<Field.Root>`,&#xA;and values correspond to error(s) related to that field.                                                                                                 |
| actionsRef     | `React.RefObject<Form.Actions \| null>`                                                                                                             | -            | A ref to imperative actions. `validate`: Validates all fields when called. Optionally pass a field name to validate a single field.                                                                                                                                                                                                                        |
| onFormSubmit   | `((formValues: Record<string, any>, eventDetails: Form.SubmitEventDetails) => void)`                                                                | -            | Event handler called when the form is submitted.&#xA;`preventDefault()` is called on the native submit event when used.                                                                                                                                                                                                                                    |
| validationMode | `Form.ValidationMode`                                                                                                                               | `'onSubmit'` | Determines when the form should be validated.&#xA;The `validationMode` prop on `<Field.Root>` takes precedence over this. `onSubmit` (default): validates the field when the form is submitted, afterwards fields will re-validate on change.`onBlur`: validates a field when it loses focus.`onChange`: validates the field on every change to its value. |
| className      | `string \| ((state: Form.State) => string \| undefined)`                                                                                            | -            | CSS class applied to the element, or a function that&#xA;returns a class based on the component's state.                                                                                                                                                                                                                                                   |
| style          | `React.CSSProperties \| ((state: Form.State) => React.CSSProperties \| undefined)`                                                                  | -            | Style applied to the element, or a function that&#xA;returns a style object based on the component's state.                                                                                                                                                                                                                                                |
| render         | `ReactElement \| ((props: React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, state: Form.State) => ReactElement)` | -            | Allows you to replace the component's HTML element&#xA;with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render.                                                                                                                                                              |

**`actionsRef` Prop Example:**

```tsx
// validate all fields
actionsRef.current?.validate();

// validate one field
actionsRef.current?.validate('email');
```

### Form.Props

Re-export of [Form](/react/components/form.md) props.

### Form.State

```typescript
type FormState = {};
```

### Form.Actions

```typescript
type FormActions = { validate: (fieldName?: string) => void };
```

### Form.SubmitEventDetails

```typescript
type FormSubmitEventDetails = {
  /** The reason for the event. */
  reason: 'none';
  /** The native event associated with the custom event. */
  event: Event;
};
```

### Form.SubmitEventReason

```typescript
type FormSubmitEventReason = 'none';
```

### Form.ValidationMode

```typescript
type FormValidationMode = 'onSubmit' | 'onBlur' | 'onChange';
```

### Form.Values

```typescript
type FormValues = Record<string, any>;
```

## Canonical Types

Maps `Canonical`: `Alias` — Use Canonical when its namespace is already imported; otherwise use Alias.

- `Form.Props`: `FormProps`
- `Form.State`: `FormState`
- `Form.Actions`: `FormActions`
- `Form.ValidationMode`: `FormValidationMode`
- `Form.SubmitEventReason`: `FormSubmitEventReason`
- `Form.SubmitEventDetails`: `FormSubmitEventDetails`
