useTextField
Provides the behavior and accessibility implementation for a text field.
install | yarn add @react-aria/textfield |
---|---|
version | 3.0.0-rc.2 |
usage | import {useTextField} from '@react-aria/textfield' |
API#
useTextField(
(props: AriaTextFieldProps,
, ref: RefObject<HTMLInputElement>
)): TextFieldAria
Features#
Text fields can be built with <input>
and <label> elements,
but you must manually ensure that they are semantically connected via ids for accessibility.
useTextField
helps automate this, and handle other accessibility features while
allowing for custom styling.
- Built with a native
<input>
element - Visual and ARIA labeling support
- Change, clipboard, composition, selection, and input event support
- Required and invalid states exposed to assistive technology via ARIA
Anatomy#
Text fields consist of an input element and a label. useTextField
automatically manages
the relationship between the two elements using the for
attribute on the <label>
element
and the aria-labelledby
attribute on the <input>
element.
useTextField
returns two sets of props that you should spread onto the appropriate element:
Name | Type | Description |
inputProps | InputHTMLAttributes<HTMLInputElement & HTMLTextAreaElement> | Props for the input element. |
labelProps | LabelHTMLAttributes<HTMLLabelElement> | Props for the text field's visible label element (if any). |
If there is no visual label, an aria-label
or aria-labelledby
prop must be passed instead
to identify the element to screen readers.
Example#
function TextField(props) {
let {label} = props;
let ref = ReactuseRef();
let {labelProps inputProps} = useTextField(props ref);
return (
<div style={display: 'flex' flexDirection: 'column' width: 200}>
<label ...labelProps> label</label>
<input ...inputProps ref= ref />
</div>
);
}
<TextField
label="Email"
placeholder="abc@example.com" />
function TextField(props) {
let {label} = props;
let ref = ReactuseRef();
let {labelProps inputProps} = useTextField(props ref);
return (
<div
style={
display: 'flex'
flexDirection: 'column'
width: 200
}>
<label ...labelProps> label</label>
<input ...inputProps ref= ref />
</div>
);
}
<TextField label="Email" placeholder="abc@example.com" />
function TextField(
props
) {
let {label} = props;
let ref = ReactuseRef();
let {
labelProps
inputProps
} = useTextField(
props
ref
);
return (
<div
style={
display: 'flex'
flexDirection:
'column'
width: 200
}>
<label
...labelProps>
label
</label>
<input
...inputProps
ref= ref
/>
</div>
);
}
<TextField
label="Email"
placeholder="abc@example.com"
/>
Internationalization#
RTL#
In right-to-left languages, text fields should be mirrored. The label should be right aligned, along with the text in the text field. Ensure that your CSS accounts for this.
Name | Type | Description |
aria-activedescendant | string | Identifies the currently active element when DOM focus is on a composite widget, textbox, group, or application. |
aria-autocomplete | 'none'
| 'inline'
| 'list'
| 'both' | Indicates whether inputting text could trigger display of one or more predictions of the user's intended value for an input and specifies how predictions would be presented if they are made. |
aria-haspopup | boolean
| 'menu'
| 'listbox'
| 'tree'
| 'grid'
| 'dialog' | Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. |
isDisabled | boolean | Whether the input is disabled. |
isReadOnly | boolean | Whether the input can be selected but not changed by the user. |
validationState | ValidationState | Whether the input should display its "valid" or "invalid" visual styling. |
isRequired | boolean | Whether user input is required on the input before form submission.
Often paired with the necessityIndicator prop to add a visual indicator to the input. |
autoFocus | boolean | Whether the element should receive focus on render |
onFocus | (
(e: FocusEvent
)) => void | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent
)) => void | Handler that is called when the element loses focus. |
onFocusChange | (
(isFocused: boolean
)) => void | Handler that is called when the element's focus status changes. |
onKeyDown | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is pressed. |
onKeyUp | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is released. |
placeholder | string | Temporary text that occupies the text input when it is empty. |
value | string | The current value (controlled). |
defaultValue | string | The default value (uncontrolled). |
onChange | (
(value: string
)) => void | Handler that is called when the value changes. |
label | ReactNode | The content to display as the label. |
aria-label | string | Defines a string value that labels the current element. |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | Identifies the element (or elements) that describes the object. |
aria-details | string | Identifies the element (or elements) that provide a detailed, extended description for the object. |
tabIndex | number | |
id | string | |
autoComplete | string | |
maxLength | number | |
minLength | number | |
name | string | |
pattern | string | |
type | 'text'
| 'search'
| 'url'
| 'tel'
| 'email'
| 'password'
| string | |
inputMode | 'none'
| 'text'
| 'tel'
| 'url'
| 'email'
| 'numeric'
| 'decimal'
| 'search' | Hints at the type of data that might be entered by the user while editing the element or its contents |
onCopy | ClipboardEventHandler<HTMLInputElement> | |
onCut | ClipboardEventHandler<HTMLInputElement> | |
onPaste | ClipboardEventHandler<HTMLInputElement> | |
onCompositionEnd | CompositionEventHandler<HTMLInputElement> | |
onCompositionStart | CompositionEventHandler<HTMLInputElement> | |
onCompositionUpdate | CompositionEventHandler<HTMLInputElement> | |
onSelect | ReactEventHandler<HTMLInputElement> | |
onBeforeInput | FormEventHandler<HTMLInputElement> | |
onInput | FormEventHandler<HTMLInputElement> | |
aria-errormessage | string | Identifies the element that provides an error message for the object. |
Name | Type | Description |
inputProps | InputHTMLAttributes<HTMLInputElement & HTMLTextAreaElement> | Props for the input element. |
labelProps | LabelHTMLAttributes<HTMLLabelElement> | Props for the text field's visible label element (if any). |