Files
heroui-vue/apps/docs/components/text-field.md
T
maoyingjie dc859fb8ae feat(styles): export shared field variant maps and restore the border fallback
HD-05 scope, against heroui ce90ea3 styles sources:

- add labelVariants/descriptionVariants/fieldErrorVariants/
  textFieldVariants/switchGroupVariants (+ VariantProps types) to
  @heroui-vue/styles and re-export them from @heroui-vue/components;
  variant keys, defaultVariants and slot sets match the pinned
  *.styles.ts files key for key (hv- namespace only)
- Label/Description/FieldError/useTextField/SwitchGroup now consume
  those mappings instead of inline class strings, so the state classes
  have a single source; rendered class output is unchanged and pinned
  by tests/shared-field-styles.test.ts (mapping output <-> component
  output)
- restore the global border-color fallback
  (*, ::after, ::before, ::backdrop, ::file-selector-button ->
  border-color: var(--border, currentColor)) in a new base/base.css,
  imported layer(base) right after preflight like the pinned entry:
  a low-priority default that components, utilities and consumer CSS
  override without !important
- view-transition globals from the pinned base.css stay unported: no
  View Transitions API usage exists in the library or docs site
- shared-styles package contract: packed runtime exports from both
  entries, variant-output assertions, CSS tokens incl. the fallback,
  strict nodenext+bundler type contract, and a real-browser consumer
  fixture (light/dark --border, explicit colors, consumer overrides,
  currentColor fallback) wired into check-package.mjs
- docs: styling layers/border fallback/exports notes, five component
  API pages, compatibility matrix and upstream source records

Full suite 111 files / 1634 tests, typecheck and build:docs pass;
shared-styles + api-root-aliases + handbook-styles verified against
the final tarballs.
2026-10-28 23:36:59 +08:00

11 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 等)
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 上。服务端/外部错误通过 Form 的 validation-errors(按 name 映射)接入,不是 TextField 的 validationErrors 数组。

事件

事件 参数 描述
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[] 当前错误列表

与上游 TextFieldRenderProps 的差异:Vue 使用作用域插槽而非 render prop;无 isFocusWithin / isFocusVisible(聚焦状态由控件自身管理)。

相关组件

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