Label
Form labels commonly used with inputs
The Label
React component uses the for
prop to associate itself with a form field.
The for
and id
props must match and be unique to the page. You can use Lodash's uniqueId
to generate a unique id.
Examples
Label with an input
<> <Label for="example-49293"> Email address </Label> <TextInput id="example-49293" onChange={function _(){}} placeholder="example@example.com" /> </>
Disabled label
Labels can appear visually disabled. It’s important to also disable the associated form element.
<> <Label for="example-201998" isDisabled > Email address </Label> <TextInput id="example-201998" isDisabled onChange={function _(){}} placeholder="example@example.com" /> </>
Label with a validation error
You can change the label color to red when the associated form element has invalid data.
Validation errors must be also include text that explains the error. This helps guide the user and is crucial for users with visual impairments.
<> <Label for="example-083109" hasError > Email address </Label> <TextInput hasError id="example-083109" onChange={function _(){}} value="example@exampl" /> <div className="mb1"> <FormNote hasError> The email address you entered is incorrect. </FormNote> </div> </>
Props
Label
children
The label text that appears above the form element.
TypeReact.ReactNode
Defaultnull
for
id
of the form field that's associated with thelabel
. Clicking on the label text will switch the browser's focus to the form field.The Label's
for
and form field'sid
prop must match and be unique to the page. You can use Lodash'suniqueId
to generate a unique id.Typestring
isDisabled
Visually disable the label.
Typeboolean
Defaultfalse
hasError
Changes the label text color to red.
Typeboolean
Defaultfalse
dataTestId
A selector to hook into the React component for use in automated testing environments.
Typestring
dataTest
deprecatedA selector to hook into the React component for use in automated testing environments.
Note:
Deprecated in favor of the
dataTestId
propTypestring