Skip to Content
文档

文本区域

用于输入多行文本。

SourceStorybookRecipe

用法

¥Usage

import { Textarea } from "@chakra-ui/react"
<Textarea placeholder="..." />

示例

¥Examples

变量

¥Variants

使用 variant 属性更改文本区域的外观。

¥Use the variant prop to change the appearance of the textarea.

尺寸

¥Sizes

使用 size 属性更改文本区域的大小。

¥Use the size prop to change the size of the textarea.

辅助文本

¥Helper Text

将文本区域与 Field 组件配对以添加帮助文本。

¥Pair the textarea with the Field component to add helper text.

Max 500 characters.
Max 500 characters.

错误文本

¥Error Text

将文本区域与 Field 组件配对以添加错误文本。

¥Pair the textarea with the Field component to add error text.

Field is required
Field is required

字段

¥Field

使用 Field 组件编写文本区域,添加标签、帮助文本和错误文本。

¥Compose the textarea with the Field component to add a label, helper text, and error text.

钩子表单

¥Hook Form

以下是如何将文本区域与 react-hook-form 集成的示例。

¥Here's an example of how to integrate the textarea with react-hook-form.

A short description of yourself

调整大小

¥Resize

使用 resize 属性控制文本区域的大小调整行为。

¥Use the resize prop to control the resize behavior of the textarea.

要限制文本区域的最大高度(或行数),我们建议使用 maxHeight 属性并将其值设置为 lh 单位。

¥To limit the maximum height (or rows) of the textarea, we recommend using the maxHeight prop and setting the value to a lh unit.

<Textarea autoresize maxH="5lh" />

自动调整大小

¥Autoresize

使用 autoresize 属性使文本区域在你输入时自动垂直调整大小。

¥Use the autoresize prop to make the textarea autoresize vertically as you type.

参考

¥Ref

以下是如何访问底层元素引用的示例

¥Here's how to access the underlying element reference

const Demo = () => {
  const ref = useRef<HTMLTextAreaElement | null>(null)
  return <Textarea ref={ref} />
}

属性

¥Props

PropDefaultType
colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

size 'md'
'xs' | 'sm' | 'md' | 'lg' | 'xl'

The size of the component

variant 'outline'
'outline' | 'subtle' | 'flushed'

The variant 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.

Previous

滑块

Next

组合框