--- title: TextField 文本输入框 description: 便于组合的文本字段,包含标签、说明与内联校验。 component: textfield group: 组件 --- ## 用法 ```vue ``` ## 组件结构 ```vue ``` > **TextField** 将标签、输入、说明与错误信息整合为单个无障碍组件。若只需独立输入,请使用 **[Input](/components/input)** 或 **[TextArea](/components/textarea)**。 ## 示例 ### 表面样式 在 [Surface](/components/surface) 内使用时,请在 Input 或 TextArea 组件上使用 `variant="secondary"`,以应用适合表面背景的低强调变体。 ### 带描述 ### 必填字段 ### 禁用状态 ### 宽度充满 ### 表单校验 将 `is-invalid` 与 `FieldError` 配合使用,以展示校验消息。`is-invalid` 只负责错误展示;阻止提交请使用 `validate` 或 Form 的 `validation-errors`。 ### 受控组件 使用 `v-model` 控制值,以同步计数器、预览或格式化逻辑。 ### 错误信息 ### 文本域 多行内容请使用 [TextArea](/components/textarea),而非 [Input](/components/input)。 ### Input 类型 ### 渲染函数 上游 `render` 自定义根元素;Vue 将 `class`/`data-*` 等透传到 TextField 根节点,暂不支持替换根元素类型。 ## 自定义样式 ### Tailwind CSS ### 全局 CSS 若要自定义组件类,可使用 `@layer components` 指令。 ```css @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](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。 ### CSS 类 - `.hv-textfield` – 根容器,样式极少(`flex flex-col gap-1`) > 变体映射 `textFieldVariants` 从 `@heroui-vue/styles` 公开(`@heroui-vue/components` 再导出同一函数与 `TextFieldVariants` 类型);`full-width` 输出 `.hv-textfield--full-width`,组件根类名由该映射生成。 > **提示:** 子组件([Label](/components/label)、[Input](/components/input)、[TextArea](/components/textarea)、[Description](/components/description)、[FieldError](/components/field-error))各自拥有 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) | ### 组合子组件 - **[Label](/components/label)** — 字段标签 - **[Input](/components/input)** — 单行文本输入 - **[TextArea](/components/textarea)** — 多行文本输入 - **[Description](/components/description)** — 辅助说明 - **[FieldError](/components/field-error)** — 校验错误 ```vue ``` ### 作用域插槽状态 默认插槽提供以下状态,可用于条件渲染: | 参数 | 类型 | 描述 | | ------------------ | ---------- | ------------ | | `isDisabled` | `boolean` | 是否禁用 | | `isReadOnly` | `boolean` | 是否只读 | | `isRequired` | `boolean` | 是否必填 | | `isInvalid` | `boolean` | 是否无效 | | `validationErrors` | `string[]` | 当前错误列表 | 与上游 `TextFieldRenderProps` 的差异:Vue 使用作用域插槽而非 render prop;无 `isFocusWithin` / `isFocusVisible`(聚焦状态由控件自身管理)。 ## 相关组件 [Input 输入框](./input) · [TextArea 多行文本框](./textarea) · [Fieldset 字段组](./fieldset)