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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
value | string | No | Controlled search text value. | |
defaultValue | string | No | '' | Initial search text for uncontrolled usage (also what a form reset restores). |
onValueChange | (value: string) => void | No | Called with the new search text when it changes (typing or clearing). | |
onClear | () => void | No | Called when the user clears the search: with the clear button, or with Escape while there is text (after | |
placeholder | string | No | 'Search' | Placeholder text shown when the input is empty. |
disabled | boolean | No | Whether the search box is disabled and non-interactive. | |
size | 'small' | 'medium' | 'large' | No | Size of the field: | |
appearance | 'outline' | 'underline' | 'filled-darker' | 'filled-lighter' | No | Look of the field: | |
contentBefore | Slot<'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 ( | |
contentAfter | Slot<'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 ( | |
dismiss | Slot<'span'> | No | Content of the clear button (replaces the default dismiss icon). It always renders **inside** the built-in A 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 Name (WCAG 2.5.3 Label in Name): content is decorative ( | |
ref | React.Ref<HTMLInputElement> | No | Ref to the | |
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 |
Any other prop is passed to the element it renders (HTMLDivElement).
Parts
rootinputdismissButton
Stable class names
| Part | Class |
|---|---|
root | wave-search-box |
contentBefore | wave-search-box__content-before |
input | wave-search-box__input |
contentAfter | wave-search-box__content-after |
dismissButton | wave-search-box__dismiss-button |
dismissIcon | wave-search-box__dismiss-icon |