字段与文本输入
把一个 TextInput、Select 或 Checkbox 放入 Field。点击标签即可聚焦或激活控件。需要必填标记时,在 Field 上设置 required,浏览器也会执行原生必填校验。
Live example
Open playground ↗Include the file extension.
App.tsx
'use client'
import { Field, TextInput } from '@ancrel/ui'
import '@ancrel/tokens/css'
import '@ancrel/ui/styles.css'
export default function App() {
return (
<Field label="File name" description="Include the file extension." required>
<TextInput name="filename" placeholder="notes.txt" leading={<span aria-hidden="true">/</span>} />
</Field>
)
}
Expected behavior
The File name label focuses a required text input. Its hint asks for the file extension. Typing a name makes the native required constraint pass.
From the workspace root, build @ancrel/tokens and @ancrel/ui, then run pnpm --filter @ancrel/example-field test.
说明与校验
用 description 解释输入格式,用 error 说明如何纠正当前值。两者同时存在时,控件依次关联说明和错误。清除 error 后,读屏软件仍能读到说明。可以在 TextInput 的 leading 或 trailing 插槽中放置单位或路径前缀。
State matrix
| State | Theme / density |
|---|---|
| default | dark/compact, dark/comfortable, light/compact, light/comfortable |
设计令牌
--ac-sys-action-focus
接口
Props
| Prop | Type | Required | Default |
|---|---|---|---|
children | ReactNode | Yes | — |
description | ReactNode | No | — |
error | ReactNode | No | — |
key | React.Key | null | undefined | No | — |
label | ReactNode | Yes | — |
ref | React.Ref<HTMLDivElement> | undefined | No | — |
required | boolean | No | — |
Constraints
- Use one TextInput, Select or Checkbox as the field control.
- Set required on Field to announce the native requirement and show the visible marker.
Anti-patterns
- Using a placeholder as the only label.
- Repeating error text in a separate unlinked message.