Skip to content

Field

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

This name is available.

Shown next to your comments.

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

Shown to other people.

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.

+47

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

Saved to the cloud.

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

NameTypeRequiredDefaultDescription
childrenReact.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: {(props) => <ThirdPartyInput inputProps={props} />} (Fluent's render-function child, children: (props: FieldControlProps) => ReactNode).

The function receives, each only when it has a value: id (the id the label points at), aria-labelledby (the label's id, which also names an element that a label cannot point at, such as a role="radiogroup" element), aria-describedby (the validation message, then the hint), aria-invalid (true in the error state only) and aria-required (true while the Field is required). It receives no native required and no size: pass them yourself, or read them from useFieldContext() in a component of your own. Leave aria-labelledby out to name the control otherwise (aria-label).

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 id from the props gets an id of its own and is named through aria-labelledby. A function cannot be passed from a Server Component: write the Field in a client component.

labelReact.ReactNodeNo

Label displayed above the control (beside it with orientation="horizontal"); names the control.

A Label or InfoLabel element passed as label renders in place of Field's own <label>, never inside it, and names the control by its label text alone (an InfoLabel's info button sits next to the <label>, outside it). Field always points it at the control (htmlFor), and gives it the Field's label id, required, label size and weight="semibold" (the look of Field's own label), each only where the element leaves it unset; Field's layout classes go before the element's className, so the element's classes win. The element's own htmlFor is read like the Field's htmlFor (which wins over it): it becomes the control's id when the control has none, and one that points at another element is replaced, with a development warning. Field shows no asterisk of its own next to such an element: the element shows its required indicator. Only the element itself is recognized, not one inside a Fragment, an array or another element.

hintReact.ReactNodeNo

Hint shown below the control, after any validation message; describes the control.

errorReact.ReactNodeNo

Validation error. Content is rendered below the control in a role="alert" element that describes the control, and the control is marked aria-invalid. true marks the control invalid without a message. A value that renders nothing (null, false, '', or an array of only those, such as an empty errors.map(…)) is no error; 0 is content.

Shorthand for validationState="error" with this message; wins over validationMessage and validationState (development warning when both are set).

validationState'none' | 'error' | 'warning' | 'success'No

Validation state of validationMessage: error marks the control invalid (aria-invalid) and announces the message (role="alert"); warning announces it without marking the control invalid; success and none show it without announcing. Default: 'error' when validationMessage renders content, else 'none'. Ignored while error is set.

validationMessageReact.ReactNodeNo

Message below the control, styled and announced by validationState; it describes the control (aria-describedby). error is the shorthand for an error message and wins over this prop.

validationMessageIconSlot<'span'>No

Icon before the message. Default: an error, warning or success glyph per state; none for 'none'. null (or content that renders nothing) shows no icon. Decorative (aria-hidden).

orientation'horizontal' | 'vertical'No'vertical'

vertical: the label above the control. horizontal: the label in a start column (a third of the width) beside the control; the message and the hint stay below the control. The label's first line lines up with a 32px control (Input, Select, …) and with the first line of a Checkbox, Switch or RadioGroup, which gets 6px of padding above and below for it. The horizontal layout wraps the control in a column, so changing orientation on a mounted Field remounts the control: an uncontrolled control loses its state and focus (control it, or keep one orientation while the Field is mounted).

size'small' | 'medium' | 'large'No`WaveProvider` `inputDefaults.size`, else `'medium'`.

The label's size and the default size of the text inputs and pickers inside the Field (their own size wins; choice controls keep theirs). The message and the hint stay small.

requiredbooleanNo

Whether the field is required: shows a decorative asterisk next to the label, sets aria-required, and turns on native constraint validation. <input>, <select>, <textarea> and Input, Select, Textarea, Slider, SearchBox and SpinButton get the native required attribute. Checkbox, Switch, RadioGroup, Rating, ColorPicker, SwatchPicker, Combobox, Dropdown, TagPicker, DatePicker and TimePicker render a hidden required input (with or without name), so submit is blocked until they are checked, switched on or have a value. An explicit required={false} on the control wins, including for aria-required.

htmlForstringNo

The id of the control the label points at, when the control has no id of its own (default: the htmlFor of a Label or InfoLabel element passed as label, else a generated id).

refReact.Ref<HTMLDivElement>No

Ref to the root <div>.

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 label, validationMessage and hint slot props): your classes after the part's (yours win a conflict), your styles over its styles, your handlers and attributes added, your ref attached. Every attribute the Field sets on a part is locked and keeps the Field's value: the ids the control's aria-labelledby and aria-describedby point at among them. A part takes no children (the Field's props give each its content). The control is your child: give it its props directly.

Any other prop is passed to the element it renders (HTMLDivElement).

Parts

  • label
  • required
  • validationMessage
  • hint

Stable class names

PartClass
rootwave-field
labelwave-field__label
requiredwave-field__required
validationMessagewave-field__validation-message
validationMessageIconwave-field__validation-message-icon
hintwave-field__hint