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
lhOnFocus / lhOnBlur聚焦 / 失焦回呼(() -> 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           # 互動部分(需裝置)