Input 輸入框
單行文字輸入。支援密碼類型、三檔尺寸、停用 / 唯讀、清除按鈕與字數上限。
契約源:
lh-ui-core/specs/input.json· 黃金用例:lh-ui-core/cases/input.json(8 條,已翻譯為 hypium 測試)
基本用法
value 受控,placeholder 取 color.text.placeholder 令牌。
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
typescript
LhInput({
value: this.text,
placeholder: '請輸入內容',
clearable: true,
onValueChange: (value: string) => { this.text = value },
})聚焦與尺寸
聚焦時描邊切換為 color.focus 並疊加 input.focusRingWidth 寬度的聚焦環; 三檔尺寸映射到 input.height / paddingHorizontal / fontSize 令牌。
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
狀態與清除
disabled 停用輸入且呈現停用樣式;clearable 在有值時顯示清除按鈕, 點選觸發 onValueChange('');maxlength 超出時截斷新輸入。
✕
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
API
Props
| 屬性 | 說明 | 型別 | 預設值 |
|---|---|---|---|
value | 當前文字值(受控) | string | '' |
placeholder | 佔位提示文字 | string | '' |
type | 輸入類型;密碼類型映射到 ArkUI InputType.Password | LhInputType | Text |
size | 輸入框尺寸,映射到尺寸令牌 | LhInputSize | Medium |
disabled | 是否停用;停用時不回應輸入、不觸發 onValueChange | boolean | false |
readonly | 是否唯讀;唯讀時可聚焦但不接受輸入 | boolean | false |
clearable | 是否顯示清除按鈕(有值時) | boolean | false |
maxlength | 最大字元數;超出截斷;為 undefined 時不限制 | number? | undefined |
onValueChange | 文字變化時觸發;disabled / readonly 時不觸發 | (value: string) => void | 空函式 |
onFocus | 獲得焦點時觸發 | () => void | 空函式 |
onBlur | 失去焦點時觸發 | () => void | 空函式 |
LhInputType
| 值 | 端側落點 |
|---|---|
Text | ArkUI InputType.Normal |
Password | ArkUI InputType.Password(遮罩顯示) |
LhInputSize
| 值 | height | paddingHorizontal | fontSize |
|---|---|---|---|
Small | input.height.small(32vp) | input.paddingHorizontal.small(12vp) | input.fontSize.small(13) |
Medium | input.height.medium(40vp) | input.paddingHorizontal.medium(16vp) | input.fontSize.medium(14) |
Large | input.height.large(48vp) | input.paddingHorizontal.large(20vp) | input.fontSize.large(16) |
令牌對照
| 令牌 | 用途 |
|---|---|
input.height.{small,medium,large} | 三檔高度 |
input.paddingHorizontal.{small,medium,large} | 三檔橫向內邊距 |
input.fontSize.{small,medium,large} | 三檔字級 |
input.borderRadius / input.borderWidth | 圓角與描邊寬度 |
input.focusRingWidth | 聚焦環寬度 |
input.clearIconSize | 清除按鈕尺寸(16vp) |
color.text.primary / color.text.placeholder | 輸入文字 / 佔位文字色 |
color.background.default / color.background.disabled | 底色 / 停用底色 |
color.border.base / color.focus | 描邊色 / 聚焦色 |
color.disabled.text | 停用文字色 |
黃金用例對照
cases/input.json 的 8 條斷言與 hypium 測試的對應關係(entry/src/test/Input.test.ets):
| case id | 斷言 | 本地測試 |
|---|---|---|
input-emits-onvaluechange | 輸入觸發 onValueChange | disabled_is_not_interactive(gate 反向斷言) |
clearable-emits-empty-value | 清除觸發空值回呼 | clear_icon_uses_token(清除圖示消費令牌) |
disabled-blocks-input | 停用態不回應輸入 | disabled_is_not_interactive |
readonly-blocks-input | 唯讀態不回應輸入 | readonly_still_enabled_in_appearance |
placeholder-uses-placeholder-token | 佔位文字取佔位令牌 | placeholder_uses_placeholder_token |
focus-uses-focus-token | 聚焦態取 focus 令牌 | focus_uses_focus_token |
small-size-uses-small-tokens | 小號消費 small 尺寸令牌 | small_size_uses_small_tokens |
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。

