FormControl
Renders a labelled input and, optionally, associated validation text and/or hint text.
When rendering an input other than a form component from Primer, you must manually pass the attributes that make the form control accessible:
FormControl.Label
should be associated with the text input by using htmlFor
aria-describedby
aria-describedby
aria-describedby
. Example: aria-describedby="caption-id validation-id"
aria-invalid={true}
should be passed to the input.disabled
should be passed.required
should be passed.When rendering a custom checkbox or radio component, you must also pass layout="horizontal"
to the FormControl
component.
We encourage using FormControl
alongside all standalone form components like TextInput
, as every input must have a corresponding label to be accessible to assistive technology.
FormControl
also provides an interface for showing a hint text caption and a validation message, and associating those with the input for assistive technology.
Name | Type | Default | Description |
---|---|---|---|
children Required | FormControl.Label | FormControl.Caption | FormControl.Validation | Autocomplete | TextInput | TextInputWithTokens | Select | ||
disabled | boolean | false | Whether the control allows user input |
id | string | a generated string | The unique identifier for this control. Used to associate the label, validation text, and caption text |
required | boolean | false | If true, the user must specify a value for the input before the owning form can be submitted |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. | |
ref | React.RefObject<HTMLDivElement> | A ref to the element rendered by this component. |
Name | Type | Default | Description |
---|---|---|---|
visuallyHidden | boolean | false | Whether the label should be visually hidden |
as | React.ElementType | 'label' | The label element can be changed to a 'legend' when it's being used to label a fieldset, or a 'span' when it's being used to label an element that is not a form input. For example: when using a FormControl to render a labeled SegementedControl, the label should be a 'span' |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |
Name | Type | Default | Description |
---|---|---|---|
children | React.ReactNode | The content (usually just text) that is rendered to give contextual info about the field | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |
Name | Type | Default | Description |
---|---|---|---|
children | React.ReactNode | The content (usually just text) that is rendered to give contextual info about the validation result for the field | |
variant Required | 'error' | 'success' | 'warning' | Changes the visual style to match the validation status | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |
Name | Type | Default | Description |
---|---|---|---|
children | React.ReactNode | The visual to render before the choice input's label | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |