Skip to content

Textarea 文本域 ​

受控多行输入框,支持行数、字数统计与最大长度。

契约源:lh-ui-core/specs/textarea.json · 黄金用例:cases/textarea.json(6 条,已翻译为 JVM 单测 + Compose UI Test)

基本用法 ​

最小高度 = 行数 × 行高 × 字号,超出后纵向滚动。

9:41Textarea

HTML 模拟预览(Android / Compose 外观),非真机渲染

kotlin
var value by remember { mutableStateOf("") }
LhTextarea(
  value = value,
  rows = 3,
  showCount = true,
  maxlength = 200,
  onValueChange = { value = it },
)

字数统计 ​

showCount 为 true 时右下角渲染计数(textarea.countFontSize,色 text.secondary);maxlength 截断输入。

9:41Textarea 计数

HTML 模拟预览(Android / Compose 外观),非真机渲染;真机右下角显示「3 / 20」计数

API ​

Props ​

属性说明类型默认值
value受控值(必填)String—
onValueChange值变更回调(必填)(String) -> Unit—
placeholder占位文字String?null
rows行数(决定最小高度)Int3
disabled是否禁用Booleanfalse
readonly是否只读Booleanfalse
showCount是否显示字数统计Booleanfalse
maxlength最大长度(超长截断)IntInt.MAX_VALUE
onFocus / onBlur聚焦 / 失焦回调(() -> Unit)?null
modifier修饰符ModifierModifier

令牌对照 ​

令牌用途
textarea.paddingVertical / textarea.paddingHorizontal内边距(spacing.md)
textarea.fontSize字号(typography.fontSize.base)
textarea.borderRadius / textarea.borderWidth / textarea.focusRingWidth圆角 / 描边宽 / 焦点描边宽
textarea.countFontSize计数字号(typography.fontSize.extraSmall
textarea.minHeight最小高度基准
typography.lineHeight.primary行高(行数换算)
color.text.{primary,secondary,placeholder}文字 / 计数 / 占位色
color.focus / color.border.base焦点 / 默认描边色
color.disabled.{bg,text,border}禁用态三色

黄金用例对照 ​

case id断言本地测试
input-emits-onvaluechange输入触发 onValueChangeLhTextareaTest.input_emits_onValueChange(Compose UI Test)
disabled-blocks-input禁用态不可输入LhTextareaTest.disabled_blocks_input
showcount-renders-count计数渲染「n / max」LhTextareaTest.showcount_renders_count
showcount-without-maxlength-renders-length-only无 maxlength 时只渲染长度LhTextareaTest.showcount_without_maxlength_renders_length_only
focus-uses-focus-token聚焦消费焦点令牌LhTextareaDefaultsTest.focus_uses_focus_tokens
maxlength-truncates-input超长输入截断LhTextareaTest.maxlength_truncates_input
bash
./gradlew :components:textarea:test                            # JVM 部分
./gradlew :components:textarea:connectedAndroidTest           # 交互部分(需设备)