Web and native

SearchField in kanso-ui 0.59.0, for React.

import { SearchField } from '@kanso-labs/kanso-ui'

A search bar: a keyword typed into a pill, submitted with Enter and cleared with Escape or its clear button.

It follows the Material Design search bar: a 56dp pill on the surface-container-high colour, a magnifier at the start, the placeholder as supporting text, and a clear button at the end while the bar holds anything. The bar grows with its container up to 720dp. Where the search page names no treatment, it borrows the text field's: a 2dp primary ring on focus, an error-coloured ring in the error state, and the shared faded look when disabled.

What you provide

  • label (required): what the bar searches. It is read by a screen reader and never shown, since the bar draws no label.
  • placeholder: the supporting text in the empty bar, which is what a person reads.
  • A value: defaultValue, or value with onChange. onSubmit receives the value on Enter, and onClear is called when the bar is emptied.
  • A width. The bar fills its container up to 720dp.

Props

PropTypeDefaultDescription
clearLabelstring—The name of the button that clears the field, for a screen reader. Left out, React Aria names it in the reader's locale — "Clear search" in English.
descriptionstring—A hint shown under the pill. Replaced by `error` when there is one, so the two never stack.
enterKeyHintSearchFieldProps$1['enterKeyHint']'search'The action an on-screen keyboard labels its Enter key with. A search field asks for Search, which iOS otherwise shows only for a search input inside a form with an action, so a field submitted through `onSubmit` alone gets it too. Inside an `Autocomplete` the field takes the Go key React Aria gives it there, for the option Enter picks.
errorstring—The problem with the current value, in words. Its presence is what puts the field in its error state — the message and `aria-invalid` follow from it. Outside a `Form` the message line arrives with the message, so the field grows when this does and moves what is under it. A `Form` holds that space from the start; so does a permanent `description`.
inputRefRef<HTMLInputElement>—A ref to the field's own `<input>`, which is the element a call site moves focus to — after a server's error, say. `ref` is the field as a whole.
label requiredstring—What the field searches, for a screen reader. The pill draws no label, so this is read and never shown; `placeholder` is what is seen. Write it so it contains the placeholder's words — `Search` beside a `Search` placeholder, or `Search people`. The placeholder is the only text a sighted reader has to call the field by, so it is what someone driving the page by voice says, and a name that does not contain it leaves them nothing to say.
placeholderstring—What the empty pill shows, naming what can be found — "Search items, files, people". It is what a person reads, so `label` repeats its words.

Also takes the props of Omit<SearchFieldProps$1, 'children' | 'enterKeyHint' | 'isInvalid' | 'validationBehavior'>.

Example

import { SearchField } from '@kanso-labs/kanso-ui'
<SearchField label="Label" placeholder="Supporting text" />
<SearchField
  label="Label"
  placeholder="Supporting text"
  defaultValue="Typed keyword"
  onSubmit={(keyword) => console.log(keyword)}
/>

The design’s classes

k-search, k-icon k-icon--md for the search icon, and k-iconbtn k-iconbtn--sm for Clear search.

SearchField in kanso-ui and react-native-kanso-ui, restyled to this language.