Skip to main content

FormField

FormField associates one form control with its label, helper text, error message, and field state.

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, errorMessage replaces helperText.

Please enter a valid email address

Disabled state​

Tosui controls​

Tosui controls consume FormField state from context. Each FormField represents one semantic form control.

Tell us about yourself
You must accept the terms

Native and custom controls​

For a native or custom control, pass a function as children. Spread the received props onto the element that receives focus.

<FormField
label="Email"
helperText="We'll only use this for account notifications"
isRequired
>
{(controlProps) => (
<input
type="email"
placeholder="you@example.com"
{...controlProps}
/>
)}
</FormField>

The function receives id, disabled, required, aria-describedby, and aria-invalid. The function prevents FormField from guessing how a custom component forwards props.

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-Required label text
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
childrenReactElement | (controlProps => ReactElement)-One Tosui control or one control adapter

Accessibility​

FormField provides these relationships:

  • A <label> points to the control ID through htmlFor.
  • aria-describedby points to the active helper text or error message.
  • Existing aria-describedby IDs remain on Tosui controls.
  • aria-invalid is true when isInvalid is true.
  • required and disabled reach the native control.
  • A generated ID links the label and the control when id is absent.

State propagation​

Tosui controls consume these values through context:

  • id
  • isInvalid
  • disabled
  • required
  • aria-describedby
  • aria-invalid

Context crosses component and layout boundaries. A layout wrapper does not receive control-only props.

TypeScript​

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