import {FormControl} from '@primer/react-brand'
FormControl
is responsible for layout and ensuring that relevant IDs and ARIA attributes are passed to its children
.
FormControl
can help compose form layouts quickly, while ensuring the inputs are accessible for assistive technologies.
An example of a typical layout composed using FormControl
:
Labels should only be visually hidden when the context is clear from the input itself. For example, a search input with a search icon. The majority of the time, labels should be visible.
The following example demonstrates declarative form validation in controlled mode.
Note that, when submitting the form with an invalid value, the invalid input will receive focus to help the user correct the error. This is especially important for users navigating the form using a screen reader.
More information on form validation best practices can be found in the Primer UI Patterns documentation.
Try changing the input value to monalisa
and submitting the form to show the success
state.
FormControl.Hint
can be used to provide additional context or guidance to the user. FormControl.Hint
must be a direct child of FormControl
.
Pass the fullWidth
prop to FormControl
to provide additional behavior and state context to all children
, rather than the input only.
FormControl
can appear in medium
and large
dimensions using the size
prop.
Pass the required
prop to FormControl
to provide additional behavior and state context to all children
, rather than the input only.
When marking a field as required, it is recommended to also provide a corresponding message at the start of the form informing the user that "all fields marked with an asterisk (*) are required".
refs
FormControl
inputs can be used in uncontrolled mode by forwarding a ref
to the underlying element.
FormControl
passes contextual data to its child inputs, labels, validation messaging and more.
Name | Type | Default | Description |
---|---|---|---|
children | 'FormControl.Label' 'Checkbox' 'Select' 'TextInput' 'FormControl.Validation' 'React.ReactElement' , | Valid child nodes | |
className | string | Sets a custom class | |
id | string | Sets a custom id | |
fullWidth | boolean | Stretches elements visually to the edges of its parent container. | |
ref | React.RefObject | Forward a Ref to the underlying DOM node | |
size | 'medium' 'large' | Visual dimensions for input and label | |
validationStatus | 'error' 'success' | Applies visual and semantic state to the underlying elements |
FormControl.Label
should be provided for the FormControl
to be accessible to assistive technology, but it may be visually hidden.
Name | Type | Default | Description |
---|---|---|---|
children | string | Label text | |
className | string | Applies a custom class | |
id | string | Sets a custom id | |
ref | React.RefObject | Forward a Ref to the underlying DOM node | |
size | 'medium' 'large' | 'medium' | Set visual dimensions |
visuallyHidden | boolean | false | Hide for sighted users |
Additional props can be passed to the <label>
element. See MDN for a list of props accepted by the <label>
element.
Name | Type | Default | Description |
---|---|---|---|
children | string | Validation message | |
className | string | FormControl.Validation custom class | |
id | string | Sets a custom id | |
ref | React.RefObject | Forward a Ref to the underlying DOM node | |
validationStatus | 'error' 'success' | Applies a visual and semantic validation state to the underlying elements |