Textarea
用于收集多行文本并支持尺寸、字数统计和自定义计数展示的文本域组件
Textarea 用于收集评论、备注、描述等多行文本。组件基于原生 <textarea> 封装,保留常用原生属性透传,并提供统一尺寸、受控状态和可选字数统计。
import { Textarea, TextareaContent, TextareaCount, TextareaRoot } from '@skyroc/web-ui';何时使用
- 需要输入多行文本,如备注、评论、问题描述、长文本表单项。
- 需要显示当前输入长度,或配合
maxLength给用户明确的输入限制反馈。 - 需要按项目统一尺寸体系调整文本域高度、字号和内边距。
- 只收集单行文本时优先使用 Input;需要富文本编辑时不适合使用 Textarea。
基础用法
Textarea 支持原生 textarea 属性,placeholder、disabled、readOnly、name、required 等属性会透传到底层 <textarea>。
<Textarea placeholder="请输入内容" />字数统计
通过 showCount 显示字数统计。默认统计 String(value).length,没有 maxLength 时只显示当前数量。
<Textarea showCount />最大长度
通过原生 maxLength 限制输入长度,并在开启 showCount 后显示 当前数量 / 最大长度。
<Textarea showCount maxLength={200} />自定义统计内容
通过 countRender 自定义统计区域的渲染内容。countRender 接收组件格式化后的统计文本。
<Textarea showCount countRender={count => <span>{count}</span>} />字符簇计数
通过 countGraphemes 自定义计数逻辑,适合 emoji、组合字符等需要按用户可见字符统计的场景。
<Textarea
showCount
countGraphemes={value =>
Array.from(new Intl.Segmenter(undefined, { granularity: 'grapheme' }).segment(String(value ?? ''))).length
}
/>尺寸
通过 size 调整文本域的最小高度、字号、内边距和计数位置,从 xs 到 2xl 共 6 个尺寸。
| 尺寸 | 最小高度 | 字号 | 适用场景 |
|---|---|---|---|
xs | 24px | 10px | 紧凑表单、表格内嵌 |
sm | 28px | 12px | 辅助说明、侧边栏 |
md | 32px | 14px | 常规场景(默认) |
lg | 36px | 16px | 重点表单项 |
xl | 40px | 18px | 大号输入区域 |
2xl | 48px | 20px | 大幅文本输入 |
自定义样式
通过 classNames 分别控制 root、content 和 count 三个 slot。className 作为原生 textarea 属性会传给内容元素;如果需要精细控制外层容器或计数区域,优先使用 classNames。
<Textarea
showCount
classNames={{
root: 'max-w-md',
content: 'min-h-32',
count: 'text-primary'
}}
/>API
Textarea
通用属性参考:继承原生 textarea 属性,并增加字数统计、项目尺寸和 slot class 配置。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 受控输入值 | string | number | readonly string[] | - |
| defaultValue | 非受控模式下的默认值 | string | number | readonly string[] | - |
| onChange | 输入值变化时触发的原生 change 事件回调 | (event) => void | - |
| onTextChange | 输入值变化时触发,接收更新后的文本值 | (value: string | number | readonly string[] | undefined) => void | - |
| placeholder | 占位文本 | string | - |
| disabled | 是否禁用文本域 | boolean | - |
| readOnly | 是否只读 | boolean | - |
| maxLength | 最大输入长度;开启 showCount 后会参与统计文本展示 | number | - |
| showCount | 是否显示字数统计 | boolean | false |
| countRender | 自定义统计内容渲染函数,接收格式化后的统计文本 | (count: string) => ReactNode | - |
| countGraphemes | 自定义计数函数,用于覆盖默认字符串长度统计 | (input: string | number | readonly string[] | undefined) => number | - |
| size | 尺寸,影响最小高度、字号、内边距和计数位置 | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'md' |
| className | 底层 textarea 的自定义 class | ClassValue | - |
| classNames | 各 slot 的自定义 class(root / content / count) | TextareaClassNames | - |
| name | 表单字段名称 | string | - |
| required | 是否必填 | boolean | - |
| autoComplete | 浏览器自动完成策略 | string | - |
| autoFocus | 是否自动聚焦 | boolean | - |
TextareaRoot
外层容器组件,继承原生 div 属性。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| className | 外层容器的自定义 class | ClassValue | - |
| size | 尺寸,用于设置 data-size 并参与 root slot 变体计算 | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'md' |
TextareaContent
底层内容组件,继承原生 textarea 属性。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| className | 底层 textarea 的自定义 class | ClassValue | - |
| size | 尺寸,影响最小高度、字号和内边距 | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'md' |
| value | textarea 当前值 | string | number | readonly string[] | - |
| maxLength | 最大输入长度 | number | - |
TextareaCount
统计展示组件,继承原生 div 属性,但 children 被定义为统计文本渲染函数。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 用于统计的 textarea 当前值 | string | number | readonly string[] | - |
| maxLength | 最大输入长度;存在时展示为 当前数量 / 最大长度 | number | - |
| children | 自定义统计内容渲染函数 | (count: string) => ReactNode | - |
| countGraphemes | 自定义计数函数 | (input: string | number | readonly string[] | undefined) => number | - |
| className | 统计区域的自定义 class | ClassValue | - |
| size | 尺寸,影响统计区域字号和定位 | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'md' |
类型
TextareaProps
Textarea 主组件属性。继承原生 textarea 属性,并增加字数统计、项目尺寸和 slot class 配置。
| 字段 | 类型 | 说明 |
|---|---|---|
| autoComplete | string | 浏览器自动完成策略。 |
| autoFocus | boolean | 是否自动聚焦。 |
| className | ClassValue | 底层 textarea 的自定义 class。 |
| classNames | TextareaClassNames | 各 slot 的自定义 class。 |
| countGraphemes | (input: string | number | readonly string[] | undefined) => number | 自定义计数函数。 |
| countRender | (count: string) => ReactNode | 自定义统计内容渲染函数。 |
| defaultValue | string | number | readonly string[] | 非受控默认值。 |
| disabled | boolean | 是否禁用。 |
| maxLength | number | 最大输入长度。 |
| name | string | 表单字段名称。 |
| onChange | (event) => void | 原生 change 事件回调。 |
| onTextChange | (value: string | number | readonly string[] | undefined) => void | 文本值变化回调。 |
| placeholder | string | 占位文本。 |
| readOnly | boolean | 是否只读。 |
| required | boolean | 是否必填。 |
| size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 组件尺寸。来自 StyledComponentProps。 |
| showCount | boolean | 是否显示字数统计。 |
| value | string | number | readonly string[] | 受控输入值。 |
| ...textareaProps | textarea element props without native className | 其他原生 textarea 属性。 |
TextareaClassNames
各 slot 的自定义 class,用于精细化控制 Textarea 各部分样式。
| 字段 | 类型 | 说明 |
|---|---|---|
| root | ClassValue | 外层容器的 class。 |
| content | ClassValue | 底层 textarea 的 class。 |
| count | ClassValue | 统计区域的 class。 |
TextareaContentProps
底层 textarea 子组件属性,继承原生 textarea 属性并支持项目尺寸。
| 字段 | 类型 | 说明 |
|---|---|---|
| className | ClassValue | 自定义 class。 |
| maxLength | number | 最大输入长度。 |
| size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 组件尺寸。 |
| value | string | number | readonly string[] | textarea 当前值。 |
| ...textareaProps | textarea element props without native className | 其他原生 textarea 属性。 |
TextareaCountProps
统计展示子组件属性,继承原生 div 属性并使用函数式 children 渲染统计文本。
| 字段 | 类型 | 说明 |
|---|---|---|
| children | (count: string) => ReactNode | 自定义统计内容渲染函数。 |
| className | ClassValue | 自定义 class。 |
| countGraphemes | (input: string | number | readonly string[] | undefined) => number | 自定义计数函数。 |
| maxLength | number | 最大输入长度。 |
| size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 组件尺寸。 |
| value | string | number | readonly string[] | 用于统计的当前值。 |
| ...divProps | div element props without native className and children | 其他原生 div 属性。 |
TextareaRootProps
外层容器子组件属性,继承原生 div 属性并支持项目尺寸。
| 字段 | 类型 | 说明 |
|---|---|---|
| className | ClassValue | 自定义 class。 |
| size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 组件尺寸。 |
| ...divProps | div element props without native className | 其他原生 div 属性。 |
StyledComponentProps<T>
项目内用于包装组件 props 的通用类型。
| 字段 | 类型 | 说明 |
|---|---|---|
| ...sourceProps | inherited props without className | 继承原始组件属性,但重新定义 className。 |
| className | ClassValue | 组件 class。 |
| size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 项目统一尺寸类型。 |