Skip to content

Textarea 多行輸入框 ​

受控多行輸入框。@Binding var value 雙向綁定,疊加佔位提示與字數計數。

契約源:lh-ui-core/specs/textarea.json · 黃金用例:lh-ui-core/cases/textarea.json(6 條,已翻譯為 XCTest,macOS 宿主即可執行)

基本形態 ​

最小高度取 textarea.minHeight,內邊距取 textarea.paddingHorizontal / paddingVertical, 聚焦時描邊換 color.focus。

9:41Textarea 基本

HTML 模擬預覽(iOS / SwiftUI 外觀),非真機渲染

swift
@State private var text = ""

LhTextarea(value: $text, placeholder: "請輸入內容") { newValue in
  // onValueChange
}

字數計數 ​

showCount = true 時右下角顯示計數:設了 maxlength 顯示 目前/上限(黃金用例 showcount-renders-count),未設則只顯示目前長度(showcount-without-maxlength-renders-length-only)。

9:41Textarea 計數
3/10

HTML 模擬預覽(iOS / SwiftUI 外觀),非真機渲染

swift
LhTextarea(value: $text, showCount: true, maxlength: 10)

API ​

Props ​

屬性說明類型預設值
value輸入內容(受控雙向綁定)String—
placeholder佔位提示文字String""
rows期望行數(影響最小高度)Int3
disabled是否停用(不觸發任何回呼)Boolfalse
readonly是否唯讀(不觸發 onValueChange,保留聚焦樣式)Boolfalse
showCount是否顯示字數計數Boolfalse
maxlength最大字數(超出截斷)Int?nil
onValueChange內容變化回呼,payload 為新值((String) -> Void)?nil
lhOnFocus聚焦回呼(() -> Void)?nil
lhOnBlur失焦回呼(() -> Void)?nil

顏色 ​

狀態描邊文字 / 佔位
預設color.border.base(textarea.borderWidth)color.text.primary / color.text.placeholder
聚焦color.focus(textarea.focusRingWidth)同上
停用color.disabled.bordercolor.disabled.text

令牌對照 ​

令牌用途
textarea.minHeight最小高度
textarea.paddingHorizontal / textarea.paddingVertical內邊距
textarea.fontSize字號
textarea.countFontSize計數文字字號
textarea.borderRadius / textarea.borderWidth圓角與描邊寬度
textarea.focusRingWidth聚焦環寬度
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
disabled-blocks-input停用態不觸發 onValueChangetestDisabledBlocksInput
showcount-renders-count計數顯示 3/10 並消費計數字號令牌testShowCountRendersCount
showcount-without-maxlength-renders-length-only無上限時只顯示 3testShowCountWithoutMaxlengthRendersLengthOnly
focus-uses-focus-token聚焦消費 focus 令牌並觸發 lhOnFocustestFocusUsesFocusToken
maxlength-truncates-input超出字數截斷testMaxlengthTruncatesInput

另有專項:testLayoutUsesTextareaTokens / testReadonlyBlocksInputButKeepsFocusStyle / testDarkColorsComeFromDarkTokens / testDimensionsAreIdenticalInDarkMode / testTextareaViewInitAcceptsConstant。

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