Field Group components are used as a wrapper component to create a consistent look and feel for form inputs and to provide a consistent way to handle error messages and helper text. It is suggest that this component be baked into your normal form input component.
Instruction content goes here lorem ipsum dolor sit.
This component is based on the <div> element and can take in any additional HTML attributes. The following are the custom props that can be passed to the component.
Prop
Type
Default
name *
Unique name of the input field. Should follow a kebab-case naming convention (e.g. "first-name")
string
—
label *
Visual or assistive label for the input field
string
—
labelHidden
Whether to visually hide the label or not. When hidden, the label will still be available to screen readers
boolean
false
sublabel
Sub-label content for the input field
string
—
instructions
Instructions for the input field that will be placed below the input
string
—
required
Whether the input field is required or not. If set to "long", the input field will be marked as required and include explicit "required" text.
boolean | "long"
false
errorMessages
Error messages to display below the input field
string[]
—
Accessibility
The <FieldGroup> component includes some accessibility features to help ensure that the component is usable and understandable by all users. The following are some of the key accessibility features that are included in the component and worth considering:
The label element is associated with the input field by using the for attribute and includes any sublabel text.
The label and instructions elements are associated with the input field by using the aria-labelledby attribute. This is an important consideration specifically for the instructions so that they are read out by screen readers when the field is focused, rather than after the input field.
The errorMessages are displayed below the input field and are marked up as a list of error messages.
The labelHidden prop visually hides the label but still ensures it is available to screen readers.
The required prop marks the input field as required and can include explicit “required” text if the value is set to "long".
Examples
Required
Use the required prop to mark a field as required. You can also use the required prop to mark a field as required and include explicit “required” text by passing the value "long".
It’s recommended that an element like this is made as a reusable component. In this case, we have put together the following Astro component to serve as a blueprint for how you could possibly set up a field group component, and what properties to consider.