Skip to content

SearchBox

SearchBox

A search input with a leading search icon and a clear button that appears while there is text (not while readOnly).

- **Layout**: the root <div> draws the field (its outline, focus and invalid look). The icon (or contentBefore), the text, contentAfter and the clear button sit side by side in it, so no slot covers the text. Pressing the field outside a control of its own focuses the input.
- **Clearing**: the clear button clears the text and moves focus back to the input. Escape clears the text too while there is any, and is then consumed, so an enclosing Dialog, Drawer or Popover stays open; in an empty field Escape reaches them. Both call onClear.
- **Routing**: ref, id, ARIA and native input attributes and the focus/keyboard handlers go to the <input type="search"> (role searchbox); className, style, data-* and other handlers stay on the root <div> (slotProps.root.ref reaches it). Inside a Field it picks up the label, hint, error and required state automatically.
- **Forms**: with name the text is submitted with the form; a form reset restores defaultValue.
- **Size and appearance**: size resolves from its own prop, then the surrounding Field's size, then WaveProvider inputDefaults.size, else 'medium'; appearance from its own prop, then WaveProvider inputDefaults.appearance, else 'outline'; both render as data-size and data-appearance on the root <div>.

Examples

Default

export const Default: Story = {};

With Slots

export const WithSlots: Story = {
  args: {
    'aria-label': 'Search files',
    placeholder: 'Search files...',
    defaultValue: 'quarterly financial report draft v2',
    contentAfter: { children: '⌘K', 'aria-hidden': true },
  },
};

Custom Dismiss

export const CustomDismiss: Story = {
  args: {
    defaultValue: 'invoices',
    dismiss: <span aria-hidden="true">✕</span>,
  },
};

Controlled

Query: (empty)

export const Controlled: Story = {
  render: (args) => {
    const [value, setValue] = useState('');
    return (
      <div>
        <SearchBox
          {...args}
          placeholder="Type to search"
          value={value}
          onValueChange={(next) => {
            setValue(next);
            args.onValueChange?.(next);
          }}
        />
        <p style={{ marginTop: 8 }}>Query: {value || '(empty)'}</p>
      </div>
    );
  },
};

Disabled

export const Disabled: Story = {
  args: {
    defaultValue: 'archived',
    disabled: true,
  },
};

Sizes And Appearances

export const SizesAndAppearances: Story = {
  render: (args) => (
    <SizeAppearanceGrid
      render={(size, appearance) => (
        <SearchBox
          {...args}
          size={size}
          appearance={appearance}
          defaultValue="design"
          aria-label={`${size} ${appearance}`}
        />
      )}
    />
  ),
};

Props

NameTypeRequiredDefaultDescription
valuestringNo

Controlled search text value.

defaultValuestringNo''

Initial search text for uncontrolled usage (also what a form reset restores).

onValueChange(value: string) => voidNo

Called with the new search text when it changes (typing or clearing).

onClear() => voidNo

Called when the user clears the search: with the clear button, or with Escape while there is text (after onValueChange('')).

placeholderstringNo'Search'

Placeholder text shown when the input is empty.

disabledbooleanNo

Whether the search box is disabled and non-interactive.

size'small' | 'medium' | 'large'No

Size of the field: small (24px tall), medium (32px) or large (40px). Default: the surrounding Field's size, else WaveProvider inputDefaults.size, else 'medium'.

appearance'outline' | 'underline' | 'filled-darker' | 'filled-lighter'No

Look of the field: outline (a full border), underline (a bottom stroke only), filled-darker or filled-lighter (a fill without a visible stroke: give the field a visible label). Default: WaveProvider inputDefaults.appearance, else 'outline'.

contentBeforeSlot<'span'>No

Slot rendered before the text (replaces the default search icon). It takes the room it needs; the text starts after it. A value that renders nothing (null, false, '', an empty array or Fragment) keeps the default icon.

contentAfterSlot<'span'>No

Slot rendered after the text, before the clear button. It takes the room it needs; the text ends before it. A value that renders nothing (false, '', an empty array or Fragment) is no slot: nothing takes room.

dismissSlot<'span'>No

Content of the clear button (replaces the default dismiss icon). It always renders **inside** the built-in <button type="button" aria-label="Clear search">, which clears the value, calls onClear and moves focus back to the input. The slot cannot hide the clear button or leave it empty: content that renders nothing (null, false, true, '', or an array, Set, generator or Fragment of only those) keeps the default icon, and a slot object without children ({ className: 'text-error' }) wraps the default icon in its element (not when it brings markup of its own through dangerouslySetInnerHTML, or is a void or component element).

A <button> element or a Wave Button passed here is not nested: its props are merged into the built-in button (its onClick runs first; preventDefault() cancels the clear) and its children become the content, with a development warning. A Wave Button's own props are not attributes: its icon becomes decorative content before its children (after them with iconPosition="after"), disabledFocusable makes the clear button unavailable but focusable as on the Button (nothing is cleared), and appearance and size are ignored.

A slot object is the content element, not the button: its attributes, event handlers included, go to that element (only the naming attributes below reach the button), so handle the press in onClear. A slot object whose as is a <button> or a Wave Button cannot render inside the button: its children render as the content (the default icon when it has none) and its props are dropped, with a development warning.

Name (WCAG 2.5.3 Label in Name): content is decorative (aria-hidden), so the button keeps the name "Clear search". An aria-label, aria-labelledby or title on the merged button or on a slot object names the button instead ({ children: 'Reset', 'aria-label': 'Reset' }). The children of a merged <button>/Button are rendered as is: when they render a text label (at least two letters or digits outside aria-hidden/hidden content, text from components such as translations included), that text names the button; an icon or a lone character (X, ×) keeps "Clear search". Text hidden only with CSS still counts as the label, so give a button with a responsive label an explicit aria-label.

refReact.Ref<HTMLInputElement>No

Ref to the <input type="search">, the focusable control (Fluent UI's ref, on its primary element). The root <div> is slotProps.root.ref.

slotProps{ root?: SlotPart<'div'>; input?: SlotPart<'input'>; dismissButton?: SlotPart<'button'> }No

Props for the elements the SearchBox draws, by part, merged into each part's own (Fluent UI's root, input and dismiss slot props): your classes after the part's (yours win a conflict), your styles over its styles, your handlers before its handlers (preventDefault() in yours skips the SearchBox's handling of the event, its own on… prop included), your ref beside its ref, and your ids added to its aria-labelledby and aria-describedby. Every attribute the SearchBox sets on a part is locked and keeps the SearchBox's value (set it through its own props); you add the attributes it does not set. A part takes no children (dismiss fills the clear button).

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

Parts

  • root
  • input
  • dismissButton

Stable class names

PartClass
rootwave-search-box
contentBeforewave-search-box__content-before
inputwave-search-box__input
contentAfterwave-search-box__content-after
dismissButtonwave-search-box__dismiss-button
dismissIconwave-search-box__dismiss-icon