FormsReact
A native form element with consolidated error handling.
Rendered by React 19.3.1
100Tests passing in React · 0 failing
3.1 KiBSize
Not measured yetMemory
Not measured yetSpeed
productdevbook and 9 upstream authors11 commits · last
Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<Form />Examples
Action
The form submits through a server function with the action state.
Zod
A Zod schema's field errors are passed to errors by field name.
API reference
4 props across 1 parts, in React syntax. className, style, render are listed once, under Shared props.
FormForm
A native form element with consolidated error handling. Renders a <form>.
| Prop | Type | Default |
|---|---|---|
errorsValidation errors returned externally, typically after submission by a server or a form action. | Errors | — |
actionsRefA ref to imperative actions. | React.RefObject<FormActions | null> | — |
onFormSubmitEvent handler called when the form is submitted. | ((formValues: FormValues, eventDetails: { reason: "none"; event: Event; }) => void) | — |
validationModeDetermines when the form should be validated. | FormValidationMode | 'onSubmit' |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, State> |
Keyboard and accessibility
Keyboard
Form adds no keys; Enter in a field submits it, as a native form does.
Accessibility
| Part | Sets | So that |
|---|---|---|
| Form | renders a <form> | the browser's own submit, reset and autofill work |
| Form | moves focus to the first invalid field when a submit fails | the person lands on the error to fix |
| Form | hands each entry of errors to the field with the same name | a server's error is read with its field, not elsewhere on the page |
Focus order
- Form adds no tab stop; its fields keep document order.
- A submit with errors moves focus to the first invalid field in document order.