跳转到内容

TextField 文本字段 ​

单行或多行的自由文本输入。

用法 ​

root 持有状态,label 与 control 中的 input 各自向它取属性;不传 value 即为非受控,组件自行维护值

组件结构 ​

加粗的是必需部件。

data-scope="text-field":root · label · control · prefix · input · suffix · clear-trigger · count

示例 ​

受控 ​

传入 value 后由宿主决定,组件自身不再修改状态;变化经 value-change 报告,是否写回由宿主决定

当前:曦寒

可清空与字数上限 ​

Control 把输入框与清空按钮圈进同一个框,clearable 使清空按钮可用并接管 Escape,maxLength 同时写为原生 maxlength 与状态机侧截断

2 / 10

禁用与校验态 ​

disabled 与 readOnly 都不可修改值,invalid 只标注 aria-invalid、不拦截输入

变体 ​

variant 决定底色与描边的绘制方式:描边、淡色填底、无框;输入框没有实心档

颜色 ​

tone 决定使用哪族颜色,与 variant 正交;这里固定 subtle 形态,语气的底色差别不必聚焦即可看到

尺寸 ​

size 只改变高度、内边距与字号,标签与清空按钮一起换档;不写即默认档

程序化改值 ​

setValue 直接写值,只受禁用、只读与字数上限约束;clear 执行清空意图,canClear 不成立时不做任何处理

(空)

原生属性 ​

写在 input 部件上的属性直接落到真正的输入框,自动填充与移动端键盘类型由它们决定

事件 ​

值的变化经组件的 value-change,聚焦失焦等原生事件直接写在 input 部件上

还没有事件

框内前后缀 ​

前后缀与输入框同在 control 这一个框里排成一行,共用它的描边与底色

¥元
cm

密码与明暗切换 ​

写在 input 部件上的 type 覆盖默认的 text,明暗由宿主的一个布尔切换

限制可输入的字符 ​

beforeinput 直接写在 input 部件上,非法字符无法进入框,值与框中的内容始终一致

聚焦与选区 ​

input 部件就是一个原生 input,取得它的节点即可聚焦、全选、把光标移到末尾

输入组 ​

圆角槽换为只保留外侧的一组值,中缝用负外边距叠掉一条描边,相邻控件拼为一体

https://
.com

多行与自动增高 ​

input 部件写为 textarea 即多行宿主;autoSize 使高度跟随内容,对象形态固定行数上下限(达到 maxRows 后内部滚动)

0 / 120

设计指引 ​

何时使用 ​

  • 姓名、标题、描述、搜索词等没有固定候选的文本。

何时不用 ​

特性 ​

  • type 覆盖 text / password / email / tel / url / search。
  • clearable 显示清空按钮,maxLength 设置字数上限。
  • 输入部件用 as="textarea" 切换到多行;autoSize 为 true 时随内容增高,也可用 { minRows, maxRows } 限定行数。两个边界必须是大于等于 1 的有限整数,且 minRows 不得大于 maxRows;无效配置会明确失败,不夹取也不沿用旧配置。
  • 自动高度跟随输入、程序化写值与运行期配置变化重新测量。关闭 autoSize、换回单行、替换输入节点或卸载组件时,归还启用前的 block-size 与 overflow-y 内联声明及其 priority;只移除作者原本没有写的声明。
  • 自动高度把一个隐藏 textarea 临时挂到输入框所属 Document,以复制后的排版与宽度计算值取得真实内容高度和单行高度,换算 minRows / maxRows;line-height: normal 不按字号推测。content-box 与 border-box 分别按自己的声明盒计算内距和边框。测量要求 textarea 已连接到带 Window 的 Document,且当前只接受 writing-mode: horizontal-tb;其他书写模式会明确失败,不把物理纵向滚动尺寸误当逻辑块尺寸。
  • prefix / suffix 在框内放置货币符、单位或图标,两段对读屏隐藏。
  • 默认皮肤把控件接入 Field Chrome:Headless 在真实视觉盒、输入、装饰段上分别投影 data-xh-field-chrome、data-xh-field-input、data-xh-field-affix,单行与 textarea 由 data-xh-field-layout 区分。旧 data-multiline / data-auto-resize 视觉钩子已删除,自定义皮肤应读取新的家族角色,不提供双写兼容。
  • 默认即 outline:--xh-bg-canvas 底、--xh-border-control 描边、control 圆角、无阴影,不写 variant 时 root 与 control 都落 data-variant="outline";subtle 为中性填充、ghost 为透明底,两者在悬停与聚焦时浮出描边,聚焦描边一律 --xh-border-control-focus。
  • 清空按钮复用 Action Control 的 field-inset profile 和 has-value 显示策略;粗指针命中区、pressed / focus / forced-colors 均由家族配方提供,适配器不另行计算尺寸或可见性。
  • 开启 clearable 后,清空按钮在空值时收起,只在有值且可编辑时出现;字段聚焦边界平滑过渡。
  • showCount 显示字数部件,数字取 count 与 maxLength,达到上限时换色。
  • 放在 FormFieldGroup 内时,未声明的 disabled / readOnly / required / invalid 从最近的 Field 或 Form 继承;实例显式写 false 时以实例为准。Field 的标签、说明和错误描述链保持挂到 input。
  • 输入组、限制可输入字符由作者组合,组件不预设。

