Skip to content

Textarea 多行输入框 ​

多行文本输入。高度随行数与内容增长,支持字数统计与字数上限。

契约源:lh-ui-core/specs/textarea.json · 黄金用例:lh-ui-core/cases/textarea.json(6 条,已翻译为 hypium 测试)

基本用法 ​

rows 决定初始可见行数,最小高度取 textarea.minHeight 令牌。

9:41Textarea 基本

HTML 模拟预览(鸿蒙 / ArkUI 外观),非真机渲染

typescript
LhTextarea({
  value: this.remark,
  placeholder: '请输入备注内容',
  rows: 3,
  onValueChange: (value: string) => { this.remark = value },
})

字数统计 ​

showCount 为 true 时在右下角显示字数统计:设置 maxlength 显示 当前/上限, 不设置时只显示当前长度。

9:41Textarea 字数统计
11/50
0

HTML 模拟预览(鸿蒙 / ArkUI 外观),非真机渲染

API ​

Props ​

属性说明类型默认值
value当前文本值(受控)string''
placeholder占位提示文字string''
rows初始可见行数;高度随行数与内容增长number3
disabled是否禁用;禁用时不响应输入、不触发 onValueChangebooleanfalse
readonly是否只读;只读时可聚焦但不接受输入booleanfalse
showCount是否在右下角显示字数统计booleanfalse
maxlength最大字符数;超出截断;showCount 时作为分母number?undefined
onValueChange文本变化时触发;disabled / readonly 时不触发(value: string) => void空函数
onFocus获得焦点时触发() => void空函数
onBlur失去焦点时触发() => void空函数

令牌对照 ​

令牌用途
textarea.minHeight无内容时的最小高度(72vp,约三行)
textarea.paddingHorizontal / textarea.paddingVertical内边距(12 / 12vp)
textarea.fontSize输入字号(14)
textarea.countFontSize字数统计字号(12)
textarea.borderRadius / textarea.borderWidth圆角与描边宽度
textarea.focusRingWidth聚焦环宽度
typography.lineHeight.primary行高(1.8)
color.text.primary / color.text.placeholder输入文字 / 占位文字色
color.background.default / color.background.disabled底色 / 禁用底色
color.border.base / color.focus描边色 / 聚焦色

黄金用例对照 ​

cases/textarea.json 的 6 条断言与 hypium 测试的对应关系(entry/src/test/Textarea.test.ets):

case id断言本地测试
input-emits-onvaluechange输入触发 onValueChangedisabled_is_not_interactive(gate 反向断言)
disabled-blocks-input禁用态不响应输入disabled_is_not_interactive
showcount-renders-count字数统计渲染 当前/上限showcount_renders_count_with_maxlength
showcount-without-maxlength-renders-length-only无上限时只显示长度showcount_renders_length_only_without_maxlength
focus-uses-focus-token聚焦态取 focus 令牌focus_uses_focus_token
maxlength-truncates-input超出上限截断maxlength_truncates_input

另有暗色专项:dark_colors_come_from_dark_tokens / dimensions_are_identical_in_dark_mode。 运行:DevEco Studio 右键 entry/src/test → Run Tests。