组件

Form

react-hook-form wired to the drawn controls, exactly like shadcn's Form. FormControl gives the control its id, aria-describedby and aria-invalid, so an Input, Select, Checkbox or Switch turns red pencil when the field has an error, its label follows, and FormMessage writes the message under it with a wavy red underline.

安装

terminal
npx shadcn@latest add @wobko/form

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Default

react-hook-form with a zod schema. Press Save with the fields empty: the labels and boxes turn red pencil and each message is written under its field with a wavy underline. Fix one and its message goes on the next change.

Every control

FormControl hands its id, aria-describedby and aria-invalid to whatever it wraps, so a Select, a Radio Group, a Checkbox, a Switch and a Slider go red the same way an Input does. A slider is not a labelable element: its FormLabel gets an id and the Slider points at it with aria-labelledby.

Server errors

An error does not have to come from the schema: after a pretend request `setError` puts "already taken" on the field, and the same label, box and underline appear. Change the text and it clears itself.

React Hook Form: bug report

shadcn's `form-rhf-demo`, the Field way to wire a form: `Controller` hands `field` and `fieldState` to a render prop, `data-invalid` on the Field and `aria-invalid` on the control turn it red, and `FieldError` prints the zod message. Submit it empty, then fill it in.

React Hook Form: input

shadcn's `form-rhf-input`: one text field with a zod length and pattern rule. The error is announced as an alert and written under the field in red pencil.

React Hook Form: select

shadcn's `form-rhf-select`: a Radix Select is not a native input, so `name`, `value` and `onValueChange` are passed by hand and `aria-invalid` goes on the trigger. "Auto" is a value the schema refuses.

React Hook Form: checkbox

shadcn's `form-rhf-checkbox`: a single boolean box (here disabled and ticked) and a group that collects an array. Untick both notification types and submit to see the group's own error.

React Hook Form: switch

shadcn's `form-rhf-switch`: a boolean switch that the schema insists on. Submit with it off and the switch turns red pencil with its message in the text column.

React Hook Form: textarea

shadcn's `form-rhf-textarea`: a ruled textarea with a length rule on both ends. Too short and too long each get their own sentence.

React Hook Form: radio group

shadcn's `form-rhf-radiogroup`: choice cards (a FieldLabel around each Field) in a controlled RadioGroup. `aria-invalid` on the group and on each item paints the whole set red when nothing is chosen.

React Hook Form: array

shadcn's `form-rhf-array`: `useFieldArray` for a list of emails, each an InputGroup with a remove button inside its box. Add up to five; the array's own min and max errors are printed under the list.

React Hook Form: every control

shadcn's `form-rhf-complex`: a radio card set, a select, a checkbox group and a switch in one form, four kinds of state and four `Controller`s. The schema wants a billing period and at least one add-on.

React Hook Form: password

shadcn's `form-rhf-password`: `useWatch` feeds a strength meter and a live checklist while the schema guards the submit. The bar is a marker pass that changes ink as the password improves; a met rule is a pencilled tick, an open one a cross, and each says so in words for a screen reader.

API 参考

FormMessage

The field's error (or your own children), drawn with a wavy red pencil underline. Renders nothing when there is nothing to say.

属性类型默认值说明
seedstring | number-Pins the hand of the underline. Default: from useId().

无障碍

  • FormLabel is a real <label for>, FormControl puts the same id on the control, and aria-describedby points at the description and (while invalid) the message; aria-invalid is set for you.
  • FormMessage is a <p> that appears next to the control with its id in aria-describedby, so a screen reader reads it with the control when focus arrives. Wrap it in an element with role="alert" (or use Field's FieldError) if you want it announced the moment it appears.
  • Not every control can be named by FormLabel's `for`: a Slider (role=slider on a span) or a Rating (a radiogroup) needs `aria-labelledby` pointing at an `id` you give the FormLabel, or an `aria-label`. FormControl's id, aria-describedby and aria-invalid do reach a Slider's thumb, a Rating's group and a RadioGroup's group (whose radios turn red).
  • Errors do not rely on color alone: the message text, the wavy underline and the control's own drawn state (red box, a bang on text fields) all say it.
  • Validation timing is react-hook-form's (`mode`: onSubmit by default, onChange, onBlur ...); the drawn components do not re-render their ink when it changes, only their state layers.
  • Windows high contrast: the ink is replaced by real borders, and the message gets a wavy text underline.

源码

CLI 会复制进你项目的文件。

正在加载源码