import { Field, Input } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root>
<Field.Label>Email</Field.Label>
<Input placeholder="me@example.com" />
</Field.Root>
)
}
用法
¥Usage
import { Field } from "@chakra-ui/react"
<Field.Root>
<Field.Label>
<Field.RequiredIndicator />
</Field.Label>
<Input />
<Field.HelperText />
<Field.ErrorText />
</Field.Root>
如果你更喜欢封闭的组件组合,请查看 以下代码片段。
¥If you prefer a closed component composition, check out the snippet below.
示例
¥Examples
错误文本
¥Error Text
将 invalid
属性传递给 Field.Root
组件,并使用 Field.ErrorText
组件指示该字段无效。
¥Pass the invalid
prop to Field.Root
and use the Field.ErrorText
to
indicate that the field is invalid.
import { Field, Input } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root invalid>
<Field.Label>Email</Field.Label>
<Input placeholder="me@example.com" />
<Field.ErrorText>This is an error text</Field.ErrorText>
</Field.Root>
)
}
辅助文本
¥Helper Text
使用 Field.HelperText
向字段添加辅助文本。
¥Use the Field.HelperText
to add helper text to the field.
import { Field, Input } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root>
<Field.Label>Email</Field.Label>
<Input placeholder="me@example.com" />
<Field.HelperText>This is a helper text</Field.HelperText>
</Field.Root>
)
}
水平
¥Horizontal
使用 orientation="horizontal"
属性可水平对齐标签和输入。
¥Use the orientation="horizontal"
prop to align the label and input
horizontally.
import { Field, Input, Stack, Switch } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="8" maxW="sm" css={{ "--field-label-width": "96px" }}>
<Field.Root orientation="horizontal">
<Field.Label>Name</Field.Label>
<Input placeholder="John Doe" flex="1" />
</Field.Root>
<Field.Root orientation="horizontal">
<Field.Label>Email</Field.Label>
<Input placeholder="me@example.com" flex="1" />
</Field.Root>
<Field.Root orientation="horizontal">
<Field.Label>Hide email</Field.Label>
<Switch.Root>
<Switch.HiddenInput />
<Switch.Control />
</Switch.Root>
</Field.Root>
</Stack>
)
}
已禁用
¥Disabled
使用 disabled
属性禁用字段。
¥Use the disabled
prop to disable the field.
import { Field, Input } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root disabled>
<Field.Label>Email</Field.Label>
<Input placeholder="me@example.com" />
</Field.Root>
)
}
文本区域
¥Textarea
以下是如何将字段组件与文本区域结合使用。
¥Here's how to use the field component with a textarea.
import { Field, Textarea } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root>
<Field.Label>Email</Field.Label>
<Textarea placeholder="Email" />
</Field.Root>
)
}
原生选择
¥Native Select
以下是如何将字段组件与原生选择功能结合使用。
¥Here's how to use the field component with a native select.
import { Field, NativeSelect } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root>
<Field.Label>Email</Field.Label>
<NativeSelect.Root>
<NativeSelect.Field>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</NativeSelect.Field>
<NativeSelect.Indicator />
</NativeSelect.Root>
</Field.Root>
)
}
必需
¥Required
将 required
属性传递给 Field.Root
组件,并使用 Field.RequiredIndicator
组件指示该字段为必填项。
¥Pass the required
prop to Field.Root
and use the Field.RequiredIndicator
to indicate that the field is required.
import { Field, Input } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root required>
<Field.Label>
Email
<Field.RequiredIndicator />
</Field.Label>
<Input placeholder="me@example.com" />
</Field.Root>
)
}
可选
¥Optional
将 fallback
属性传递给 Field.RequiredIndicator
,以添加可选文本。
¥Pass the fallback
prop to the Field.RequiredIndicator
to add optional text.
import { Badge, Field, Input } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root>
<Field.Label>
Email
<Field.RequiredIndicator
fallback={
<Badge size="xs" variant="surface">
Optional
</Badge>
}
/>
</Field.Label>
<Input placeholder="me@example.com" />
</Field.Root>
)
}
已关闭组件
¥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 field
属性
¥Props
根元素
¥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.ElementType The underlying element to render. | |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | boolean Whether to remove the component's style. | |
disabled | boolean Indicates whether the field is disabled. | |
ids | ElementIds The ids of the field parts. | |
invalid | boolean Indicates whether the field is invalid. | |
readOnly | boolean Indicates whether the field is read-only. | |
required | boolean Indicates whether the field is required. |