Search field
Slant / Forms
A search input with a clear button and a keyboard shortcut.
Demo
Usage
import { Field, SearchField } from '@tuttn/slant'
// Controlled: filter as the reader types; Enter calls onSearch.
const [query, setQuery] = useState('')
<SearchField value={query} onValueChange={setQuery} onSearch={runSearch}
shortcut="/" label="Search tasks" placeholder="Search tasks" />
// Uncontrolled, submitted with a form: the input itself carries the name.
<form role="search" action="/search">
<SearchField name="q" defaultValue="handoff" />
</form>
// In a Field: the label names it instead of `label`.
<Field>
<Field.Label>Filter checkpoints</Field.Label>
<SearchField onValueChange={setFilter} />
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | The query. Pass it to control the field; leave it out to let the field keep its own. |
defaultValue | string | '' | The query an uncontrolled field starts with. |
onValueChange | (value: string) => void | — | Called with the new query as the reader types, and with `''` when the clear button or Escape empties it. |
onSearch | (value: string) => void | — | Called with the query when the reader presses Enter. |
label | string | 'Search' | The accessible name, unless a Field's label or `aria-labelledby` names the field. From the root's `messages.search`. |
clearLabel | string | 'Clear search' | The clear button's accessible name. From the root's `messages.clearSearch`. |
shortcut | string | — | A single character, such as `/`, that focuses the field from anywhere on the page. Shown as a key hint while the field is empty. |
size | 'sm' | 'md' | 'lg' | 'md' | Height on the control scale: 28, 34 or 40px at regular density. |
invalid | boolean | — | Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this. |
…rest | InputHTMLAttributes<HTMLInputElement> | — | The ref and every other attribute (name, placeholder, autoFocus, aria-*, handlers) reach the <input type="search">; className and style go on the group. |
Keyboard
- Escape clears the query when there is one, and goes no further, so a dialog around the field stays open. With the field empty it does nothing of its own.
- Enter calls
onSearchwith the query, and still submits a form around the field. - The
shortcutkey (a single character, such as/) focuses the field from anywhere on the page, unless the reader is typing in another field or holds Ctrl, Alt or Meta. It is shown as a key hint while the field is empty, and set asaria-keyshortcuts.
Accessibility
The input has the searchbox role. Its name is label, or the root's messages.search ("Search"), unless a Field's label or aria-labelledby names it. The clear button appears only while there is a query, is named by messages.clearSearch, and stays out of the tab order since Escape does the same; after a click it puts focus back in the field.
It is drawn as an Input group. For a list of commands rather than a query, see Command bar.