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
lhSize輸入框尺寸,對應到尺寸令牌LhInputSize.medium
disabled是否停用(不觸發任何回呼)Boolfalse
readonly是否唯讀(不觸發 onValueChange,保留聚焦樣式)Boolfalse
clearable是否顯示清除按鈕Boolfalse
maxlength最大字數(超出截斷,黃金用例 maxlength-truncates-input)Int?nil
onValueChange內容變化回呼,payload 為新值((String) -> Void)?nil
lhOnFocus聚焦回呼(() -> Void)?nil
lhOnBlur失焦回呼(() -> 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 令牌並觸發 lhOnFocustestFocusUsesFocusToken
small-size-uses-small-tokens小號消費 small 尺寸令牌testSmallSizeUsesSmallTokens
maxlength-truncates-input超出字數截斷testMaxlengthTruncatesInput

另有專項:testPasswordTypeIsSecure / testDarkColorsComeFromDarkTokens / testDimensionsAreIdenticalInDarkMode / testInputViewInitAcceptsConstant。

bash
swift test --filter LhInputTests    # macOS 宿主即可執行,無需模擬器