Skip to content
MIZU.

AsyncForm

A form that follows the save promise and preserves input when a save fails.

View .md

Live — this one is real

Usage

tsx
"use client";

import { AsyncForm, TextField } from "mizu-ui";

export function Example() {
  return (
    <AsyncForm
      onSubmit={async (data, signal) => {
        const response = await fetch("/api/settings", {
          method: "POST",
          body: data,
          signal,
        });
        if (!response.ok) throw new Error("Could not save settings.");
      }}
    >
      <TextField label="Display name" name="name" required />
      <button type="submit" className="mizu-text-button">
        Save settings
      </button>
    </AsyncForm>
  );
}

It starts with "use client" because it keeps state or passes functions to components, so it pastes into a Next.js page as it is. Other React setups ignore the directive.

Source & setup

Add this component to your project with the shadcn CLI. The command copies its source into your configured UI directory, with base.css, Mizu’s tokens, and the stylesheets that hold its rules. Each rule lives in one stylesheet, so components you add together never repeat CSS. In a project without a components.json, run npx shadcn@latest init 0xuser64bit/mizu/preset first: plain init installs shadcn’s theme into your global CSS. Then load the fonts.

sh
npx shadcn@latest add 0xuser64bit/mizu/async-form 0xuser64bit/mizu/text-field

The example also uses TextField, so the command adds it too.

Every component imports from its own path, its name in kebab case. To paste the example, replace its mizu-ui import with:

tsx
import { AsyncForm } from "@/components/ui/mizu/async-form";
import { TextField } from "@/components/ui/mizu/text-field";

For npm imports, import mizu-ui/styles.css once. Fonts are optional via mizu-ui/fonts.css. Override --mizu-* tokens or use className for local styling. Uses native React and CSS; install the package peers for root imports. A Client Component: its source starts with “use client”.

Inspect forms/AsyncForm.tsx

Source is included in the npm package under src/forms/AsyncForm.tsx.

Open full source ↗

Props

PropTypeDefaultDescription
onSubmit(data: FormData, signal: AbortSignal) => Promise<void>—Native validity runs before submission. Signal aborts on unmount.
successMessagestring"Saved."Successful save feedback.

Accessibility

Uses native form validation and disables the fieldset while pending. Errors are live; fields stay intact for correction.

Motion

Pending state follows a real promise, never a simulated timeout.

Works alongside