Field
A labeled control with an optional hint and error under it. FieldHint and FormError are those two lines on their own, for a control outside a Field.
import { Field, FieldHint, FormError } from '@adecore/ui';import { Field, FieldHint, FormError, Input, Segmented } from '@adecore/ui';
import { useState } from 'react';
export default function FieldDemo() {
const [name, setName] = useState('feature/export');
const taken = name.trim() === 'main';
const [ground, setGround] = useState('dark');
return (
<div className="flex w-80 flex-col gap-4">
<Field label="Branch name" hint="Lowercase, with slashes for a folder." error={taken ? 'A branch called main already exists.' : null}>
<Input mono value={name} onChange={(event) => setName(event.target.value)} />
</Field>
<Field group label="Ground" hint="What every frame is drawn on.">
<Segmented
label="Ground"
value={ground}
onValueChange={setGround}
options={[
{ id: 'dark', label: 'Dark' },
{ id: 'light', label: 'Light' }
]}
/>
</Field>
<div>
<Input aria-label="Remote URL" placeholder="https://" />
<FieldHint>A hint on its own keeps a margin above it.</FieldHint>
</div>
<FormError>The remote refused the push.</FormError>
</div>
);
}Type main into the branch name to see the error. An Input or TextArea inside a Field connects itself: label, aria-describedby and aria-invalid. Any other control needs its own accessible name.
A group
A <label> cannot point at a Segmented, a Select, ChoiceCards or a compound control. group draws the same label, hint and error, and makes the Field a role="group" named by its label. The control inside still needs its own accessible name, and an Input inside is not connected.
<Field group label="Folder" hint="The project is made in a new folder here.">
<div className="flex items-center gap-2">
<div className="field flex min-w-0 flex-1 items-center text-text-muted">{path}</div>
<Button variant="secondary" onClick={choose}>Choose…</Button>
</div>
</Field>Beside the control
orientation="horizontal" puts the label in a 112 pixel column beside the control, so the controls of a dialog line up. Hint and error stay under the control. The label aligns with the control's first line.
import { Field, Input, Segmented, TextArea } from '@adecore/ui';
import { useState } from 'react';
export default function FieldHorizontalDemo() {
const [kind, setKind] = useState('service');
return (
<div className="flex w-120 flex-col gap-3">
<Field orientation="horizontal" label="Name">
<Input defaultValue="Run server" />
</Field>
<Field orientation="horizontal" group label="Kind" hint="Keeps running until you stop it.">
<Segmented
label="Kind"
value={kind}
onValueChange={setKind}
options={[
{ id: 'service', label: 'Service' },
{ id: 'task', label: 'Task' }
]}
/>
</Field>
<Field orientation="horizontal" label="Command">
<TextArea rows={2} mono defaultValue="php -S 0.0.0.0:8000 -t public dev/server.php" />
</Field>
</div>
);
}Props
| Prop | Type | Default | |
|---|---|---|---|
children | ReactNode | Required. The control. | |
label | ReactNode | A SectionLabel above the control. | |
hint | ReactNode | ||
error | ReactNode | Marks the control invalid. null or '' shows nothing. | |
group | boolean | false | |
orientation | 'vertical' | 'horizontal' | 'vertical' | |
className | string | ||
ref | Ref<HTMLDivElement> |
FieldHint and FormError
FieldHint is a <p> in the hint's style. FormError is a <p> with role="alert", for an error of a whole form or dialog. Both take className, ref and render.
FieldProps, FieldHintProps and FormErrorProps are exported types.