Input
A field that lets users enter a single value, such as a line of text.
An input takes a single value from users: one line of text by default, and a number, an email address or a file when type says so. It accepts every attribute of the native input element and fills the width of its container. A label names the value that the input asks for, and htmlFor on the label refers to the id of the input, so a click on the label moves the focus into the input.
Usage
Examples
With Placeholder
A short hint helps users enter a value in the expected form, such as the format of a supplier code. Given as placeholder, the hint stays in the empty input and disappears as soon as users type, so it complements the label instead of replacing it.
Default Value
Editing an existing record starts from the value it already holds, such as the name of a product group. With defaultValue, the input begins with that value, and users change only what is different.
Controlled
Other parts of an interface often depend on what users type, such as a list of articles that narrows with every letter of a search. Through value and onChange the application holds the value, so it reacts to each change and can set the value itself, for example to empty the input when users reset their filters.
File
Some workflows need a document from the device users work on, such as a scanned invoice or a photo of damaged goods. With type set to "file", the input lets users choose that document instead of typing a value.
Disabled
A field may not apply until users make an earlier choice, such as a delivery date before they select a delivery method. Once disabled is set, users can neither focus nor change the input, its state is exposed to assistive technology, and the form leaves its value out when users submit it.
Read-Only
Values that the system assigns, such as the number of an order, belong in a form without being open to change. Through readOnly the input keeps such a value fixed while users can still reach it with the keyboard, and unlike a disabled input, it is submitted with the form.
Invalid
When a value fails a check, such as an article number with too few digits, users need to know which field to correct. Setting aria-invalid marks the input as invalid, so users recognize it at a glance and assistive technology reports it as well.
API Reference
| Prop | type |
|---|---|
| Type | HTMLInputTypeAttribute |
| Default | – |
| Description | Sets the kind of value that users enter, such as "text", "email", "number", "password" or "file". Without it, the input takes one line of text. |
| Prop | value |
|---|---|
| Type | string | readonly string[] | number |
| Default | – |
| Description | Sets the value of the input and makes it controlled. Update it in onChange. |
| Prop | defaultValue |
|---|---|
| Type | string | number | readonly string[] |
| Default | – |
| Description | Sets the value of the input at first render. The input then tracks its value itself. |
| Prop | onChange |
|---|---|
| Type | (event: ChangeEvent<HTMLInputElement>) => void |
| Default | – |
| Description | Runs when users change the value. It receives the change event, and event.target.value holds the value that follows. |
| Prop | placeholder |
|---|---|
| Type | string |
| Default | – |
| Description | Shows a hint in muted text while the input is empty. The hint disappears as soon as users type, so it doesn't replace the label. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from focusing and changing the input, dims it and exposes the disabled state to assistive technology. A disabled input is not submitted with the form. |
| Prop | readOnly |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from changing the value. Unlike a disabled input, it stays focusable and is submitted with the form. |
| Prop | required |
|---|---|
| Type | boolean |
| Default | false |
| Description | Requires users to fill in the input before they can submit the form. |
| Prop | aria-invalid |
|---|---|
| Type | boolean | "false" | "true" | "grammar" | "spelling" |
| Default | – |
| Description | Marks the value as invalid for assistive technology. With true, the border, the background, the text and the placeholder take the destructive color. |