Form Control
FormControl provides context such as isInvalid, isDisabled, and isRequired
to form elements.
It follows the WAI specifications for forms.
Import#
import {FormControl,FormLabel,FormErrorMessage,FormHelperText,} from "@chakra-ui/react"
Usage#
Sample usage for a radio or checkbox group#
Making a field required#
By passing the isRequired props, the Input field has aria-required set to
true, and the FormLabel will show a red asterisk.
Select Example#
Number Input Example#
Usage with Form Libraries#
Form Libraries like Formik make it soooo easy to manage form state and validation. I 💖 Formik
Improvements from v1#
We've improved the accessibility of the
FormControlcomponent. Here are the changes:idpassed to the form control will be passed to the form input directly.FormLabelwill havehtmlForthat points to theidof the form input.FormErrorMessageaddsaria-describedbyandaria-invalidpointing to the form input.FormHelperTextadds/extendsaria-describedbypointing to the form input.isDisabled,isRequired,isReadOnlyprops passed toFormControlwill cascade across all related components.
FormLabelis now aware of thedisabled,focusedanderrorstate of the form input. This helps you style the label accordingly using the_disabled,_focus, and_invalidstyle props.If you render
FormErrorMessageandisInvalidisfalseorundefined,FormErrorMessagewon't be visible. The only way to make it visible is by passingisInvalidand setting it totrue.
Props#
| Name | Type | Description | Default |
|---|---|---|---|
| colorScheme | "blue" | "cyan" | "gray" | "green" | "orange" | "pink" | "purple" | "red" | "teal" | "yellow" | "whiteAlpha" | "blackAlpha" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram" | Color Schemes for FormControl are not implemented in the default theme. You can extend the theme to implement them. | - |
| isDisabled | boolean | If `true`, the form control will be disabled. This has 2 side effects: - The `FormLabel` will have `data-disabled` attribute - The form element (e.g, Input) will be disabled | - |
| isInvalid | boolean | If `true`, the form control will be invalid. This has 2 side effects: - The `FormLabel` and `FormErrorIcon` will have `data-invalid` set to `true` - The form element (e.g, Input) will have `aria-invalid` set to `true` | - |
| isReadOnly | boolean | If `true`, the form control will be readonly | - |
| isRequired | boolean | If `true`, the form control will be required. This has 2 side effects: - The `FormLabel` will show a required indicator - The form element (e.g, Input) will have `aria-required` set to `true` | - |
| label | string | The label text used to inform users as to what information is requested for a text field. | - |
| size | string | - | |
| variant | string | - |