组合 ​

最佳实践 ​

  • type 必须正确:移动端软键盘按它切换,写错会增加用户的按键次数。
  • 密码框的明暗切换按钮要有可访问名称,并在切换后更新。

反模式 ​

  • 用它收集固定格式的分段值(日期、验证码):应使用日期字段、分格输入。
  • 输入时就报格式错误。

API 参考 ​

产物 ​

层值
自定义元素<xh-text-field>
Vue 组件XhTextFieldClearTrigger XhTextFieldControl XhTextFieldCount XhTextFieldInput XhTextFieldLabel XhTextFieldPrefix XhTextFieldRoot XhTextFieldSuffix
组合式函数useTextField
状态机textFieldMachine
皮肤@xihan-ui/styles/text-field.css

Props ​

属性类型必填说明
valuestring受控值;提供后由宿主决定,状态机不自行修改。
defaultValuestring非受控初值。
typeTextFieldType单行宿主的输入类型,默认 text;as 为 textarea 时不发该属性。
placeholderstring
disabledboolean
readOnlyboolean
requiredboolean
invalidboolean
namestring表单字段名;提供后才参与提交。
maxLengthnumber字符数上限。同时落为原生 maxlength 与状态机侧的截断,两者都需要。
clearableboolean开启清空能力:有值时显示清空按钮、Escape 接管。关闭时按钮带 hidden 收起。
showCountboolean显示字数部件:关闭时 count 部件带 hidden 收起。
autoSizeboolean | TextFieldAutoSize多行宿主的自动高度:按横向书写的真实行盒随内容增高;对象形态固定行数上下限。
variantControlVariant形态:outline / subtle / ghost,决定底色与描边的绘制方式。默认 outline。
toneTone语气:brand / neutral / success / warning / danger / info,决定聚焦强调使用哪族颜色。
sizeSize尺寸:sm / md / lg,决定输入框与清空按钮的几何档位。
translationsPartial<TextFieldTranslations>读屏文案;默认英文。
onValueChange(details: TextFieldValueChangeDetails) => void

事件 ​

自定义元素将载荷放在 detail;Vue 使用同名 emit。

事件载荷说明
value-changeTextFieldValueChangeDetails值变化;detail 为 { value: string }

插槽 ​

仅列出带载荷的插槽。

Vue 组件插槽载荷说明
XhTextFieldCountdefaultTextFieldCountSlotProps
XhTextFieldRootdefaultTextFieldRootSlotProps

React 适配器 props ​

只列各组件自己声明的那些:继承自 ComponentPropsWithRef 的 DOM 属性不在其中,根组件上与上面 Props 表同名的也不重复列。Vue 的对应物是上面的插槽表。

React 组件属性类型必填说明
XhTextFieldCountchildrenSlotChildren<TextFieldCountSlotProps>
XhTextFieldInputasTextFieldInputHost输入框渲染为哪个标签,默认 input;写 textarea 即多行宿主,接入 autoSize 自动高度。
XhTextFieldRootchildrenSlotChildren<TextFieldRootSlotProps>

状态 ​

以下名称仅用于内部状态机。

状态:idle

事件:VALUE.SET · VALUE.CLEAR · FORM.RESET · PRESS.START · PRESS.END

判据:canEdit · canClear

connect API ​

getXxxProps() 返回对应部件的宿主属性。

