Textarea 多行輸入框
多行文字輸入。高度隨列數與內容增長,支援字數統計與字數上限。
契約源:
lh-ui-core/specs/textarea.json· 黃金用例:lh-ui-core/cases/textarea.json(6 條,已翻譯為 hypium 測試)
基本用法
rows 決定初始可見列數,最小高度取 textarea.minHeight 令牌。
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
typescript
LhTextarea({
value: this.remark,
placeholder: '請輸入備註內容',
rows: 3,
onValueChange: (value: string) => { this.remark = value },
})字數統計
showCount 為 true 時在右下角顯示字數統計:設定 maxlength 顯示 當前/上限, 不設定時只顯示當前長度。
11/50
0
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
API
Props
| 屬性 | 說明 | 型別 | 預設值 |
|---|---|---|---|
value | 當前文字值(受控) | string | '' |
placeholder | 佔位提示文字 | string | '' |
rows | 初始可見列數;高度隨列數與內容增長 | number | 3 |
disabled | 是否停用;停用時不回應輸入、不觸發 onValueChange | boolean | false |
readonly | 是否唯讀;唯讀時可聚焦但不接受輸入 | boolean | false |
showCount | 是否在右下角顯示字數統計 | boolean | false |
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 | 輸入觸發 onValueChange | disabled_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。

