Field
A form field that wires label, control, description and error message together; validation stays external.
Installation
Add component
pnpm dlx @elyri/cli@latest add fieldExamples
Basic
Label wires the control automatically, Description adds a hint.
展示在仓库首页与侧边栏。
import { Field } from './components/elyri/Field';
import { Input } from './components/elyri/Input';
export function Example() {
return (
<Field>
<Field.Label>项目名称</Field.Label>
<Input placeholder="elyri-ui" />
<Field.Description>展示在仓库首页与侧边栏。</Field.Description>
</Field>
);
}Invalid
invalid drives the red border and aria-invalid; render Message conditionally.
我们不会公开你的邮箱。
import { useState } from 'react';
import { Field } from './components/elyri/Field';
import { Input } from './components/elyri/Input';
export function Example() {
const [value, setValue] = useState('');
const invalid = value !== '' && !value.includes('@');
return (
<Field invalid={invalid}>
<Field.Label>邮箱</Field.Label>
<Input value={value} onChange={(event) => setValue(event.target.value)} placeholder="you@example.com" />
<Field.Description>我们不会公开你的邮箱。</Field.Description>
{invalid && <Field.Message>请输入包含 @ 的邮箱地址。</Field.Message>}
</Field>
);
}Any control
Input / Textarea / Select / RadioGroup all hook into the wiring.
Pick one
import { Field } from './components/elyri/Field';
import { RadioGroup } from './components/elyri/Radio';
import { Select } from './components/elyri/Select';
import { Textarea } from './components/elyri/Textarea';
export function Example() {
return (
<div>
<Field required>
<Field.Label>水果</Field.Label>
<Select placeholder="Pick one">
<Select.Trigger />
<Select.Content>
<Select.Item value="apple">Apple</Select.Item>
<Select.Item value="banana">Banana</Select.Item>
</Select.Content>
</Select>
</Field>
<Field>
<Field.Label>备注</Field.Label>
<Textarea placeholder="可选" />
</Field>
<Field>
<Field.Label>尺寸偏好</Field.Label>
<RadioGroup defaultValue="md">
<RadioGroup.Radio value="sm">小</RadioGroup.Radio>
<RadioGroup.Radio value="md">中</RadioGroup.Radio>
</RadioGroup>
</Field>
</div>
);
}Disabled
Field's disabled propagates to controls that do not set their own.
整域禁用时标签与描述同步变灰。
import { Field } from './components/elyri/Field';
import { Input } from './components/elyri/Input';
export function Example() {
return (
<Field disabled>
<Field.Label>项目名称</Field.Label>
<Input placeholder="不可编辑" />
<Field.Description>整域禁用时标签与描述同步变灰。</Field.Description>
</Field>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
invalid | boolean | false | Invalid state: red border plus aria-invalid on the control |
required | boolean | — | Required state: aria-required for controls without a native required |
disabled | boolean | — | Disabled state: applies to controls that do not set their own |
Field.Label | label 元素 | — | The label; htmlFor points at the control automatically |
Field.Description | p 元素 | — | Hint text; registered into aria-describedby |
Field.Message | p 元素 | — | Error text; render it conditionally with invalid, registered into aria-describedby |