Text input components are used to collect text-based data from the user. It should be used for single-line text input fields such as text, email, password, number, tel, and url fields.
This component is based on the <input> element and can take in any additional HTML attributes through the use of the inputProps property. 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
—
placeholder
Placeholder text for the input field
string
—
disabled
Whether the input field is disabled or not
boolean
false
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.
The <TextInput> component is designed with accessibility in mind and extends much of the accessibility features from the <Field Group> component. The following are some of the field specific accessibility features that are included in the component and worth considering:
The aria-labelledby attribute is used to associate the label and instructions elements with the input field. 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.
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 text input component, and what properties to consider.