# 字段与文本输入

把一个 `TextInput`、`Select` 或 `Checkbox` 放入 `Field`。点击标签即可聚焦或激活控件。需要必填标记时，在 `Field` 上设置 `required`，浏览器也会执行原生必填校验。

```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:

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` 插槽中放置单位或路径前缀。

* default — dark/compact, dark/comfortable, light/compact, light/comfortable

## 设计令牌

`--ac-sys-action-focus`

| `Context` | `--ac-sys-action-focus` |
| - | - |
| `dark/compact` | `#cdb2ea` |
| `dark/comfortable` | `#cdb2ea` |
| `light/compact` | `#684a84` |
| `light/comfortable` | `#684a84` |

## 接口

| `Prop` | `Type` | `Required` | `Default` |
| - | - | - | - |
| `children` | `ReactNode` | `true` | `—` |
| `description` | `ReactNode` | `false` | `—` |
| `error` | `ReactNode` | `false` | `—` |
| `key` | `React.Key \| null \| undefined` | `false` | `—` |
| `label` | `ReactNode` | `true` | `—` |
| `ref` | `React.Ref<HTMLDivElement> \| undefined` | `false` | `—` |
| `required` | `boolean` | `false` | `—` |

```ts
import * as React from 'react';
import type { FieldProps } from './types.js';
export declare const Field: React.ForwardRefExoticComponent<FieldProps & React.RefAttributes<HTMLDivElement>>;
```
