Skip to content

Input 输入框 ​

单行文本输入。支持密码类型、三档尺寸、禁用 / 只读、清除按钮与字数上限。

契约源:lh-ui-core/specs/input.json · 黄金用例:lh-ui-core/cases/input.json(8 条,已翻译为 hypium 测试)

基本用法 ​

value 受控,placeholder 取 color.text.placeholder 令牌。

9:41Input 基本

HTML 模拟预览(鸿蒙 / ArkUI 外观),非真机渲染

typescript
LhInput({
  value: this.text,
  placeholder: '请输入内容',
  clearable: true,
  onValueChange: (value: string) => { this.text = value },
})

聚焦与尺寸 ​

聚焦时描边切换为 color.focus 并叠加 input.focusRingWidth 宽度的聚焦环; 三档尺寸映射到 input.height / paddingHorizontal / fontSize 令牌。

9:41Input 聚焦与尺寸

HTML 模拟预览(鸿蒙 / ArkUI 外观),非真机渲染

状态与清除 ​

disabled 禁用输入且呈现禁用样式;clearable 在有值时显示清除按钮, 点击触发 onValueChange('');maxlength 超出时截断新输入。

9:41Input 状态
✕

HTML 模拟预览(鸿蒙 / ArkUI 外观),非真机渲染

API ​

Props ​

属性说明类型默认值
value当前文本值(受控)string''
placeholder占位提示文字string''
type输入类型;密码类型映射到 ArkUI InputType.PasswordLhInputTypeText
size输入框尺寸,映射到尺寸令牌LhInputSizeMedium
disabled是否禁用;禁用时不响应输入、不触发 onValueChangebooleanfalse
readonly是否只读;只读时可聚焦但不接受输入booleanfalse
clearable是否显示清除按钮(有值时)booleanfalse
maxlength最大字符数;超出截断;为 undefined 时不限制number?undefined
onValueChange文本变化时触发;disabled / readonly 时不触发(value: string) => void空函数
onFocus获得焦点时触发() => void空函数
onBlur失去焦点时触发() => void空函数

LhInputType ​

值端侧落点
TextArkUI InputType.Normal
PasswordArkUI InputType.Password(掩码显示)

LhInputSize ​

值heightpaddingHorizontalfontSize
Smallinput.height.small(32vp)input.paddingHorizontal.small(12vp)input.fontSize.small(13)
Mediuminput.height.medium(40vp)input.paddingHorizontal.medium(16vp)input.fontSize.medium(14)
Largeinput.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输入触发 onValueChangedisabled_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。