Beta Preview

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.

Introduction

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.

Example

Select the best way to contact you about issues with your account.
Select a contact method.
import {useField} from '@react-aria/label';

function ContactPicker(props) {
  let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField(props);

  return (
    <div style={{display: 'flex', flexDirection: 'column', width: 200, marginBottom: 20}}>
      <label {...labelProps}>{props.label}</label>
      <select {...fieldProps}>
        <option>Email</option>
        <option>Phone</option>
        <option>Fax</option>
        <option>Carrier pigeon</option>
      </select>
      {props.description &&
        <div {...descriptionProps} style={{fontSize: 12}}>{props.description}</div>
      }
      {props.errorMessage &&
        <div {...errorMessageProps} style={{color: 'red', fontSize: 12}}>{props.errorMessage}</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." />
</>

API

useField(props: ):

AriaFieldProps

NameTypeDefault
validate(value: T) => truenullundefinedDefault:
A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if validationBehavior="native". For realtime validation, use the isInvalid prop instead.
validationBehavior'aria''native'Default: 'aria'
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.
isInvalidbooleanDefault:
Whether the input value is invalid.
errorMessageReactNode(v: ) => ReactNodeDefault:
An error message for the field.
descriptionReactNodeDefault:
A description for the field. Provides a hint such as specific requirements for what to choose.
aria-detailsstringDefault:
Identifies the element (or elements) that provide a detailed, extended description for the object.
aria-describedbystringDefault:
Identifies the element (or elements) that describes the object.
aria-labelledbystringDefault:
Identifies the element (or elements) that labels the current element.
aria-labelstringDefault:
Defines a string value that labels the current element.
idstringDefault:
The element's unique identifier. See MDN.
labelReactNodeDefault:
The content to display as the label.
labelElementTypeElementTypeDefault: 'label'
The HTML element used to render the label, e.g. 'label', or 'span'.

FieldAria

NameType
fieldProps
Props to apply to the field container element being labeled.
labelPropsDOMAttributesLabelHTMLAttributes<HTMLLabelElement>
Props to apply to the label container element.
errorMessagePropsDOMAttributes
Props for the error message element, if any.
descriptionPropsDOMAttributes
Props for the description element, if any.