Field
Form field with a label, an input or control, and optional validation handling.
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
- Combobox
- Lookup
- Radio
- SearchInput
- Select
- TextArea
- TextInput
- ToggleSwitch
Demos
Configurable
Note that this configurable demo is only shown with a TextInput inside the Field. See the demos below for use of the Field component with other types of inputs or groups of inputs, along with code samples.
See the validation and error state demo below for more information about how to add and customize an error message.
Name | Value |
---|---|
Props | |
labelIcon | |
optionalFlag | |
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. |
With rich description and help text
You can include markup in the description
and help-text
slots. Only links and text formatting are recommended as description and help text should be concise.
With validation and error state
You can display a validation message based on the current status of the field. 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.
The status
you bind to the Field component will also be passed down to its child components, which will display appropriate styles for that status (e.g. a red border in the error state). You do not need to bind the status
prop to the child input components.
Setting the status based on field validity is up to you. In the example below, 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.
Try entering a username into the field below that's longer than a single character to see the error state and error message.
Fieldsets
For any field that contains multiple inputs or controls, set the isFieldset
prop to true
. This will output a <fieldset>
element with a <legend>
instead of a label. Below are some examples of fieldsets.
Radio group
Groups of Radio or Checkbox components are considered fieldsets.
Complex field with two inputs
The field below has two inputs, a TextInput and a Select. In addition to the field-level label, each input needs its own individual label. In this case, the individual labels can be visually hidden. In the example below, the labels 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 that, 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, see the nested Fields example below.
Fieldset with nested Fields
In this example, each input within the field needs its own visible label, description, validation status, and validation message. To accomplish this, each input is wrapped in a Field component. The entire field is wrapped in a top-level Field component with isFieldset
set 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.
Usage
Props
Prop name | Description | Type | Values | Default |
---|---|---|---|---|
labelIcon | Icon before the label text. Do not use this if including a start icon within the input component. | Icon | - | '' |
optionalFlag | Text to indicate that the field is optional. For example, this might be '(optional)' in English. This text will be placed next to the label text. | 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. | ValidationStatusType | 'default' , 'error' | 'default' |
messages | Message text keyed on validation status type. | ValidationMessages | - | () => { return {}; } |
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. |