Field
Used to add labels, help text, and error messages to form fields.
Usage
import { Field } from "@chakra-ui/react"<Field.Root>
<Field.Label>
<Field.RequiredIndicator />
</Field.Label>
<Input />
<Field.HelperText />
<Field.ErrorText />
</Field.Root>Examples
Error Text
Pass the invalid prop to Field.Root and use the Field.ErrorText to
indicate that the field is invalid.
Helper Text
Use the Field.HelperText to add helper text to the field.
Horizontal
Use the orientation="horizontal" prop to align the label and input
horizontally.
Disabled
Use the disabled prop to disable the field.
Textarea
Here's how to use the field component with a textarea.
Native Select
Here's how to use the field component with a native select.
Required
Pass the required prop to Field.Root and use the Field.RequiredIndicator
to indicate that the field is required.
Optional
Pass the fallback prop to the Field.RequiredIndicator to add optional text.
Closed Component
Here's how to setup the Field for a closed component composition.
import { Field as ChakraField } from "@chakra-ui/react"
import * as React from "react"
export interface FieldProps extends Omit<ChakraField.RootProps, "label"> {
label?: React.ReactNode
helperText?: React.ReactNode
errorText?: React.ReactNode
optionalText?: React.ReactNode
}
export const Field = React.forwardRef<HTMLDivElement, FieldProps>(
function Field(props, ref) {
const { label, children, helperText, errorText, optionalText, ...rest } =
props
return (
<ChakraField.Root ref={ref} {...rest}>
{label && (
<ChakraField.Label>
{label}
<ChakraField.RequiredIndicator fallback={optionalText} />
</ChakraField.Label>
)}
{children}
{helperText && (
<ChakraField.HelperText>{helperText}</ChakraField.HelperText>
)}
{errorText && (
<ChakraField.ErrorText>{errorText}</ChakraField.ErrorText>
)}
</ChakraField.Root>
)
},
)
If you want to automatically add the closed component to your project, run the command:
npx @chakra-ui/cli snippet add fieldProps
Root
| Prop | Default | Type |
|---|---|---|
colorPalette | gray | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'The color palette of the component |
orientation | vertical | 'vertical' | 'horizontal'The orientation of the component |
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | booleanWhether to remove the component's style. | |
disabled | booleanIndicates whether the field is disabled. | |
ids | ElementIdsThe ids of the field parts. | |
invalid | booleanIndicates whether the field is invalid. | |
readOnly | booleanIndicates whether the field is read-only. | |
required | booleanIndicates whether the field is required. |
Explorer
Explore the Field component parts interactively. Click on parts in the sidebar
to highlight them in the preview.
Component Anatomy
Hover to highlight, click to select parts
root
errorText
helperText
input
label
select
textarea
requiredIndicator