Form API
View componentThe full prop reference for the Form component. A form with declarative validation built on native ValidityState.
Import
import { Form } from '@structyl/styled';Props
Form.Root
The root form element that manages field validation state and provides context to child components
| Prop | Type | Default |
|---|---|---|
| asChild# | booleanRender as a child element instead of native form; pass a single React element child | — |
| onClearServerErrors# | () => voidCallback fired when the form changes, typically used to clear server-side validation errors | — |
| className# | stringCSS class name (styled version applies 'flex flex-col gap-4' by default) | — |
| onInvalid# | (event: Event) => voidHTML form onInvalid handler (preventDefault is composed automatically) | — |
| onChange# | (event: ChangeEvent<HTMLFormElement>) => voidHTML form onChange handler (onClearServerErrors is composed automatically) | — |
Form.Field
Container for a form field that associates label, control, and message elements
| Prop | Type | Default |
|---|---|---|
| asChild# | booleanRender as a child element instead of native div | — |
| name# | stringField name; required and must match the input name | — |
| serverInvalid# | booleanFlag to mark field as invalid from server-side validation (shows error message) | false |
| className# | stringCSS class name (styled version applies 'flex flex-col gap-1.5' by default) | — |
Form.Label
Label element automatically associated with the field's input control via htmlFor
| Prop | Type | Default |
|---|---|---|
| asChild# | booleanRender as a child element instead of native label | — |
| className# | stringCSS class name (styled version applies text-sm font-medium and disabled styling) | — |
Form.Control
Input element that tracks validation state and integrates with form validation
| Prop | Type | Default |
|---|---|---|
| asChild# | booleanRender as a child element instead of native input | — |
| className# | stringCSS class name (styled version applies border, focus, and invalid state styling) | — |
| aria-invalid# | booleanAuto-set based on validity state or serverInvalid flag; reflects HTML5 validation | — |
| aria-describedby# | stringAuto-set to message element ID for accessibility | — |
| type# | stringHTML input type (e.g., 'text', 'email', 'number', 'password') | — |
| required# | booleanHTML5 validation: field is required | — |
| disabled# | booleanDisable the input element | — |
| pattern# | stringHTML5 validation: regex pattern for input value | — |
| minLength# | numberHTML5 validation: minimum string length | — |
| maxLength# | numberHTML5 validation: maximum string length | — |
| min# | string | numberHTML5 validation: minimum value for number/date inputs | — |
| max# | string | numberHTML5 validation: maximum value for number/date inputs | — |
| step# | string | numberHTML5 validation: step value for number/date inputs | — |
| placeholder# | stringPlaceholder text shown when input is empty | — |
| onChange# | (event: ChangeEvent<HTMLInputElement>) => voidChange event handler (validity state is tracked automatically) | — |
| ref# | React.Ref<HTMLInputElement>Forward ref to underlying HTML input element | — |
Form.Message
Validation error message element that displays based on field validity or server errors
| Prop | Type | Default |
|---|---|---|
| asChild# | booleanRender as a child element instead of native span | — |
| match# | 'badInput' | 'patternMismatch' | 'rangeOverflow' | 'rangeUnderflow' | 'stepMismatch' | 'tooLong' | 'tooShort' | 'typeMismatch' | 'valid' | 'valueMissing' | string | ((value: string, formData: FormData) => boolean | Promise<boolean>)Validity matcher: show message when this HTML5 validation state or custom matcher matches | — |
| forceMatch# | booleanForce message to display even if validity doesn't match | — |
| children# | React.ReactNodeMessage content; if omitted, shows default message for HTML5 validity matchers | — |
| className# | stringCSS class name (styled version applies 'text-destructive text-xs') | — |
Form.ValidityState
Render prop component that provides the current field's HTML5 ValidityState for custom UI
| Prop | Type | Default |
|---|---|---|
| children# | (validity: ValidityState | undefined) => React.ReactNodeRender function receiving the field's ValidityState object or undefined | — |
Form.Submit
Submit button element (HTML type='submit' automatically set)
| Prop | Type | Default |
|---|---|---|
| asChild# | booleanRender as a child element instead of native button | — |
| className# | stringCSS class name for styling | — |
| disabled# | booleanDisable the button | — |
| onClick# | (event: React.MouseEvent<HTMLButtonElement>) => voidClick event handler | — |
| type# | 'submit'Button type (automatically set to 'submit', overridable via props) | 'submit' |
Source code
If you didn't find what you need here, read the component implementation .