TextField 文本输入框更新
便于组合的文本字段,包含标签、说明与内联校验。
引入
import { TextField } from '@heroui/react';用法
import {Input, Label, TextField} from "@heroui/react";
export function Basic() {
return (
<TextField className="w-full max-w-64" name="email" type="email">
<Label>邮箱</Label>
<Input placeholder="输入你的邮箱" />
</TextField>
);
}组件结构
import {TextField, Label, Input, Description, FieldError} from '@heroui/react';
export default () => (
<TextField>
<Label />
<Input />
<Description />
<FieldError />
</TextField>
)TextField 将标签、输入、说明与错误信息整合为单个无障碍组件。若只需独立输入,请使用 Input 或 TextArea。
带说明
为你的账户选择一个唯一的用户名
import {Description, Input, Label, TextField} from "@heroui/react";
export function WithDescription() {
return (
<TextField className="w-full max-w-64" name="username">必填字段
此字段为必填项
import {Description, Input, Label, TextField} from "@heroui/react";
export function Required() {
return (
<TextField isRequired className="w-full max-w-64" name="fullName">校验
使用 isInvalid 与 FieldError 展示校验信息。
为你的资料选择一个唯一的用户名。
至少 20 个字符 (0/20).
"use client";
import {Description, FieldError, Input, Label, TextArea, TextField} from "@heroui/react";
import React from "react";
受控
通过受控 value 同步计数器、预览或格式化。
字符数: 0
字符数: 0 / 200
"use client";
import {Description, Input, Label, TextArea, TextField} from "@heroui/react";
import React from "react";
错误信息
请输入有效的邮箱地址
import {FieldError, Input, Label, TextField} from "@heroui/react";
export function WithError() {
return (
<TextField isInvalid className="w-full max-w-64" name="email" type="email">禁用状态
此字段不可编辑
import {Description, Input, Label, TextField} from "@heroui/react";
export function Disabled() {
return (
<TextField isDisabled className="w-full max-w-64" name="accountId" value="USR-12345">TextArea
最多 500 个字符
import {Description, Label, TextArea, TextField} from "@heroui/react";
export function TextAreaExample() {
return (
<TextField className="w-full max-w-64" name="message">Input 类型
import {Input, Label, TextField} from "@heroui/react";
export function InputTypes() {
return (
<div className="flex w-full max-w-64 flex-col gap-4">全宽
密码长度必须超过 8 个字符
import {FieldError, Input, Label, TextField} from "@heroui/react";
export function FullWidth() {
return (
<div className="w-[400px] space-y-4">在 Surface 内
置于 Surface 中时,请在 Input 或 TextArea 上使用 variant="secondary",以应用适合表面背景的弱强调变体。
我们绝不会与他人分享此信息
至少 4 行
import {Description, Input, Label, Surface, TextArea, TextField} from "@heroui/react";
export function OnSurface() {
return (
<Surface className="flex w-full min-w-[340px] flex-col gap-4 rounded-3xl p-6">自定义渲染函数
"use client";
import {Input, Label, TextField} from "@heroui/react";
export function CustomRenderFunction() {样式
传入 Tailwind CSS 类
import {TextField, Label, Input, Description} from '@heroui/react';
function CustomTextField() {
return (
<TextField className="gap-2 rounded-xl border border-border/60 bgsurface p-4 shadow-sm">
<Label className="text-sm font-semibold text-default-700">
Project name
</Label>
<Input className="rounded-lg border border-border/60 bgsurface px-3 py-2" />
<Description className="text-xs text-default-500">
Keep it short and memorable.
</Description>
</TextField>
);
}自定义组件类
TextField 默认样式很少。覆盖 .textfield 类即可自定义容器样式。
@layer components {
.textfield {
@apply flex flex-col gap-1;
}
/* 无效时自动隐藏说明 */
.textfield[data-invalid="true"] [data-slot="description"],
.textfield[aria-invalid="true"] [data-slot="description"] {
@apply hidden;
}
/* Description 默认内边距 */
.textfield [data-slot="description"] {
@apply px-1;
}
}CSS 类
.textfield– 根容器,样式极少(flex flex-col gap-1)
提示: 子组件(Label、Input、TextArea、Description、FieldError)各自拥有 CSS 类与样式,定制方式请参见对应文档。
交互状态
TextField 会根据状态自动管理以下 data 属性:
- 无效:
[data-invalid="true"]或[aria-invalid="true"]— 无效时自动隐藏 description 插槽 - 禁用:
[data-disabled="true"]— 在isDisabled为 true 时应用 - 焦点在内部:
[data-focus-within="true"]— 任一子级 input 聚焦时应用 - 可见焦点:
[data-focus-visible="true"]— 键盘导航产生可见焦点时应用
更多属性可通过 render prop 获取(见下文 TextFieldRenderProps)。
API 参考
TextField Props
继承 React Aria TextField 的全部 props。
Base Props
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
children | React.ReactNode | (values: TextFieldRenderProps) => React.ReactNode | - | 子组件(Label、Input 等)或渲染函数。 |
className | string | (values: TextFieldRenderProps) => string | - | 用于样式的 CSS 类,支持渲染 prop。 |
style | React.CSSProperties | (values: TextFieldRenderProps) => React.CSSProperties | - | 行内样式,支持渲染 prop。 |
fullWidth | boolean | false | TextField 是否占满容器宽度。 |
id | string | - | 元素的唯一 id。 |
render | DOMRenderFunction<keyof React.JSX.IntrinsicElements, TextFieldRenderProps> | - | 使用自定义渲染函数覆盖默认 DOM 元素。 |
Validation Props
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
isRequired | boolean | false | 提交表单前是否必须填写。 |
isInvalid | boolean | - | 当前值是否无效。 |
validate | (value: string) => ValidationError | true | null | undefined | - | 自定义校验函数。 |
validationBehavior | 'native' | 'aria' | 'native' | 使用原生 HTML 表单校验还是 ARIA 属性。 |
validationErrors | string[] | - | 服务端校验错误。 |
Value Props
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
value | string | - | 当前值(受控)。 |
defaultValue | string | - | 默认值(非受控)。 |
onChange | (value: string) => void | - | 值变化时调用的事件处理函数。 |
State Props
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
isDisabled | boolean | - | 是否禁用输入。 |
isReadOnly | boolean | - | 是否可选中但不可修改。 |
Form Props
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
name | string | - | 用于 HTML 表单提交的 input 名称。 |
autoFocus | boolean | - | 是否在挂载时自动聚焦。 |
Accessibility Props
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
aria-label | string | - | 无可见标签时的无障碍标签。 |
aria-labelledby | string | - | 用于标注该字段的元素 id。 |
aria-describedby | string | - | 用于描述该字段的元素 id。 |
aria-details | string | - | 提供附加详情的元素 id。 |
Composition Components
TextField 与以下独立组件配合使用,请直接按需引入并组合:
- Label —
@heroui/react的字段标签组件 - Input —
@heroui/react的单行文本输入 - TextArea —
@heroui/react的多行文本输入 - Description —
@heroui/react的辅助说明组件 - FieldError —
@heroui/react的校验错误信息组件
这些组件各自有独立的 props API,请在 TextField 内直接使用:
<TextField isRequired isInvalid={hasError}>
<Label>Email Address</Label>
<Input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
<Description>We'll never share your email.</Description>
<FieldError>Please enter a valid email address.</FieldError>
</TextField>TextFieldRenderProps
对 className、style 或 children 使用渲染 prop 时,可使用以下值:
| Prop | 类型 | 描述 |
|---|---|---|
isDisabled | boolean | 字段是否禁用。 |
isInvalid | boolean | 字段当前是否无效。 |
isReadOnly | boolean | 字段是否只读。 |
isRequired | boolean | 字段是否必填。 |
isFocused | boolean | 字段是否聚焦(已弃用 — 请使用 isFocusWithin)。 |
isFocusWithin | boolean | 是否有任一子元素聚焦。 |
isFocusVisible | boolean | 是否为可见键盘焦点。 |





