useField
Provides the accessibility implementation for input fields. Fields accept user input, gain context from their label, and may display a description or error message.
| install | yarn add @react-aria/label |
|---|---|
| version | 3.2.0 |
| usage | import {useField} from '@react-aria/label' |
API#
useField(
(props: AriaFieldProps
)): FieldAriaExample#
The useField hook associates a form control with a label, and an optional description and/or error message. This is useful for providing context about how users should fill in a field, or a validation message. useField takes care of creating ids for each element and associating them with the correct ARIA attributes (aria-labelledby and aria-describedby).
By default, useField assumes that the label is a native HTML <label> element. However, if you are labeling a non-native form element, be sure to use an element other than a <label> and set the labelElementType prop appropriately.
Note: Many other React Aria hooks such as useTextField, useSelect, and useComboBox already include support for description and error message elements. If you're using one of those hooks, there's no need to use useField.
function ContactPicker(props) {
let {labelProps fieldProps descriptionProps errorMessageProps} = useField(
props
);
return (
<div
style={
display: 'flex'
flexDirection: 'column'
width: 200
marginBottom: 20
}>
<label ...labelProps>propslabel</label>
<select ...fieldProps>
<option>Email</option>
<option>Phone</option>
<option>Fax</option>
<option>Carrier pigeon</option>
</select>
propsdescription && (
<div ...descriptionProps style={fontSize: 12}>
propsdescription
</div>
)
propserrorMessage && (
<div ...errorMessageProps style={color: 'red' fontSize: 12}>
propserrorMessage
</div>
)
</div>
);
}
<ContactPicker
label="Preferred contact method"
description="Select the best way to contact you about issues with your account."
/>
<ContactPicker
label="Preferred contact method"
errorMessage="Select a contact method."
/>function ContactPicker(props) {
let {
labelProps
fieldProps
descriptionProps
errorMessageProps
} = useField(props);
return (
<div
style={
display: 'flex'
flexDirection: 'column'
width: 200
marginBottom: 20
}>
<label ...labelProps>propslabel</label>
<select ...fieldProps>
<option>Email</option>
<option>Phone</option>
<option>Fax</option>
<option>Carrier pigeon</option>
</select>
propsdescription && (
<div ...descriptionProps style={fontSize: 12}>
propsdescription
</div>
)
propserrorMessage && (
<div
...errorMessageProps
style={color: 'red' fontSize: 12}>
propserrorMessage
</div>
)
</div>
);
}
<ContactPicker
label="Preferred contact method"
description="Select the best way to contact you about issues with your account."
/>
<ContactPicker
label="Preferred contact method"
errorMessage="Select a contact method."
/>function ContactPicker(
props
) {
let {
labelProps
fieldProps
descriptionProps
errorMessageProps
} = useField(props);
return (
<div
style={
display: 'flex'
flexDirection:
'column'
width: 200
marginBottom: 20
}>
<label
...labelProps>
propslabel
</label>
<select
...fieldProps>
<option>
Email
</option>
<option>
Phone
</option>
<option>
Fax
</option>
<option>
Carrier pigeon
</option>
</select>
propsdescription && (
<div
...descriptionProps
style={
fontSize: 12
}>
propsdescription
</div>
)
propserrorMessage && (
<div
...errorMessageProps
style={
color: 'red'
fontSize: 12
}>
propserrorMessage
</div>
)
</div>
);
}
<ContactPicker
label="Preferred contact method"
description="Select the best way to contact you about issues with your account."
/>
<ContactPicker
label="Preferred contact method"
errorMessage="Select a contact method."
/>| Name | Type | Default | Description |
labelElementType | ElementType | 'label' | The HTML element used to render the label, e.g. 'label', or 'span'. |
label | ReactNode | — | The content to display as the label. |
id | string | — | The element's unique identifier. See MDN. |
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. |
description | ReactNode | — | A description for the field. Provides a hint such as specific requirements for what to choose. |
errorMessage | ReactNode | — | An error message for the field. |
validationState | ValidationState | — | Whether the input should display its "valid" or "invalid" visual styling. |
| Name | Type | Description |
descriptionProps | HTMLAttributes<HTMLElement> | Props for the description element, if any. |
errorMessageProps | HTMLAttributes<HTMLElement> | Props for the error message element, if any. |
labelProps | LabelHTMLAttributes<HTMLLabelElement> | Props to apply to the label container element. |
fieldProps | AriaLabelingProps & DOMProps | Props to apply to the field container element being labeled. |