成员类型说明
valuestring
emptyboolean值为空串。作者据此显示占位说明等内容。
disabledboolean
readOnlyboolean
invalidboolean
clearableboolean
atLimitboolean已到达 maxLength:无法再输入,作者据此把字数提示标红。
countnumber当前字数,即 value 的长度。作者用它渲染 count 部件中的数字。
maxLengthnumber | undefined字数上限的原样透传;未设上限时为 undefined,此时只渲染当前字数。
showCountboolean字数部件当前是否显示(开启了 showCount)。
canClearboolean清空按钮当前是否可用(开启 clearable、可编辑、且有值)。
setValue(next: string) => void直接写值,只受 disabled / readOnly 与 maxLength 约束,与 clearable 无关。
clear() => void发起清空意图,受 canClear 约束;无条件清空使用 setValue('')。
autoSizeboolean | TextFieldAutoSize自动高度配置的原样透传;适配器在程序化写值后据此补测一次。
getRootProps() => T['element']
getControlProps() => T['element']视觉盒;提供后由它绘制描边与聚焦环,未提供时输入框自身作为盒。
getLabelProps() => T['label']
getInputProps(props?: TextFieldInputProps) => T['input']传 as: 'textarea' 即多行宿主:去除 type、接入自动高度。
getPrefixProps() => T['element']输入框前的装饰段(货币符、单位、图标);对读屏隐藏,不参与名字链。
getSuffixProps() => T['element']输入框后的装饰段;对读屏隐藏,不参与名字链。
getClearTriggerProps() => T['button']
getCountProps() => T['element']字数部件:承载 count / maxLength 两个数字,未开启 showCount 时带 hidden 收起。

无障碍 ​

键盘 ​

规格出处:W3C APG

按键生效条件行为
Escapefocus in input, clearable 且值非空, not disabled/readOnly清空值;三个条件缺一即不接管该键,交回给外层与浏览器
Enter / Spaceheld in clear-trigger, clearable 且值非空, not disabled/readOnly按住期间清空按钮投影 data-pressed,与指针 :active 同一副按压面;抬起或失焦撤下,值清空后按钮藏起一并撤下。清空按钮不占 Tab 位,键盘这一路只在焦点落到它身上时有面

ARIA ​

以下属性由 connect 生成。

部件属性值
prefixaria-hidden'true'
inputaria-invalid'true' | 'false'
inputaria-labelledbylabel 部件的 id
inputaria-readonly'true' | 'false'
inputaria-required'true' | 'false'
suffixaria-hidden'true'
clear-triggeraria-labellabel.clearTrigger
countaria-hidden'true'

样式参考 ​

皮肤 ​

@xihan-ui/styles/text-field.css 使用 [data-scope="text-field"][data-part="root"] 部件选择器,位于 xihan.components 层。覆盖样式使用 xihan.overrides。

数据属性 ​

由 connect 生成;条件不成立时不输出无值属性。

部件属性值
rootdata-at-max''(条件成立时才出现)
rootdata-disabled''(条件成立时才出现)
rootdata-empty''(条件成立时才出现)
rootdata-invalid''(条件成立时才出现)
rootdata-readonly''(条件成立时才出现)
rootdata-sizeprops.size
rootdata-toneprops.tone
rootdata-variantprops.variant
rootdata-xh-action-owner''
labeldata-disabled''(条件成立时才出现)
controldata-at-max''(条件成立时才出现)
controldata-disabled''(条件成立时才出现)
controldata-empty''(条件成立时才出现)
controldata-invalid''(条件成立时才出现)
controldata-readonly''(条件成立时才出现)
controldata-variantprops.variant
controldata-xh-field-chrome''
controldata-xh-field-sizeprops.size
prefixdata-disabled''(条件成立时才出现)
prefixdata-xh-field-affix'prefix'
inputdata-at-max''(条件成立时才出现)
inputdata-disabled''(条件成立时才出现)
inputdata-empty''(条件成立时才出现)
inputdata-invalid''(条件成立时才出现)
inputdata-xh-field-auto-size''(条件成立时才出现)
inputdata-xh-field-input''
inputdata-xh-field-layout'textarea' | 'single-line'
suffixdata-disabled''(条件成立时才出现)
suffixdata-xh-field-affix'suffix'
clear-triggerdata-pressed''(条件成立时才出现)
clear-triggerdata-xh-action-control''
clear-triggerdata-xh-action-display'has-value'
clear-triggerdata-xh-action-has-value''(条件成立时才出现)
clear-triggerdata-xh-action-profile'field-inset'
clear-triggerdata-xh-action-sizeprops.size
clear-triggerdata-xh-action-variant'ghost'
countdata-at-max''(条件成立时才出现)
countdata-disabled''(条件成立时才出现)

CSS 变量 ​

