Examples
Single line field
Multiline field
Error field
Success field
Disabled field
Code
React
A TextField
component renders an input field as well as supporting UI elements like a label, error message, and hint text.
Name | Type | Default | Description |
---|---|---|---|
ariaLabel | string | Apply an aria-label to the text field to provide additional
context to assistive devices. | |
defaultValue | TextFieldDefaultValue | Sets the initial value. Use this for an uncontrolled component; otherwise,
use the value property. | |
disabled | boolean | ||
fieldClassName | string | ||
autoFocus | boolean | ||
id | string | A unique id to be used on the text field. | |
inputRef | (...args: any[]) => any | Access a reference to the input or textarea element | |
requirementLabel | ReactNode | Text showing the requirement ("Required", "Optional", etc.). See Required and Optional Fields. | |
inversed | boolean | Applies the "inverse" UI theme | |
labelMask | (rawInput: string, valueOnly?: boolean) => string | Applies date format masking to the input value entered
and renders to a text field above the input.
Passing true to valueOnly will return just the
formatted value entered. | |
mask | TextFieldMask | Apply formatting to the field that's unique to the value you expect to be entered. Depending on the mask, the field's appearance and functionality may be affected. | |
multiline | boolean | Whether or not the text field is a multiline text field | |
name required | string | ||
numeric | boolean | Sets inputMode , type , and pattern to improve accessibility and consistency for number fields. Use this prop instead of type="number" , see here for more information. | |
onBlur | (...args: any[]) => any | ||
onChange | (...args: any[]) => any | ||
rows | TextFieldRows | Optionally specify the number of visible text lines for the field. Only applicable if this is a multiline field. | |
size | TextFieldSize | Set the max-width of the input either to 'small' or 'medium' . | |
type | string | text | HTML input type attribute. If you are using type=number please use the numeric prop instead. |
value | TextFieldValue | Sets the input's value . Use this in combination with onChange
for a controlled component; otherwise, set defaultValue . | |
className | string | Additional classes to be added to the root element. | |
label required | ReactNode | Label for the field. | |
errorMessage | ReactNode | Enable the error state by providing an error message. | |
errorMessageClassName | string | Additional classes to be added to the error message | |
errorId | string | The ID of the error message applied to this field. | |
hint | ReactNode | Additional hint text to display | |
textClassName | string | Additional classes to be added to the label text. | |
errorPlacement | "top" | "bottom" | Location of the error message relative to the field input | |
labelClassName | string | Additional classes to be added to the field label | |
labelId | string | A unique id to be used on the field label. If one isn't provided, a unique ID
will be generated. |
Styles
The following CSS variables can be overridden to customize Input/Form components:
Form components
This component also makes use of form field styles, which can be customized by the following variables:
Guidance
When to use
- If you can’t reasonably predict a user’s answer to a prompt and there might be wide variability in users’ answers.
- When using another type of input will make answering more difficult. For example, birthdays and other known dates are easier to type in than they are to select from a calendar picker.
- When users want to paste in a response.
When to consider alternatives
- When users are choosing from a specific set of options. Consider checkboxes, radio buttons, or a dropdown in these cases.
Usage
- Don't use placeholder text in form fields. Use hint text instead, if you need to provide contextual information. Placeholder text disappears after a user types a value, therefore users will no longer have that text available when they need to review their entries. People who have cognitive or visual disabilities have additional problems with placeholder text.
- The length of the text field provides a hint to users as to how much text to write. Do not require users to write paragraphs of text into a single-line
input
box; use atextarea
instead. - Text fields are among the easiest type of input for desktop users but are more difficult for mobile users.
- Only show error messages or styling after a user has interacted with a particular field.
View the "Forms" guidelines for additional guidance and best practices.
Accessibility
- Group each set of thematically related controls in a
fieldset
element. Use thelegend
element to offer a label within eachfieldset
. Thefieldset
andlegend
elements make it easier for screen reader users to navigate the form. - Keep your form blocks in a vertical pattern. This approach is ideal, from an accessibility standpoint, because of limited vision that makes it hard to scan from left to right.
Related patterns
Learn more
- Form Guidelines
- "Placeholders in Form Fields Are Harmful"
- Asking for a date of birth
- GOV.UK text boxes discussion
- Four steps for choosing form elements on the Web (PDF)
Component maturity
For more information about how we tested and validated our work for each checklist item, read our component maturity documentation.