Signature
Import
See this widget in action
Preview this widget and its behavior.
About this widget
ThemenuSelect widget allows a user to select a single value to refine inside a select element.
Requirements
Theattribute provided to the widget must be in attributes for faceting,
either on the dashboard or using the attributesForFaceting parameter with the API.
Examples
JavaScript
Options
string | HTMLElement
required
The CSS Selector or
HTMLElement to insert the widget into.string
required
The name of the attribute in the record.
JavaScript
number
default:10
The maximum number of values to display.
JavaScript
string[] | function
default:"Uses facetOrdering if set, ['name:asc']"
How to sort refinements. Must be one or more of the following strings:
"count:asc""count:desc""name:asc""name:desc""isRefined"
Array.sort.If facetOrdering is set for this facet in renderingContent,
and no value for sortBy is passed to this widget,
facetOrdering is used, and the default order as a fallback.object
default:"{}"
TThe CSS classes you can override:
root. The root element of the widget.noRefinementRoot. The root element if there are no refinements.select. Theselectelement.option. Theoptionelements of theselect.
JavaScript
function
default:"items => items"
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements.JavaScript
Templates
You can customize parts of a widget’s UI using the Templates API. Each template includes anhtml function,
which you can use as a tagged template.
This function safely renders templates as HTML strings and works directly in the browser—no build step required.
For details, see Templating your UI.
The
html function is available in InstantSearch.js version 4.46.0 or later.string | function
The template to customize each
option element. It exposes:label: string. The label to display in the option.value: string. The value for the option.count: number. The number of hits that match this value.isRefined: boolean. Indicates if it’s the current refined value.
string | function
The template to customize the first option of the select.
HTML output
HTML
Customize the UI with connectMenu
If you want to create your own UI of the menuSelect widget, you can use connectors.
This connector is also used to build the
menu widget.connectMenu, you can import it with the declaration relevant to how you installed InstantSearch.js.
JavaScript
Create a render function
This rendering function is called before the first search (init lifecycle step)
and each time results come back from Algolia (render lifecycle step).
JavaScript
If SEO is important for your search page, ensure that your custom HTML is optimized for search engines:
- Use
<a>tags withhrefattributes to allow search engine bots to follow links. - Use semantic HTML and include structured data when relevant.
Rendering options
object[]
The elements that can be refined for the current search results. With each item:
value: string. The value of the menu item.label: string. The label of the menu item.count: number. The number of matched results after a refinement is applied.isRefined: boolean. Indicates if the refinement is applied.
JavaScript
boolean
Returns
true if a refinement can be applied.JavaScript
function
Sets the refinement and triggers a search.
JavaScript
(eventType, facetValue) => void
The function to send
click events.
The click event is automatically sent when refine is called.
To learn more, see the insights middleware.eventType: 'click'facetValue: string
JavaScript
object
All original widget options forwarded to the render function.
JavaScript
Create and instantiate the custom widget
First, create your custom widgets using a rendering function. Then, instantiate them with parameters. There are two kinds of parameters you can pass:- Instance parameters. Predefined options that configure Algolia’s behavior.
- Custom parameters. Parameters you define to make the widget reusable and adaptable.
renderFunction, both instance and custom parameters are accessible through connector.widgetParams.
JavaScript
Instance options
string
required
The name of the attribute in the record.
JavaScript
number
default:10
The maximum number of values to display.
JavaScript
string[] | function
default:"['isRefined', 'name:asc']"
How to sort refinements. Must be one or more of the following strings:
"count:asc""count:desc""name:asc""name:desc""isRefined"
Array.sort.function
default:"items => items"
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements.JavaScript