Field โ
A form field with a label, an input or control, and an optional validation message.
Name | Value |
---|---|
Props | |
labelIcon | |
optional | |
hideLabel | |
isFieldset | |
disabled | |
status | |
Slots | |
label | |
description | |
help-text | |
View | |
Reading direction | |
Note: For icon properties, the relevant icon also needs to be imported from the @wikimedia/codex-icons package. See the usage documentation for more information. |
Overview โ
When to use Field โ
Use the Field component to create form layouts with a variety of form inputs, such as:
- TextInput and TextArea
- Select
- Combobox
- Checkbox, Radio groups, and ToggleSwitch
- ChipInput
- Lookup and MultiselectLookup
If you do not need to collect user input, do not use Field. For example, Field is not necessary to display a SearchInput. Although SearchInput is built with a TextInput component, it triggers an action and is not a form item.
About Field โ
Field provides features for building a form field to collect user input. Field includes the following elements.
Label โ
A label
serves as a precise and informative title for the form field, indicating the type of information to be entered. Explore Label to check its different properties.
Keep the Fieldโs label short, clear, and easy to scan. Consistent & Clear
- Label text is bold by default, but the label of nested fields should use the regular font weight to make the hierarchy clear.
- Use a start icon next to the label to emphasize the required input in the form field.
- Avoid using an icon next to the label if the input already contains an icon.
Optional indicator (optional) โ
Fields are assumed to be required by default. If a field is optional, it can be labeled as such.
- Only indicate the optional Fields in the form, ensuring that the entire word "optional" is displayed for translation purposes.
- Don't mark required labels with an asterisk or use abbreviations in the optional indicator.
Description (optional) โ
A description can also be added below the label to provide more information about it. This description can include plain text and links.
Input(s) โ
The input element, or a group of inputs, where the user enters information. Field can use any form input such as TextInput, Select, Checkbox, etc.
Placeholder (when applicable) โ
Labels indicate what the input should be. Placeholder text provides an example of what type of information is being requested in the input.
Placeholder text should further illustrate and support the label. Clear
Placeholder text should never replace the label nor be the input's only description. Accessible
Help text (optional) โ
Help text provides supplementary information or instructions to guide users in completing the field correctly. This text often clarifies formatting or indicates input restrictions. Help text can include plain text and links.
Give clear information about input restrictions. Clear & Accessible
Validation message โ
An inline validation message will appear to provide feedback about the status of the field.
Examples โ
For more detailed information about form fields, consult the guidelines for constructing forms.
Validation messages โ
You can display a validation message based on the current status of the field.
Developer notes
Set the status
prop based on the field's validity, then pass in an object of messages
keyed on validation status type. If there is a message for the current status, it will be displayed. Children of the Field component will automatically display styles for that status.
Setting the status based on field validity is up to you. In the error example, it's done as you're changing the input. You could also validate on blur to give the user a chance to finish filling out the field, as demonstrated in the warning example and the success example.
Error โ
Try entering a username into the field below that's longer than a single character to trigger the error state and error message.
Let a reader know exactly what to do to fix the issue. Relevant & Trustworthy
Don't give multiple options for fixing the issue. Concise
Warning โ
This example shows a warning message on blur if the username doesn't meet the criteria written in the help text. Note that form inputs do not display a "warning" state.
Keep messages short and precise. Concise & Accessible
Don't write in a way that might cause a reader to panic or worry. Trustworthy
Success โ
This example shows a success message on blur when the username is unique. Note that form inputs do not display a "success" state.
Use clear, positive, short messages. Accessible & Concise
Don't include too much information. Relevant
Fieldset with radio group โ
Groups of Radio or Checkbox components are considered fieldsets.
Use a fieldset for groups of binary inputs.
Developer notes
For fieldsets, set the isFieldset
prop to true
. This will output a <fieldset>
element with a <legend>
instead of a label.
Field with multiple inputs โ
The following example is a fieldset with two inputs that depend on one another. Each input needs its own individual label, but in this case, the individual labels can be visually-hidden.
- Use a fieldset for any field that contains multiple inputs or controls that depend on one another to form a single meaningful value or dataset.
- Don't use a fieldset to group separate inputs or for layout purposes.
Developer notes
In the example below, the labels for the individual TextInput and Select components are included in two different ways:
- For the TextInput, the Label component is used with
hideLabel
set totrue
. - For the Select, an
aria-label
is applied.
Note: when you enter erroneous data in the TextInput below, error styles for both the TextInput and the Select will display since they are both contained in a single Field. If you need to show separate states for each input, refer to the nested Fields example below.
Fieldset with nested Fields โ
Nest Fields within a Field when each input needs its own visible label, description, validation status, and validation message.
- Use a fieldset to group Fields that depend on one another to form a single meaningful value or dataset.
- Don't use a fieldset to group separate Fields or for layout purposes.
Developer notes
This is a fieldset, so set isFieldset
to true
.
Note that each sub-field has its own status. This enables you to show error styles only for the input that contains the error, not the entire fieldset.
Nested fields will become disabled when their parent field is disabled.
Custom help text content โ
The help-text
slot can contain markup, including other components and dynamic text. This is an example of a custom character counter embedded inside a Field.
- Use a character counter only when it's necessary to limit the number of characters in a form input.
- Display an inline error message if the character limit is exceeded.
Technical implementation โ
Vue usage โ
This component can wrap the following:
- A single form input or control
- An input group (e.g. a group of Radios or Checkboxes)
- A set of nested fields (inputs wrapped in their own Field components).
The following Codex components can be used inside the Field component:
- Checkbox
- ChipInput
- Combobox
- Lookup
- MultiselectLookup
- Radio
- SearchInput
- Select
- TextArea
- TextInput
- ToggleSwitch
Props โ
Prop name | Description | Type | Default |
---|---|---|---|
labelIcon | Icon before the label text. Do not use this if including a start icon within the input component. | Icon | '' |
optional | Whether the field is optional. This will add a flag next to the label indicating that the field is optional. | boolean | false |
optionalFlag | Text to indicate that the field is optional. Omit this prop to use the default value, "(optional)". | string | '' |
hideLabel | Whether the label should be visually hidden. Note that this will also hide the description. | boolean | false |
isFieldset | Whether this field contains a group of inputs. When true, this outputs a <fieldset> element with a semantic <legend> . When false, it outputs a <div> with a semantic <label> . | boolean | false |
disabled | Whether the entire field is disabled. | boolean | false |
status | status attribute of the input. This also determines which validation message is shown. | ValidationStatusType | 'default' |
messages | Message text keyed on validation status type. | ValidationMessages | {} |
Slots โ
Name | Description | Bindings |
---|---|---|
label | Label text. | |
description | Short description text. | |
default | Input, control, or input group. | |
help-text | Further explanation of how to use this field. | |
warning | Warning message content for messages containing HTML markup. | |
error | Error message content for messages containing HTML markup. | |
success | Success message content for messages containing HTML markup. |
CSS-only version โ
Refer to the CSS-only Label docs for instructions on making the label visually-hidden or adding a label icon.
Markup structure โ
Single input โ
Fieldset โ
Inside a <form>
, use a <fieldset>
element for input groups.
When outputting a <fieldset>
, the markup of this component is quite different:
- The outer element is the
<fieldset>
instead of a<div>
- A
<legend>
is used instead of a<label>
. Refer to the docs on the CSS-only Label for more info. - The description is included inside the
<legend>
- The
for
andaria-describedby
attributes are not needed
Disabled โ
To display a disabled field:
- Add the
.cdx-field--disabled
class to the outer element - Add the
.cdx-label--disabled
class to the.cdx-label
element - Disable the input(s) or control(s) (in the example below, the
disabled
attribute is added to the<input>
element)
This works with single fields and fieldsets.
Error status and message โ
To display error styles and show an error message:
- Apply error styles to the input (in the example below, the
.cdx-text-input--status-error
class is applied to the text input wrapper) - Add the validation message below the control wrapper
Keyboard navigation โ
Key | Function |
---|---|
Tab | It moves the focus to the next interactive element in tab order. |
Shift + Tab | It moves the focus to the previous interactive element. |
Enter | If the focus is placed on one of the interactive elements within the Field, it activates them. |