Field
Lays out a form control with a label, a validation message, a hint and a required indicator, and wires them to the control.
- Validation: validationMessage with a validationState (error, warning, success, none), or error, the shorthand for an error message. The message renders below the control with the state's icon and color, followed by the hint (which stays visible); both describe the control. Only the error state marks the control aria-invalid; error and warning messages are announced (role="alert"). The root carries data-validation-state and data-orientation.
- orientation="horizontal" puts the label in a start column beside the control, with the message and the hint below the control. The label lines up with the first line of the control (a Checkbox, Switch or RadioGroup gets 6px of padding above and below for it).
- size sets the label's type ramp and, in the horizontal layout, its top padding and the padding that lines a Checkbox, Switch or RadioGroup row up with it, so the label's first line keeps lining up with a 24, 32 or 40px control. The message and the hint stay small. Controls inside read the prop from FieldContext as their own default size (their own size wins); the Field's own label falls back to WaveProvider inputDefaults.size, else medium, and the root always renders that resolved value as data-size.
- label also takes a Label or InfoLabel element, which renders in place of Field's own <label> with the Field's wiring (see label); an InfoLabel's info button sits next to the label text, outside the <label>, so the control is named by the text alone. A group control (RadioGroup, your own <div role="radiogroup">) is named through aria-labelledby pointing at that <label>.
- Provides FieldContext: the library's inputs (Input, Select, Textarea, Slider, SearchBox, Checkbox, Switch, RadioGroup, Rating, SpinButton, pickers, …) read it wherever they are inside the Field and are named by the label, described by the message and the hint and marked invalid/required. Controls of your own read it with useFieldControl and useFieldContext; FieldContextProvider provides one for a field layout of your own.
- Merges into its **first** element child (a native <input>/<select>/<textarea>, a library control or your own component): id (unless the child has one — the label then points at the child's id), aria-describedby (joined with the child's own ids), aria-invalid (in the error state), aria-required and, for native form controls that take a value, required. Only defined values are merged; the child's own values are kept, so a child's aria-invalid={false} stays valid while the Field error still describes it (as for a library control nested deeper). Further element children are rendered as they are.
- A **function child** renders the control itself and receives the attributes as props (id, aria-labelledby, aria-describedby, aria-invalid, aria-required; see children), for a control Field cannot merge into, such as a third-party input that takes them under other names. Field renders what it returns as it is.
- aria-required is added only where it is allowed: components (Field cannot see what they render, so they decide where it goes — see the wrapper note below), <input> (not type="button"/"submit"/"reset"/"image"), <select>, <textarea>, and elements whose explicit role supports it (checkbox, combobox, switch, textbox, …). A <button>, <meter>, <output> or <progress> is labelled and described but not marked required.
- Your own widget built on an element with an explicit role (<div role="radiogroup">) is named through aria-labelledby (joined with its own; not added when it has an aria-label), described, marked invalid, and gets aria-required when its role supports it. It receives no id, because a <label htmlFor> cannot name it. A role-less <div> or <span> wrapper (or a role="presentation"/"none" one) is left alone: the library control inside it reads FieldContext.
- A custom element (<my-text-field>) without a role is treated like a component: it receives the id, the description and aria-invalid (a form-associated custom element is named by the label). Field does not guess its semantics: pass required/aria-required or an explicit role yourself.
- **One control per Field.** A Field labels and describes a single control; wrap each control in its own Field (a development warning is logged when a Field has more than one element child). Further element children are rendered as they are. A library control among them still reads FieldContext, so it shares the Field's name and description (when the first child holds the control id, it gets an id of its own and is named through aria-labelledby). Several library controls inside one plain wrapper element (<div>) are all named and described too: the first one takes the control id the label points at, the others get ids of their own and are named through aria-labelledby (when the first one is removed, the next one takes the control id over).
- **Wrapper and layout components around a library control** (a Tooltip, your own Row): put them inside a plain element (<div>, <div className="flex">), which Field leaves alone; the library control inside then holds the control id and is labelled, described and marked invalid/required through FieldContext. Field cannot see inside a component, so a component **first child** is treated as the control (it may be your own input): it receives the control id, aria-describedby, aria-invalid and, with required, aria-required, and a wrapper puts them on its own element (Tooltip's <span>, the layout <div>). The library control inside still gets an id of its own and is named by the label through aria-labelledby, but clicking the label does not focus it, and with required, aria-required on the wrapper's non-widget element is an ARIA error (axe aria-allowed-attr), even though the control inside is correctly marked required.
Examples
Default
export const Default: Story = {
render: (args) => (
<Field {...args}>
<Input placeholder="Enter your name" />
</Field>
),
};With Hint
We'll never share your email.
export const WithHint: Story = {
args: {
label: 'Email',
hint: "We'll never share your email.",
},
render: (args) => (
<Field {...args}>
<Input type="email" placeholder="you@example.com" />
</Field>
),
};With Error
export const WithError: Story = {
args: {
label: 'Password',
error: 'Password must be at least 8 characters.',
},
render: (args) => (
<Field {...args}>
<Input type="password" />
</Field>
),
};Required
export const Required: Story = {
args: {
label: 'Username',
required: true,
},
render: (args) => (
<Field {...args}>
<Input placeholder="Required field" />
</Field>
),
};With Select
Where you live today.
export const WithSelect: Story = {
args: {
label: 'Country',
hint: 'Where you live today.',
required: true,
},
render: (args) => (
<Field {...args}>
<Select defaultValue="">
<option value="" disabled>
Choose a country
</option>
<option value="no">Norway</option>
<option value="se">Sweden</option>
<option value="dk">Denmark</option>
</Select>
</Field>
),
};With Textarea
export const WithTextarea: Story = {
args: {
label: 'Description',
error: 'Description is required.',
},
render: (args) => (
<Field {...args}>
<Textarea placeholder="Describe the issue" />
</Field>
),
};With Slider
Applies to every output device.
export const WithSlider: Story = {
args: {
label: 'Volume',
hint: 'Applies to every output device.',
},
render: (args) => (
<Field {...args}>
<Slider min={0} max={100} defaultValue={40} />
</Field>
),
};Validation States
export const ValidationStates: Story = {
render: (args) => (
<div className="flex flex-col gap-4">
<Field
{...args}
label="Username"
validationState="error"
validationMessage="This username is taken."
>
<Input defaultValue="wave" />
</Field>
<Field
{...args}
label="Password"
validationState="warning"
validationMessage="This password is used often."
>
<Input type="password" defaultValue="password1" />
</Field>
<Field
{...args}
label="Display name"
validationState="success"
validationMessage="This name is available."
>
<Input defaultValue="Wave Rider" />
</Field>
<Field
{...args}
label="Nickname"
validationState="none"
validationMessage="Shown next to your comments."
>
<Input />
</Field>
</div>
),
};Hint With Message
At least 12 characters, with a number and a symbol.
export const HintWithMessage: Story = {
args: {
label: 'Password',
hint: 'At least 12 characters, with a number and a symbol.',
error: 'The password is too short.',
},
render: (args) => (
<Field {...args}>
<Input type="password" defaultValue="short" />
</Field>
),
};Horizontal
export const Horizontal: Story = {
args: {
orientation: 'horizontal',
},
render: (args) => (
<form className="flex max-w-xl flex-col gap-4" onSubmit={(e) => e.preventDefault()}>
<Field {...args} label="Display name" hint="Shown to other people.">
<Input defaultValue="Wave Rider" />
</Field>
<Field
{...args}
label="Email"
validationState="warning"
validationMessage="Not verified yet."
>
<Input type="email" defaultValue="wave@example.com" />
</Field>
<Field {...args} label="Notifications">
<Switch label="Email me about replies" defaultChecked />
</Field>
<Field {...args} label="Newsletter">
<Checkbox label="Send me the monthly newsletter" />
</Field>
<Field {...args} label="Theme">
<RadioGroup defaultValue="system">
<RadioGroup.Item value="light" label="Light" />
<RadioGroup.Item value="dark" label="Dark" />
<RadioGroup.Item value="system" label="Same as the system" />
</RadioGroup>
</Field>
</form>
),
};Sizes
export const Sizes: Story = {
render: (args) => (
<div className="flex flex-col gap-4">
<Field {...args} label="Small field" size="small">
<Input placeholder="Small" />
</Field>
<Field {...args} label="Medium field" size="medium">
<Input placeholder="Medium" />
</Field>
<Field {...args} label="Large field" size="large">
<Input placeholder="Large" />
</Field>
</div>
),
};Field with a function child
Your work address.
A mobile number.
export const WithFunctionChild: Story = {
name: 'Field with a function child',
args: {
label: 'Email',
hint: 'Your work address.',
required: true,
},
render: (args) => (
<div className="flex max-w-sm flex-col gap-4">
<Field {...args}>
{(props) => (
<input
type="email"
autoComplete="email"
{...props}
className={cn(
'h-8 px-3 text-wave-body-1 text-wave-foreground',
OUTLINE_BOX,
inputFocus,
)}
/>
)}
</Field>
<Field {...args} label="Phone" hint="A mobile number." error="Enter a phone number.">
{(props) => <PhoneInput inputProps={props} />}
</Field>
</div>
),
};Custom Message Icon
export const CustomMessageIcon: Story = {
render: (args) => (
<div className="flex flex-col gap-4">
<Field
{...args}
label="Workspace"
validationState="success"
validationMessage="Saved to the cloud."
validationMessageIcon={
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" stroke="currentColor">
<path d="M3 9h6a2 2 0 0 0 0-4 3 3 0 0 0-6 1 1.5 1.5 0 0 0 0 3Z" />
</svg>
}
>
<Input defaultValue="Design team" />
</Field>
<Field
{...args}
label="Project"
validationMessage="A project name is required."
validationMessageIcon={null}
>
<Input />
</Field>
</div>
),
};Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
children | React.ReactNode | ((props: FieldControlProps) => React.ReactNode) | No | The control. An element child receives the Field's attributes (see the component's notes on the first element child). A function child renders the control itself, for a control Field cannot merge into, such as a third-party input that takes its attributes under other names: The function receives, each only when it has a value: Field calls the function during its own render (keep it pure and call no hooks in it; render a component that does) and renders what it returns as it is, with nothing merged into it, inside the Field's context. The function's control holds the label's id, so a library control in the result that receives no | |
label | React.ReactNode | No | Label displayed above the control (beside it with A | |
hint | React.ReactNode | No | Hint shown below the control, after any validation message; describes the control. | |
error | React.ReactNode | No | Validation error. Content is rendered below the control in a Shorthand for | |
validationState | 'none' | 'error' | 'warning' | 'success' | No | Validation state of | |
validationMessage | React.ReactNode | No | Message below the control, styled and announced by | |
validationMessageIcon | Slot<'span'> | No | Icon before the message. Default: an error, warning or success glyph per state; none for | |
orientation | 'horizontal' | 'vertical' | No | 'vertical' |
|
size | 'small' | 'medium' | 'large' | No | `WaveProvider` `inputDefaults.size`, else `'medium'`. | The label's size and the default |
required | boolean | No | Whether the field is required: shows a decorative asterisk next to the label, sets | |
htmlFor | string | No | The id of the control the label points at, when the control has no | |
ref | React.Ref<HTMLDivElement> | No | Ref to the root | |
slotProps | { label?: SlotPart<'label'>; required?: SlotPart<'span'>; validationMessage?: SlotPart<'p'>; hint?: SlotPart<'p'> } | No | Props for the elements the Field draws around the control, by part, merged into each part's own (Fluent UI's |
Any other prop is passed to the element it renders (HTMLDivElement).
Parts
labelrequiredvalidationMessagehint
Stable class names
| Part | Class |
|---|---|
root | wave-field |
label | wave-field__label |
required | wave-field__required |
validationMessage | wave-field__validation-message |
validationMessageIcon | wave-field__validation-message-icon |
hint | wave-field__hint |