This component is based on the <select> 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 select. Should follow a kebab-case naming convention (e.g. "first-name")
string
—
label *
Visual or assistive label for the select
string
—
options *
The array of options to display in the dropdown
[{ value: string, label: 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 select
string
—
instructions
Instructions for the select that will be placed below the input
string
—
placeholder
Placeholder text for the select
string
—
disabled
Whether the select is disabled or not
boolean
false
required
Whether the select is required or not. If set to "long", the select will be marked as required and include explicit "required" text.
boolean | "long"
false
inputProps
Additional attributes to pass to the select field
HTMLAttributes<select>
—
errorMessages
Error messages to display below the select
string[]
—
Accessibility
The <Select> 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 select component, and what properties to consider.