Skip to content

Forms with React Hook Form and Zod

A form recipe for React with react-hook-form and zod: SheenUI's Field wires labels and errors, every control forwards ref and onBlur, and the submit button shows the async state.

Files to copy

14 folders · 21 files · 4 packages
From theme/src/
  • components/alert/
  • components/button/
  • components/checkbox/
  • components/field/
  • components/input/
  • components/number-field/
  • components/select/
  • components/drawer/parts/sheet.tsx
  • components/spinner/
  • components/status-icons/
  • hooks/{use-controllable.ts, use-latest.ts, use-magnetic.ts, use-media-query.ts, use-preferences.ts, use-virtual-window.ts}
  • motion/{gate.ts, tokens.ts}
  • lib/{cn.ts, field-control-shell.tsx, field-size.ts, form.ts, glide-highlight.tsx, group-segments.ts, motion-props.ts, page-size.ts, popup-a11y.ts, popup-motion.ts, state-light.tsx, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/alert.jsonRegistry items: alert, button, checkbox, field, input, number-field, select. The command installs the first; swap the name in the URL for any other. The styles need the Tailwind v4 glass UI setup.
The examples also use
    npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json https://lokendra-builds.github.io/sheenui/r/checkbox.json https://lokendra-builds.github.io/sheenui/r/field.json https://lokendra-builds.github.io/sheenui/r/input.json https://lokendra-builds.github.io/sheenui/r/number-field.json https://lokendra-builds.github.io/sheenui/r/select.json
    Packages
    pnpm add @base-ui/react clsx motion tailwind-merge

    Install

    The form libraries are your app's dependencies, not SheenUI's. The examples were tested with react-hook-form 7.91, zod 4.6 and @hookform/resolvers 7.0:

    pnpm add react-hook-form zod @hookform/resolvers

    The controls come from the registry; every item below installs with npx shadcn@latest add and its docs page lists the exact files: Field, Input, Select, Checkbox, NumberField, Button, Alert. Each control page has the single-field version of this pattern; the Field page shows it with every control.

    The second example (every control) also uses these. One command adds everything both examples import:

    npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/alert.json https://lokendra-builds.github.io/sheenui/r/button.json https://lokendra-builds.github.io/sheenui/r/checkbox.json https://lokendra-builds.github.io/sheenui/r/color-picker.json https://lokendra-builds.github.io/sheenui/r/combobox.json https://lokendra-builds.github.io/sheenui/r/date-picker.json https://lokendra-builds.github.io/sheenui/r/field.json https://lokendra-builds.github.io/sheenui/r/input.json https://lokendra-builds.github.io/sheenui/r/number-field.json https://lokendra-builds.github.io/sheenui/r/otp-input.json https://lokendra-builds.github.io/sheenui/r/radio-group.json https://lokendra-builds.github.io/sheenui/r/rating.json https://lokendra-builds.github.io/sheenui/r/segmented.json https://lokendra-builds.github.io/sheenui/r/select.json https://lokendra-builds.github.io/sheenui/r/slider.json https://lokendra-builds.github.io/sheenui/r/switch.json https://lokendra-builds.github.io/sheenui/r/tags-input.json

    Sign-up form: zod, Field and an async submit

    Text inputs use register() (they forward ref, onChange, onBlur and name); everything else goes through Controller. Submit with errors to see focus move to the first invalid field; use an @example.com email for a server error.

    Try anything@example.com to see a server error.

    Every control with Controller

    The pattern never changes: value (or checked), the change handler, onBlur, ref and name. The values update live on the right.

    Volume
    40
    Rate it
    • react
    Billing

    Form values (watch)

    {
      "volume": 40,
      "notify": true,
      "stars": 4,
      "birthday": null,
      "country": null,
      "tags": [
        "react"
      ],
      "view": "week",
      "code": "",
      "billing": "monthly",
      "brand": "#7c5cff"
    }

    Frequently asked questions

    Keyboard

    KeysAction
    TabMoves through the fields; after a failed submit focus goes to the first invalid control (react-hook-form calls its ref.focus()).
    EnterSubmits from any text field.

    Accessibility

    • Field links each label, description and error to its control (for, aria-describedby, aria-invalid), and the error is announced as an alert when it appears.
    • Every SheenUI form control forwards ref, onBlur, name and the aria props: ref lets react-hook-form focus the control after a failed validation, onBlur marks it touched.
    • The submit button shows loading through aria-busy and ignores clicks while it runs, so a second submit cannot start; focus stays on it.
    • Server errors are put on the field they belong to with setError, so they are announced and linked like client-side errors.

    By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .