import { Field } from '@sky-uk/ui-core';

The Field component is used to decorate Text Input, Textarea and Select components with a label, message and validation.

Sub-components

  • Field.Label – label of the input element
  • Field.Control – input element
  • Field.Validation – error message of the input element
  • Field.Message – inline message of the input element

Control

Either a Text Input, Textarea or Select needs to be passed to the as prop of the Field.Control.

TextInput

Textarea

Select

Using a disabled, defaultValue on an uncontrolled Select input is not possbile with React. Once disabled is applied, React then considered the option no longer selectable by default and this option will only appear once a user has interacted with the Select.

Using a controlled field allows the Select to have a selected, but disabled option.

State and Validation

state can be confirmed or error.

The Field component will ensure the Validation and Label have the same state as the input component.

It is best practice to validate input fields only upon form submission.

Inline Message

Field.Message could be used to display inline message related to the field. These will always be displayed as the bottom most element on the input field.

Required Fields

Adding required attribute on the Field.Control will render an asterisk to signify that a field is required. The asterisk color will also change based on the input status.

Disabled and Read-only

You can provide disabled or readOnly properties to make text input or textarea non-interactive. Select component only has disabled property.

Accessibility

Please refer to the W3C Forms Concepts guide when working with forms.