Skip to content

Text Input

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.

<TextInput
label="Label"
name="example-1"
placeholder="Placeholder"
class="max-w-xs"
/>

API Reference

This component inherits from the Field Group component.

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 *
string
label *
string
labelHidden
boolean false
sublabel
string
instructions
string
placeholder
string
disabled
boolean false
required
boolean | "long" false
type
"text" | "email" | "password" | "number" | "tel" | "url" "text"
inputProps
HTMLAttributes<input>
errorMessages
string[]

Accessibility

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".

<TextInput
label="Label"
name="example-2"
required
placeholder="Placeholder"
/>
<TextInput
label="Label"
name="example-3"
required="long"
placeholder="Placeholder"
/>

Disabled

Use the disabled prop to disable the input field.

<TextInput
label="Label"
name="example-4"
placeholder="Placeholder"
disabled
/>

With Helper Text

Use the sublabel and instructions props to add helper text to the input field.

Instructions lorem ipsum dolor.
<TextInput
label="Label"
name="example-5"
sublabel="Sublabel lorem ipsum dolor sit amet."
instructions="Instructions lorem ipsum dolor."
placeholder="Placeholder"
/>

With Errors

Use the errorMessages prop to display error messages below the input field.

  • Error message 1
  • Error message 2
<TextInput
label="Label"
name="example-6"
errorMessages={['Error message 1', 'Error message 2']}
placeholder="Placeholder"
/>

With Hidden Label

Use the labelHidden prop to visually hide the label. When hidden, the label will still be available to screen readers.

<TextInput
label="Label"
name="example-7"
labelHidden
placeholder="Placeholder"
/>

Astro Component

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.

TextInput.astro
---
import type { ComponentProps, HTMLAttributes } from 'astro/types'
import FieldGroup from '@/components/forms/FieldGroup.astro'
interface Props extends ComponentProps<typeof FieldGroup> {
/**
Placeholder text for the input field
*/
placeholder?: string
/**
Whether the input field is disabled or not
*/
disabled?: boolean
/**
Type of input field
*/
type?: 'text' | 'email' | 'password' | 'number' | 'tel' | 'url'
/**
Additional props to pass into the input field
*/
inputProps?: HTMLAttributes<'input'>
}
const {
placeholder,
type = 'text',
disabled = false,
inputProps,
...fieldGroupProps
} = Astro.props
---
<FieldGroup data-component="TextInput" {...fieldGroupProps}>
<input
type={type}
id={fieldGroupProps.name}
name={`input-${fieldGroupProps.name}`}
placeholder={placeholder}
disabled={disabled}
required={fieldGroupProps.required === true ||
fieldGroupProps.required === 'long'}
aria-labelledby={`label-${fieldGroupProps.name}${
fieldGroupProps.instructions
? ` instructions-${fieldGroupProps.name}`
: ''
}`}
{...inputProps}
/>
</FieldGroup>