Skip to main content

FormField

FormField is a wrapper component that provides labels, helper text, error messages, and accessibility features for form controls.

Open in Storybook

Import

import { FormField } from "@tosui/react";

Basic Usage

Helper Text

Must be at least 8 characters

Required Field

Invalid State

When isInvalid is true, the error message replaces the helper text.

Please enter a valid email address

Disabled State

With Different Controls

FormField works with any form control.

Tell us about yourself
You must accept the terms

Complete Form Example

<VStack gap={4}>
<FormField label="Full Name" isRequired>
<Input placeholder="John Doe" />
</FormField>

<FormField
label="Email"
helperText="We'll never share your email"
isRequired
>
<Input type="email" placeholder="you@example.com" />
</FormField>

<FormField label="Role">
<Select>
<option value="">Select a role</option>
<option value="developer">Developer</option>
<option value="designer">Designer</option>
<option value="manager">Manager</option>
</Select>
</FormField>

<FormField label="Message" helperText="Optional">
<Textarea placeholder="Your message..." />
</FormField>

<Button type="submit" w="100%">Submit</Button>
</VStack>

Props Reference

PropTypeDefaultDescription
labelstring-Label text (required)
helperTextstring-Helper text below the control
errorMessagestring-Error message (shown when isInvalid)
isRequiredbooleanfalseShow required asterisk
isInvalidbooleanfalseShow invalid state
disabledbooleanfalseDisable the control
idstringauto-generatedCustom ID for the field
childrenReactNode-The form control

Accessibility

FormField provides:

  • Proper <label> element linked to the control via htmlFor
  • aria-describedby linking the control to helper/error text
  • aria-invalid attribute when in error state
  • Auto-generated unique IDs when not provided

State Propagation

FormField automatically passes these props to child controls:

  • id - for label association
  • isInvalid - for visual error state
  • disabled - for disabled state
  • aria-describedby - for accessibility
  • aria-invalid - for accessibility

TypeScript

import { FormField, type FormFieldProps } from "@tosui/react";