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
onFocus聚焦回调(() -> Void)?nil
onBlur失焦回调(() -> 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 令牌并触发 onFocustestFocusUsesFocusToken
maxlength-truncates-input超出字数截断testMaxlengthTruncatesInput

另有专项:testLayoutUsesTextareaTokens / testReadonlyBlocksInputButKeepsFocusStyle / testDarkColorsComeFromDarkTokens / testDimensionsAreIdenticalInDarkMode / testTextareaViewInitAcceptsConstant。

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