Skip to content

Input 输入框 ​

受控单行输入框。@Binding var value 双向绑定,支持明暗密文、清除按钮、字数上限与聚焦环。

契约源:lh-ui-core/specs/input.json · 黄金用例:lh-ui-core/cases/input.json(8 条,已翻译为 XCTest,macOS 宿主即可运行)

基本形态 ​

高度与 Button 对齐(三档 input.height 令牌),聚焦时描边换 color.focus 并加宽为聚焦环。

9:41Input 基本

HTML 模拟预览(iOS / SwiftUI 外观),非真机渲染

swift
@State private var text = ""

LhInput(value: $text, placeholder: "请输入内容") { newValue in
  // onValueChange
}

尺寸 ​

三档尺寸映射到 input.height / paddingHorizontal / fontSize 令牌。

9:41Input 尺寸

HTML 模拟预览(iOS / SwiftUI 外观),非真机渲染

密码与清除 ​

type: .password 落地为 SecureField(测试 testPasswordTypeIsSecure); clearable 在有值时显示清除按钮,点击触发 onValueChange("")(黄金用例 clearable-emits-empty-value)。

9:41Input 扩展

HTML 模拟预览(iOS / SwiftUI 外观),非真机渲染

swift
LhInput(value: $pwd, type: .password, clearable: true)

API ​

Props ​

属性说明类型默认值
value输入内容(受控双向绑定)String—
placeholder占位提示文字String""
type输入类型(明文 / 密文)LhInputType.text
size输入框尺寸,映射到尺寸令牌LhInputSize.medium
disabled是否禁用(不触发任何回调)Boolfalse
readonly是否只读(不触发 onValueChange,保留聚焦样式)Boolfalse
clearable是否显示清除按钮Boolfalse
maxlength最大字数(超出截断,黄金用例 maxlength-truncates-input)Int?nil
onValueChange内容变化回调,payload 为新值((String) -> Void)?nil
onFocus聚焦回调(() -> Void)?nil
onBlur失焦回调(() -> Void)?nil

LhInputSize ​

值heightpaddingHorizontalfontSize
.smallinput.height.small(32pt)input.paddingHorizontal.small(12pt)input.fontSize.small(13pt)
.mediuminput.height.medium(40pt)input.paddingHorizontal.medium(16pt)input.fontSize.medium(14pt)
.largeinput.height.large(48pt)input.paddingHorizontal.large(20pt)input.fontSize.large(16pt)

LhInputType ​

值落地
.textTextField
.passwordSecureField

颜色 ​

状态描边文字 / 占位
默认color.border.base(input.borderWidth)color.text.primary / color.text.placeholder
聚焦color.focus(input.focusRingWidth)同上
禁用color.disabled.bordercolor.disabled.text

令牌对照 ​

令牌用途
input.height.{small,medium,large}三档高度
input.paddingHorizontal.{small,medium,large}三档横向内边距
input.fontSize.{small,medium,large}三档字号
input.borderRadius / input.borderWidth圆角(跟随 radius.small)与描边宽度
input.focusRingWidth聚焦环宽度
input.clearIconSize清除按钮图标尺寸
color.focus聚焦描边色
color.text.{primary,placeholder,disabled}文字与占位色
color.border.base / color.disabled.border描边色
color.background.default / color.disabled.bg底色

黄金用例对照 ​

case id断言本地测试
input-emits-onvaluechange输入触发 onValueChange("青花瓷")testDefaultIsEditable
clearable-emits-empty-value清除触发 onValueChange("")testClearableShowsClearButtonWhenHasValue
disabled-blocks-input禁用态不触发 onValueChangetestDisabledBlocksInput
readonly-blocks-input只读态不触发 onValueChangetestReadonlyBlocksInput
placeholder-uses-placeholder-token占位文字取占位色令牌testPlaceholderUsesPlaceholderToken
focus-uses-focus-token聚焦消费 focus 令牌并触发 onFocustestFocusUsesFocusToken
small-size-uses-small-tokens小号消费 small 尺寸令牌testSmallSizeUsesSmallTokens
maxlength-truncates-input超出字数截断testMaxlengthTruncatesInput

另有专项:testPasswordTypeIsSecure / testDarkColorsComeFromDarkTokens / testDimensionsAreIdenticalInDarkMode / testInputViewInitAcceptsConstant。

bash
swift test --filter LhInputTests    # macOS 宿主即可跑,无需模拟器