Fragiola

API: components

SearchField

A search query's field, cleared by Escape and a clear button out of the tab order, submitted by Enter, as a form control, in each framework.

A text field for a search query, with a button that clears it: a site search, a filter box. The Input is a native <input type="search"> (the searchbox role, the APG's textbox for search; its form goes in a search landmark, see the APG's landmarks); Escape clears it, Enter submits the query. It is a form control: the Input carries the name, submits, resets and validates, alone or in a Field. The APG has no search field pattern and Base UI no SearchField: it follows React Aria's SearchField. Suggestions are an Autocomplete or a Combobox.

Product search
Gallery

Behaviour

  • Escape clears a field that holds a query (its default prevented, so a dialog around it stays open); on an empty field it is not handled and goes on to the page, so a second Escape closes a dialog or a popup around it.
  • Enter calls onSubmit(value) and the browser goes on with the form's submission.
  • The ClearButton empties the field and focuses the Input; it is out of the tab order (tabindex="-1": Escape clears from the keyboard) and stays rendered while the field is empty (data-empty, which a style hides it with).
  • Read-only: Escape and the ClearButton do not clear it; disabled: every part is.
  • Forms: the Input is the form control (no hidden input); a reset returns it to its default (reason none).
  • In a Field: the Input is the labelable and the validated element; validate gets the query.
  • Reasons: input-change, escape-key, clear-press, none.

Anatomy

partelementdata-primitives-partrole
Rootdivsearch-field-rootnone; holds the query
Inputinputsearch-field-inputsearchbox (type="search")
ClearButtonbuttonsearch-field-clear-buttonbutton, out of the tab order

Usage

import { SearchField } from "@fragiola/primitives-react/search-field";

<search>
    <label htmlFor="search">Search</label>
    <SearchField.Root id="search" name="q" onSubmit={(query) => search(query)}>
        <SearchField.Input placeholder="Lamps, desks…" />
        <SearchField.ClearButton aria-label="Clear search">×</SearchField.ClearButton>
    </SearchField.Root>
</search>;

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. The names below are React's; Vue binds them the same way (v-model:value, @value-change, @submit), and Angular as inputs and outputs ([(value)], (valueChangeDetails) with { value, details }, (valueSubmit)).

Root

proptypedefaultdescription
valuestringthe query, controlled
defaultValuestring""the initial query, uncontrolled
onValueChange(value: string, details: { reason }) => voidevery change of the query
onSubmit (react, vue)(value: string) => voidEnter in the Input (not while disabled or read-only)
onValueSubmit (angular)(value: string) => voidAngular's (valueSubmit): Enter in the Input
disabledbooleanfalsedisables the Input and the ClearButton
readOnlybooleanfalsethe Input is read-only; nothing clears it
requiredbooleanfalsethe Input is required
namestringthe Input's name (a Field's wins)
idstringthe Input's id (generated when not given)

Input

No props of its own.

ClearButton

No props of its own.

State and part attributes

partattributes
every partdata-empty while the query is "", data-disabled, data-readonly, data-required
Rootin a Field, the field's states
Inputtype="search", disabled, readonly, required; in a Field, aria-labelledby, aria-describedby, aria-invalid
ClearButtontype="button", tabindex="-1", aria-controls (the Input's id), disabled while disabled or read-only

Keyboard

keyaction
Escapeclears a query; on an empty field, goes on to the page
EnteronSubmit(value), then the form's submission
Tabthe Input; the ClearButton is skipped

Exports

entryexports
@fragiola/primitives-react/search-fieldChangeDetails, ChangeReason, RenderProp, SearchField, SearchFieldClearButtonProps, SearchFieldInputProps, SearchFieldRootProps, SearchFieldState
@fragiola/primitives-vue/search-fieldChangeDetails, ChangeReason, RenderSlotProps, SearchField, SearchFieldRootEmits, SearchFieldRootProps, SearchFieldState
@fragiola/primitives-angular/search-fieldChangeDetails, ChangeReason, SearchFieldClearButton, SearchFieldInput, SearchFieldRoot, SearchFieldState, ValueChangeEvent