Skip to content

Search field API

import { SearchField } from '@cimpress-ui/react';

This component can be used in a controlled or uncontrolled way.

In the controlled way, this component doesn’t maintain its own internal state, and its value is provided by the parent component. Use the controlled way when you need to be able to change the state of this component in other parts of your application.

In the uncontrolled way, this component maintains its own internal state, and can optionally notify the parent component when its internal state changes. Use the uncontrolled way when you don’t need to change the state of this component in other parts of your application.

This component exposes an imperative API through the apiRef prop. This API allows triggering behaviors that can’t be expressed by props.

The search term can be submitted by pressing the Enter key, and cleared by pressing the Escape key.

SearchField requires a textual label to remain accessible to assistive technologies. See our accessibility guide for more details.

SearchField exposes multiple events that can be used to detect changes:

  • onChange - triggered on every change of the field’s value
  • onSubmit - triggered when the value is submitted by the user (for example, by pressing the Enter key)
  • onClear - triggered when the value is cleared by the user (for example, by pressing the clear button)

Play with the example below to see the difference between these events.

Search field can integrate with native HTML forms. See our forms guide to learn how to work with forms.

Allows users to enter and clear a search query.

See search field usage guidelines.

Ref<HTMLDivElement>

The ref type for this component.

SearchFieldProps
string

The element's unique identifier. See MDN.

boolean

Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

string

Sets the CSS className for the element. Only use as a last resort. Use style props instead.

See styling guide.

CSSProperties

Sets the CSS style for the element. Only use as a last resort. Use style props instead.

See styling guide.

string

The content to display as the label.

string

Defines a string value that labels the current element.

string

Identifies the element (or elements) that labels the current element.

string

Identifies the element (or elements) that describes the object.

string

Identifies the element (or elements) that provide a detailed, extended description for the object.

string

The <form> element to associate the input with. The value of this attribute must be the id of a <form> in the same document. See MDN.

string

The name of the input element, used when submitting an HTML form. See MDN.

string

A description for the field. Provides a hint such as specific requirements for what to choose.

FieldError

An error message for the field.

(value: string) => string | true | string[] | undefined

A function that returns an error message (or true) if a given value is invalid. Validation errors are displayed to the user when the form is submitted. For real-time validation, use the error prop instead.

string

The placeholder text displayed in the input field.

RefObject<SearchFieldApi | null>

A React ref that allows access to the imperative API of this component.

(value: string) => void

Handler that is called when the SearchField is submitted.

() => void

Handler that is called when the clear button is pressed.

string

Describes the type of autocomplete functionality the input should provide if any. See MDN.

number

The maximum number of characters supported by the input. See MDN.

number

The minimum number of characters required by the input. See MDN.

string

Regex pattern that the value of the input must match to be valid. See MDN.

'search' | 'none' | 'text' | 'tel' | 'url' | 'email' | 'numeric' | 'decimal'

Hints at the type of data that might be entered by the user while editing the element or its contents. See MDN.

string

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.

string

An enumerated attribute that defines whether the element may be checked for spelling errors. See MDN.

(e: FocusEvent<HTMLInputElement>) => void

Handler that is called when the element receives focus.

(e: FocusEvent<HTMLInputElement>) => void

Handler that is called when the element loses focus.

boolean

Whether the element should receive focus on render.

boolean

Whether user input is required on the input before form submission.

boolean

Whether the input value is invalid.

boolean

Whether the input is disabled.

boolean

Whether the input can be selected but not changed by the user.

string

The current value (controlled).

string

The default value (uncontrolled).

(value: string) => void

Handler that is called when the value changes.

StyleProps
Responsive<Spacing | "auto">

The amount of margin applied to all edges of this component.

Responsive<Spacing | "auto">

The amount of margin applied to the left and right edges of this component. Takes priority over margin.

Responsive<Spacing | "auto">

The amount of margin applied to the top and bottom edges of this component. Takes priority over margin.

Responsive<Spacing | "auto">

The amount of margin applied to the top edge of this component. Takes priority over marginY and margin.

Responsive<Spacing | "auto">

The amount of margin applied to the right edge of this component. Takes priority over marginX and margin.

Responsive<Spacing | "auto">

The amount of margin applied to the bottom edge of this component. Takes priority over marginY and margin.

Responsive<Spacing | "auto">

The amount of margin applied to the left edge of this component. Takes priority over marginX and margin.