Files
heroui-vue/apps/docs/components/text-field.md
T

12 KiB
Raw Blame History

title, description, component, group
title description component group
TextField 文本输入框 便于组合的文本字段,包含标签、说明与内联校验。 textfield 组件

用法

<script setup lang="ts">
import { TextField, Label, Input } from "@heroui-vue/components";
</script>

组件结构

<script setup lang="ts">
import {
  TextField,
  Label,
  Input,
  Description,
  FieldError,
} from "@heroui-vue/components";
</script>

<template>
  <TextField>
    <Label />
    <Input />
    <Description />
    <FieldError />
  </TextField>
</template>

TextField 将标签、输入、说明与错误信息整合为单个无障碍组件。若只需独立输入,请使用 Input 或 TextArea。

示例

表面样式

在 Surface 内使用时,请在 Input 或 TextArea 组件上使用 variant="secondary",以应用适合表面背景的低强调变体。

带描述

必填字段

禁用状态

宽度充满

表单校验

将 is-invalid 与 FieldError 配合使用,以展示校验消息。is-invalid 只负责错误展示;阻止提交请使用 validate 或 Form 的 validation-errors。

受控组件

使用 v-model 控制值,以同步计数器、预览或格式化逻辑。

错误信息

文本域

多行内容请使用 TextArea,而非 Input。

Input 类型

渲染函数

上游 render 自定义根元素;Vue 将 class/data-* 等透传到 TextField 根节点,暂不支持替换根元素类型。

自定义样式

Tailwind CSS

全局 CSS

若要自定义组件类,可使用 @layer components 指令。

@layer components {
  .hv-textfield {
    @apply flex flex-col gap-1;
  }

  /* When invalid, the description is hidden automatically */
  .hv-textfield[data-invalid="true"] [data-slot="description"],
  .hv-textfield[aria-invalid="true"] [data-slot="description"] {
    @apply hidden;
  }

  /* Description has default padding */
  .hv-textfield [data-slot="description"] {
    @apply px-1;
  }
}

样式参考

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 hv-。

CSS 类

  • .hv-textfield – 根容器,样式极少(flex flex-col gap-1)

变体映射 textFieldVariants 从 @heroui-vue/styles 公开(@heroui-vue/components 再导出同一函数与 TextFieldVariants 类型);full-width 输出 .hv-textfield--full-width,组件根类名由该映射生成。

提示: 子组件(Label、Input、TextArea、Description、FieldError)各自拥有 CSS 类与样式,定制方式请参见对应文档。

交互状态

TextField 会根据状态自动管理以下 data 属性:

  • 无效:[data-invalid="true"] — 无效时自动隐藏 description 插槽
  • 禁用:[data-disabled="true"] — 在 is-disabled 为 true 时应用
  • 必填:[data-required="true"]
  • 只读:[data-readonly="true"]

API 参考

TextField

Prop 类型 默认值 描述
默认插槽 Component - 子组件(Label、Input、Description、FieldError 等)
validationErrors string | string[] - 字段级外部错误,与 Form 同名错误合并
autoFocus boolean false 挂载后聚焦真实文本控件
modelValue string - 当前值(受控;v-model)
defaultValue string - 默认值(非受控)
name string - 用于 HTML 表单提交的 input 名称
id string - 控件 id;子控件显式 id 优先
type string - 下发到 Input 的 type
isDisabled boolean false 是否禁用输入
isReadOnly boolean false 只读
isRequired boolean false 提交表单前是否必须填写
isInvalid boolean - 错误展示状态;不单独阻止提交
validate (value: string) => string | string[] | true | null | undefined - 同步校验函数
validationBehavior 'native' | 'aria' 继承 Form,否则 'native' 原生约束拦截或仅 ARIA 语义
fullWidth boolean false 是否占满容器宽度
variant 'primary' | 'secondary' - 视觉变体,继承到子控件
minLength / maxLength / pattern number / number / string - 下发到真实文本控件的原生约束
autoComplete string - 浏览器自动完成提示(下发 autocomplete)
inputMode string - 移动键盘类型提示(下发 inputmode)
form string - 关联外部 form 的 id(下发到真实控件)
class string | Array | Object - 附加 CSS 类(Vue class 绑定)

值使用 v-model(update:modelValue)。受控值由父级决定,拒绝更新时恢复实际 DOM 值。name、原生约束、autoComplete / inputMode / form 与 ARIA 落在真实 input/textarea 上。服务端/外部错误既可由字段 validationErrors(字符串或数组)传入,也可由 Form 的 validation-errors 按 name 映射。

事件

事件 参数 描述
update:modelValue string 值变化时发出;配合 v-model

暴露方法

通过模板 ref 调用:

名称 类型 描述
focus () => void 聚焦当前文本控件
element HTMLInputElement | HTMLTextAreaElement | undefined 底层控件(模板 ref 的公开实例会自动解包内部 Ref)

组合子组件

<script setup lang="ts">
import {
  TextField,
  Label,
  Input,
  Description,
  FieldError,
} from "@heroui-vue/components";
import { ref } from "vue";

const email = ref("");
const hasError = ref(false);
</script>

<template>
  <TextField v-model="email" :is-invalid="hasError" is-required>
    <Label>邮箱地址</Label>
    <Input type="email" placeholder="you@example.com" />
    <Description>我们不会分享你的邮箱。</Description>
    <FieldError>请输入有效的邮箱地址。</FieldError>
  </TextField>
</template>

作用域插槽状态

默认插槽提供以下状态,可用于条件渲染:

参数 类型 描述
isDisabled boolean 是否禁用
isReadOnly boolean 是否只读
isRequired boolean 是否必填
isInvalid boolean 是否无效
validationErrors string[] 当前错误列表

Vue 使用类型可解析的作用域插槽;TextFieldRenderProps 包含 value、isFocusWithin、isFocusVisible 与 validationDetails。组内焦点移动保持 focus-within,移出整个字段才清除。

相关组件

Input 输入框 · TextArea 多行文本框 · Fieldset 字段组

字段级错误与焦点

autoFocus?: boolean 在挂载后聚焦真实 input/textarea(禁用控件不聚焦),不落到字段 div。默认插槽提供 value、isFocusWithin、isFocusVisible、validationErrors 和 validationDetails;根的 data-focus-within / data-focus-visible 与实际组内焦点和浏览器判定同步。

validationErrors?: string | string[] 用于字段级外部错误;显示顺序为字段错误、Form 中该 name 的错误、已触发的 validate/原生错误,字段与 Form 的重复消息去重。native 策略下外部错误写入真实控件的 customValidity,aria 策略只显示语义错误。isInvalid 仍只控制展示,不单独设置 customValidity。

改变值的输入事件会忽略当前字段错误响应与 Form 错误响应,reset 也会清除它们;新数组/对象引用视为新响应,重新显示。相同响应引用的原地修改不用于重新投递已忽略的错误。字段级响应只影响当前字段,Form 的其他字段错误保留。FieldError 的 validationDetails 是只读的原生 ValidityState 标志快照,不是第二套字段值。