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。