Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

UIComponentsReact

Form

A form whose fields show their own validation messages and the errors the server returns.

Built on the headless Form.See its API in Docs

Rendered by React 19.3.1

form.tsx

Installation

npx shadcn@4.23.0 add https://hamur.dev/r/react/form.json

It also adds utils, label. The app needs its setup first: Installation.

Usage

Form is the <form>. Give each field a FormField with its name and, for client validation, a validate function or the input's constraint attributes (required, type="email"); render it as a FormItem with render={<FormItem />}. Inside, FormLabel labels the control, FormDescription describes it and FormMessage shows its error while it is invalid. The Input, Textarea, Select and other items are field controls already; FormControl is for a plain element. Pass the server's errors to Form as errors, keyed by field name: each shows in its field's FormMessage until the value changes. Validation, validationMode and onFormSubmit come from the headless Form and Field; there is no form library.

import {
  Form,
  FormItem,
  FormLabel,
  FormControl,
  FormDescription,
  FormMessage,
  FormField,
} from '@/components/ui/form'

Examples

Default

A username checked on submit, and the server's error for a taken username.

Its preview and code are at the top of the page.

Differences from shadcn's: shadcn's form wraps react-hook-form (with zod through @hookform/resolvers); this one is Base UI's Form and Field and installs neither. shadcn's Base UI style has no form item at all, so the names follow its Radix style. Form renders the <form> (shadcn's is react-hook-form's FormProvider, with no element). FormField takes Base UI Field's props (name, validate, validationMode, invalid, disabled) in place of a control and a render({ field, fieldState }) callback; its render is Base UI's, so render={<FormItem />} makes one element of the two. FormControl is Base UI's field control, not a slot around its child. FormMessage shows only while the field is invalid, so children passed to it are an error text, not a hint (use FormDescription), and it renders a <div>. The label marks an error with data-invalid where shadcn's sets data-error. useFormField is not exported: the field's state is on every part as Base UI's data attributes (data-invalid, data-dirty, data-touched, data-filled, data-focused) and reaches a className or render function.

API reference

The styled parts pass every prop to the headless part they wrap and add classes. The props, data attributes and keyboard are on Form's API reference.