本组件公开覆盖槽由独立皮肤的实际消费位生成;默认来源、作用部件和状态均与 CSS 同源。

变量部件CSS 属性状态默认来源说明
--xh-text-field-action-bgclear-trigger--xh-ink-surface
background-color
default
xh-ink-surface
--xh-_action-variant-bg-resttext-field 的 clear-trigger 部件 --xh-ink-surface、background-color 覆盖槽。
--xh-text-field-action-bg-activeclear-triggerbackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-bg-pressedtext-field 的 clear-trigger 部件 background-color 覆盖槽。
--xh-text-field-action-bg-hoverclear-triggerbackground-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-_action-variant-bg-hovertext-field 的 clear-trigger 部件 background-color 覆盖槽。
--xh-text-field-action-fgclear-triggercolordefault--xh-fg-mutedtext-field 的 clear-trigger 部件 color 覆盖槽。
--xh-text-field-action-fg-hoverclear-triggercolordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-fg-defaulttext-field 的 clear-trigger 部件 color 覆盖槽。
--xh-text-field-action-font-sizeclear-triggerfont-sizedefault--xh-_text-field-action-font-sizetext-field 的 clear-trigger 部件 font-size 覆盖槽。
--xh-text-field-action-radiusclear-triggerborder-radiusdefault--xh-shape-insettext-field 的 clear-trigger 部件 border-radius 覆盖槽。
--xh-text-field-action-sizeclear-triggerblock-size
inline-size
min-inline-size
default
xh-action-profile=field-inset
--xh-_action-profile-visual-sizetext-field 的 clear-trigger 部件 block-size、inline-size、min-inline-size 覆盖槽。
--xh-text-field-affix-fgprefix
suffix
colorxh-field-affix--xh-fg-mutedtext-field 的 prefix、suffix 部件 color 覆盖槽。
--xh-text-field-affix-fg-disabledprefix
suffix
colordisabled
xh-field-affix
--xh-fg-disabledtext-field 的 prefix、suffix 部件 color 覆盖槽。
--xh-text-field-affix-font-sizeprefix
suffix
font-sizexh-field-affix--xh-_text-field-font-sizetext-field 的 prefix、suffix 部件 font-size 覆盖槽。
--xh-text-field-control-bgcontrolbackground-colorxh-field-chrome--xh-_field-variant-bg-resttext-field 的 control 部件 background-color 覆盖槽。
--xh-text-field-control-bg-disabledcontrolbackground-colordisabled
xh-field-chrome
--xh-_field-variant-bg-disabledtext-field 的 control 部件 background-color 覆盖槽。
--xh-text-field-control-bg-hovercontrolbackground-colordisabled
hover
invalid
loading
not([data-disabled])
not([data-invalid])
not([data-loading])
not([data-readonly])
readonly
xh-field-chrome
--xh-_field-variant-bg-hovertext-field 的 control 部件 background-color 覆盖槽。
--xh-text-field-control-bg-readonlycontrolbackground-colorreadonly
xh-field-chrome
--xh-_field-variant-bg-read-onlytext-field 的 control 部件 background-color 覆盖槽。
--xh-text-field-control-bordercontrolborderxh-field-chrome--xh-_field-variant-border-resttext-field 的 control 部件 border 覆盖槽。
--xh-text-field-control-border-at-maxcontrolborder-colorat-max
invalid
not([data-invalid])
--xh-border-at-limittext-field 的 control 部件 border-color 覆盖槽。
--xh-text-field-control-border-focuscontrolborder-colordisabled
focus-within
not([data-disabled])
xh-field-chrome
--xh-_field-variant-border-focustext-field 的 control 部件 border-color 覆盖槽。
--xh-text-field-control-border-hovercontrolborder-colordisabled
hover
invalid
loading
not([data-disabled])
not([data-invalid])
not([data-loading])
not([data-readonly])
readonly
xh-field-chrome
--xh-_field-variant-border-hovertext-field 的 control 部件 border-color 覆盖槽。
--xh-text-field-control-border-invalidcontrolborder-colorinvalid
xh-field-chrome
--xh-_field-variant-border-invalidtext-field 的 control 部件 border-color 覆盖槽。
--xh-text-field-control-fgcontrolcolorxh-field-chrome--xh-fg-defaulttext-field 的 control 部件 color 覆盖槽。
--xh-text-field-control-gapcontrolgapxh-field-chrome--xh-_text-field-gaptext-field 的 control 部件 gap 覆盖槽。
--xh-text-field-control-hcontrolblock-size
min-block-size
has([data-xh-field-input][data-xh-field-layout='multi-tag'])
has([data-xh-field-input][data-xh-field-layout='single-line'])
has([data-xh-field-input][data-xh-field-layout='textarea'])
xh-field-chrome
xh-field-input
xh-field-layout=multi-tag
xh-field-layout=single-line
xh-field-layout=textarea
--xh-_text-field-htext-field 的 control 部件 block-size、min-block-size 覆盖槽。
--xh-text-field-control-max-hcontrolmax-block-sizehas([data-xh-field-input][data-xh-field-auto-size])
xh-field-auto-size
xh-field-input
--xh-viewport-h-smtext-field 的 control 部件 max-block-size 覆盖槽。
--xh-text-field-control-min-wcontrol
root
min-inline-sizedefault
xh-field-chrome
--xh-control-min-wtext-field 的 control、root 部件 min-inline-size 覆盖槽。
--xh-text-field-control-pxcontrolpadding-inlinexh-field-chrome--xh-_text-field-pxtext-field 的 control 部件 padding-inline 覆盖槽。
--xh-text-field-control-radiuscontrolborder-radiusxh-field-chrome--xh-shape-controltext-field 的 control 部件 border-radius 覆盖槽。
--xh-text-field-control-shadowcontrolbox-shadowxh-field-chromenonetext-field 的 control 部件 box-shadow 覆盖槽。
--xh-text-field-control-wrootinline-size
min-inline-size
default--xh-control-wtext-field 的 root 部件 inline-size、min-inline-size 覆盖槽。
--xh-text-field-count-fgcountcolordefault--xh-fg-mutedtext-field 的 count 部件 color 覆盖槽。
--xh-text-field-count-fg-at-maxcountcolorat-max--xh-fg-warningtext-field 的 count 部件 color 覆盖槽。
--xh-text-field-count-fg-disabledcountcolordisabled--xh-fg-disabledtext-field 的 count 部件 color 覆盖槽。
--xh-text-field-count-font-sizecountfont-sizedefault--xh-_text-field-action-font-sizetext-field 的 count 部件 font-size 覆盖槽。
--xh-text-field-gaprootgapdefault--xh-space-1text-field 的 root 部件 gap 覆盖槽。
--xh-text-field-icon-sizecontrol
root
--xh-icon-sizedefault
size=lg
size=sm
xh-field-chrome
--xh-_field-size-glyph-size
--xh-glyph-size-lg
--xh-glyph-size-md
--xh-glyph-size-sm
text-field 的 control、root 部件 --xh-icon-size 覆盖槽。
--xh-text-field-input-autofill-bginputbox-shadow-webkit-autofill
autofill
xh-field-input
--xh-bg-canvastext-field 的 input 部件 box-shadow 覆盖槽。
--xh-text-field-input-autofill-fginput-webkit-text-fill-color-webkit-autofill
autofill
xh-field-input
--xh-fg-defaulttext-field 的 input 部件 -webkit-text-fill-color 覆盖槽。
--xh-text-field-input-fginputcolorxh-field-input--xh-fg-defaulttext-field 的 input 部件 color 覆盖槽。
--xh-text-field-input-font-sizeinputfont-sizexh-field-input--xh-_text-field-font-sizetext-field 的 input 部件 font-size 覆盖槽。
--xh-text-field-label-fglabelcolordefault--xh-fg-defaulttext-field 的 label 部件 color 覆盖槽。
--xh-text-field-label-fg-disabledlabelcolordisabled--xh-fg-subtletext-field 的 label 部件 color 覆盖槽。
--xh-text-field-label-font-sizelabelfont-sizedefault--xh-text-label-sizetext-field 的 label 部件 font-size 覆盖槽。
--xh-text-field-label-font-weightlabelfont-weightdefault--xh-text-label-weighttext-field 的 label 部件 font-weight 覆盖槽。
--xh-text-field-placeholder-fginputcolorplaceholder
xh-field-input
--xh-fg-subtletext-field 的 input 部件 color 覆盖槽。
--xh-text-field-textarea-pyinputpadding-blockxh-field-input
xh-field-layout=textarea
--xh-space-2text-field 的 input 部件 padding-block 覆盖槽。

动效 ​

动效角色:按压 · 状态(见动效规范)。

本组件皮肤不含过渡与关键帧,也没有脚本驱动的动效:状态一变,外观立即到位。

RTL ​

皮肤用逻辑属性排布(inline-start 一族),dir="rtl" 下自动镜像。

Released under The MIT License