Actions
editActions
editTo update the state, you can use actions below. Actions are functions that update the Request State and performs an API request.
setSearchTerm("search term");
To get access to the actions within your component, you must wrap your component with our context HOCs.
// Selects `searchTerm` and `setSearchTerm` for use in Component withSearch(({ searchTerm, setSearchTerm }) => ({ searchTerm, setSearchTerm }))(Component);
See WithSearch & withSearch for more information.
There are certain cases where you may need to apply one or more actions at a time. Search UI intelligently batches actions into a single API call.
For example, if you need to apply two filters at once, it is perfectly acceptable to write the following code:
addFilter("states", "Alaska", "any"); addFilter("world_heritage_site", "true");
This will only result in a single API call.
addFilter
editaddFilter( name: string, value: FilterValue, type: FilterType = "all" )
Add a filter in addition to current filters values.
Examples
editaddFilter("states", "Alaska"); addFilter("isPublished", true); addFilter("rating", 1); addFilter("states", ["Alaska", "California"], "all"); addFilter("states", ["Alaska", "California"], "any"); addFilter("published",{ name: "published", from: "2020-01-01", to: "2020-12-31" }); addFilter("rating",{ name: "badRating", from: 1, to: 6 });
Parameters
editParameters | description |
---|---|
|
Required. Name of the field |
|
Required. Filter Value. See |
|
Optional. Defaults to |
setFilter
editsetFilter( name: string, value: FilterValue, type: FilterType = "all" )
Set a filter value, replacing current filter values.
Examples
editsetFilter("states", "Alaska"); setFilter("isPublished", true); setFilter("rating", 1); setFilter("states", ["Alaska", "California"], "all"); setFilter("states", ["Alaska", "California"], "any"); setFilter("published",{ name: "published", from: "2020-01-01", to: "2020-12-31" }); setFilter("rating",{ name: "badRating", from: 1, to: 6 });
Parameters
editParameters | description |
---|---|
|
Required. Name of the field |
|
Required. Filter Value. See |
|
Optional. Defaults to |
removeFilter
editRemoves filters or filter values.
removeFilter( name: string, value?: FilterValue, type?: FilterType )
Examples
editremoveFilter("states"); removeFilter("states", ["Alaska", "California"]); removeFilter("published", { name: "published", from: "2020-01-01", to: "2020-12-31" }); removeFilter("rating", { name: "badRating", from: 1, to: 6 });
Parameters
editParameters | description |
---|---|
|
Required. Name of the field |
|
Optional. Filter Value. Will remove all filters under field if value not specified. See |
|
Optional. Defaults to |
reset
editReset state to initial search state.
reset();
clearFilters
editClear all filters.
clearFilters((except: string[] = []));
Examples
editclearFilters(); clearFilters(["states"]); // field name
Parameters
editParameters | description |
---|---|
|
Optional. String array. Field names which you want to ignore being cleared. |
setCurrent
editUpdate the current page number. Used for paging.
setCurrent(current: number)
Examples
editsetCurrent(2);
Parameters
editParameters | description |
---|---|
|
Required. Number type. The page number. |
setResultsPerPage
editUpdate the number of results per page. Used for paging.
setResultsPerPage(resultsPerPage: number)
Examples
editsetResultsPerPage(20);
Parameters
editParameters | description |
---|---|
|
Required. Number type. Sets number of results per page. |
setSearchTerm
editsetSearchTerm( searchTerm: string, { autocompleteMinimumCharacters = 0, autocompleteResults = false, autocompleteSuggestions = false, shouldClearFilters = true, refresh = true, debounce = 0 }: SetSearchTermOptions = {} )
Update the search term. Also gives you the ability to control autocomplete options.
Examples
editsetSearchTerm("train");
Parameters
editParameters | description |
---|---|
|
Required. String type. the new search term to query by |
|
Optional. Object type. See |
SetSearchTermOptions Parameters
editParameters | description |
---|---|
|
Optional. miniumum terms to start performing autocomplete suggestions |
|
Optional. To perform autocomplete Results |
|
Optional. To perform autocomplete Suggestions |
|
Optional. To clear filters |
|
Optional. To refresh results |
|
Optional. |
setSort
editsetSort( sort: SortOption[] | string, sortDirection: SortDirection )
Update the sort option.
Parameters
editParameters | description |
---|---|
|
|
|
String - "asc" or "desc" |
trackClickThrough
edittrackClickThrough( documentId: string, tags: string[] = [] )
Report a clickthrough event, which is when a user clicks on a result link.
Parameters
editParameters | description |
---|---|
|
String - The document ID associated with the result that was clicked |
|
Optional. Array[String] Optional tags which can be used to categorize this click event |
trackAutocompleteClickThrough
edittrackAutocompleteClickThrough( documentId: string, tags: string[] = [] )
Report a clickthrough event, which is when a user clicks on an autocomplete suggestion.
Parameters
editParameters | description |
---|---|
|
String - The document ID associated with the result that was clicked |
|
Optional. Array[String] Optional tags which can be used to categorize this click event |
trackAutocompleteSuggestionClickThrough
editThis action requires the use of the analytics plugin.
trackAutocompleteSuggestionClickThrough( suggestion: string, postion: number tags: string[] = [] )
Report a suggestion clickthrough event, which is when a user clicks on an autocomplete suggestion.
Parameters
editParameters | description |
---|---|
|
String - The suggestion that was clicked |
|
Number - The position of the suggestion that was clicked |
|
Optional. Array[String] Optional tags which can be used to categorize this click event |
a11yNotify
edita11yNotify( messageFunc: string, messageArgs?: unknown )
Reads out a screen reader accessible notification. See a11yNotificationMessages
under TODO LINK
Parameters
editParameters | description |
---|---|
|
String - object key to run as function |
|
Object - Arguments to pass to form your screen reader message string |
Types
editFilterValue & FilterType Types
editFilterValue
can be either a value type or a range type.
type FilterValue = FilterValueValue | FilterValueRange; type FieldValue = string | number | boolean | Array<string | number | boolean>; type FilterValueValue = FieldValue; type FilterValueRange = { from?: FieldValue; name: string; to?: FieldValue; }; type FilterType = "any" | "all" | "none";