SearchField

A search field allows a user to enter and clear a search query.

Theme 
 
 
isDisabled 
Example
SearchField.tsx
SearchField.css
import {SearchField} from './SearchField';

<SearchField
  label="Search"
  placeholder="Search documents" />

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.

isRequired 
type 
 
 
 
import {SearchField} from './SearchField';
import {Button} from './Button';
import {Form} from './Form';;

function Example(props) {
  return (
    <Form>
      <SearchField
        {...props}
        label="Search"
        placeholder="Search documents"
        name="search"
        isRequired
      />
      <Button type="submit">Submit</Button>
    </Form>
  );
}

Examples

API

Shows a search field component with labels pointing to its parts, including the label, input, and clear button elements.ValueLabelInputLabelClear button
<SearchField>
  <Label />
  <Input />
  <Button />
  <Text slot="description" />
  <FieldError />
</SearchField>

SearchField

NameTypeDefault
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.
isDisabledbooleanDefault:
Whether the input is disabled.
isReadOnlybooleanDefault:
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.
autoCorrectstringDefault:
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.
spellCheckstringDefault:
An enumerated attribute that defines whether the element may be checked for spelling errors. See MDN.
children<>Default:
The children of the component. A function may be provided to alter the children based on component state.
valuestringDefault:
The current value (controlled).
defaultValuestringDefault:
The default value (uncontrolled).
onChange(value: T) => voidDefault:
Handler that is called when the value changes.

Default className: react-aria-SearchField

Render PropCSS Selector
isEmptyCSS Selector: [data-empty]
Whether the search field is empty.
isDisabledCSS Selector: [data-disabled]
Whether the search field is disabled.
isInvalidCSS Selector: [data-invalid]
Whether the search field is invalid.
isReadOnlyCSS Selector: [data-readonly]
Whether the search field is read only.
isRequiredCSS Selector: [data-required]
Whether the search field is required.
stateCSS Selector:
State of the search field.