A search field allows a user to enter and clear a search query.
Value
Use the value or defaultValue prop to set the text value, and onChange to handle user input. onSubmit is called when the user presses Enter.
Value: Submitted value:
import {SearchField} from './SearchField';
import {useState} from 'react';
function Example() {
let [search, setSearch] = useState('');
let [submittedSearch, setSubmittedSearch] = useState('');
return (
<div>
<SearchField
label="Search"
placeholder="Search documents"
value={search}
onChange={setSearch}
onSubmit={setSubmittedSearch} />
<pre style={{fontSize: 12}}>
Value: {search}{'\n'}
Submitted value: {submittedSearch}
</pre>
</div>
);
}
Forms
Use the name prop to submit the text value to the server. Set the isRequired, minLength, maxLength, pattern, or type props to validate the value, or implement custom client or server-side validation. See the Forms guide to learn more.
Examples
API
<SearchField>
<Label />
<Input />
<Button />
<Text slot="description" />
<FieldError />
</SearchField>
SearchField
| Name | Type | Default |
|---|---|---|
enterKeyHint | 'enter'
| 'done'
| 'go'
| 'next'
| 'previous'
| 'search'
| 'send' | Default: — |
An enumerated attribute that defines what action label or icon to preset for the enter key on virtual keyboards. See MDN. | ||
type | 'text'
| 'search'
| 'url'
| 'tel'
| 'email'
| 'password'
| string & | Default: 'search'
|
The type of input to render. See MDN. | ||
isDisabled | boolean | Default: — |
Whether the input is disabled. | ||
isReadOnly | boolean | Default: — |
Whether the input can be selected but not changed by the user. | ||
inputMode | 'none'
| 'text'
| 'tel'
| 'url'
| 'email'
| 'numeric'
| 'decimal'
| 'search' | Default: — |
Hints at the type of data that might be entered by the user while editing the element or its contents. See MDN. | ||
autoCorrect | string | Default: — |
An attribute that takes as its value a space-separated string that describes what, if any, type of autocomplete functionality the input should provide. See MDN. | ||
spellCheck | string | Default: — |
An enumerated attribute that defines whether the element may be checked for spelling errors. See MDN. | ||
children | ChildrenOrFunction | Default: — |
The children of the component. A function may be provided to alter the children based on component state. | ||
value | string | Default: — |
The current value (controlled). | ||
defaultValue | string | Default: — |
The default value (uncontrolled). | ||
onChange | | Default: — |
Handler that is called when the value changes. | ||
minLength | number | Default: — |
The minimum number of characters required by the input. See MDN. | ||
maxLength | number | Default: — |
The maximum number of characters supported by the input. See MDN. | ||
pattern | string | Default: — |
Regex pattern that the value of the input must match to be valid. See MDN. | ||
isRequired | boolean | Default: — |
Whether user input is required on the input before form submission. | ||
isInvalid | boolean | Default: — |
Whether the input value is invalid. | ||
validate | | Default: — |
A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if | ||
validationBehavior | 'native' | 'aria' | Default: 'native'
|
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. | ||
onSubmit | | Default: — |
Handler that is called when the SearchField is submitted. | ||
onClear | | Default: — |
Handler that is called when the clear button is pressed. | ||
onFocus | | Default: — |
Handler that is called when the element receives focus. | ||
onBlur | | Default: — |
Handler that is called when the element loses focus. | ||
onFocusChange | | Default: — |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | Default: — |
Handler that is called when a key is pressed. | ||
onKeyUp | | Default: — |
Handler that is called when a key is released. | ||
onCopy | ClipboardEventHandler | Default: — |
Handler that is called when the user copies text. See MDN. | ||
onCut | ClipboardEventHandler | Default: — |
Handler that is called when the user cuts text. See MDN. | ||
onPaste | ClipboardEventHandler | Default: — |
Handler that is called when the user pastes text. See MDN. | ||
onCompositionStart | CompositionEventHandler | Default: — |
Handler that is called when a text composition system starts a new text composition session. See MDN. | ||
onCompositionEnd | CompositionEventHandler | Default: — |
Handler that is called when a text composition system completes or cancels the current text composition session. See MDN. | ||
onCompositionUpdate | CompositionEventHandler | Default: — |
Handler that is called when a new character is received in the current text composition session. See MDN. | ||
onSelect | ReactEventHandler | Default: — |
Handler that is called when text in the input is selected. See MDN. | ||
onBeforeInput | FormEventHandler | Default: — |
Handler that is called when the input value is about to be modified. See MDN. | ||
onInput | FormEventHandler | Default: — |
Handler that is called when the input value is modified. See MDN. | ||
style | StyleOrFunction | Default: — |
The inline style for the element. A function may be provided to compute the style based on component state. | ||
className | ClassNameOrFunction | Default: 'react-aria-SearchField'
|
The CSS className for the element. A function may be provided to compute the class based on component state. | ||
name | string | Default: — |
The name of the input element, used when submitting an HTML form. See MDN. | ||
autoComplete | string | Default: — |
Describes the type of autocomplete functionality the input should provide if any. See MDN. | ||
form | string | Default: — |
The | ||
autoFocus | boolean | Default: — |
Whether the element should receive focus on render. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
excludeFromTabOrder | boolean | Default: — |
Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. | ||
aria-activedescendant | string | Default: — |
Identifies the currently active element when DOM focus is on a composite widget, textbox, group, or application. | ||
aria-autocomplete | 'none'
| 'inline'
| 'list'
| 'both' | Default: — |
Indicates whether inputting text could trigger display of one or more predictions of the user's intended value for an input and specifies how predictions would be presented if they are made. | ||
aria-haspopup | boolean
| 'false'
| 'true'
| 'menu'
| 'listbox'
| 'tree'
| 'grid'
| 'dialog' | Default: — |
Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. | ||
aria-controls | string | Default: — |
Identifies the element (or elements) whose contents or presence are controlled by the current element. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
aria-errormessage | string | Default: — |
Identifies the element that provides an error message for the object. | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||
render | DOMRenderFunction | Default: — |
Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements:
| ||
Default className: react-aria-SearchField
| Render Prop | CSS Selector |
|---|---|
isEmpty | CSS Selector: [data-empty]
|
| Whether the search field is empty. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the search field is disabled. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the search field is invalid. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the search field is read only. | |
isRequired | CSS Selector: [data-required]
|
| Whether the search field is required. | |
state | CSS Selector: — |
| State of the search field. | |