In the example above, we supply autocomplete query parameters as an object. Additionally, the autocompleteQuery parameter can also be supplied as a function. The function flavor can be used for building complex query parameters and provides access to other pre-defined configuration parameters. Section below shows an example of autocompleteQuery supplied as a function which provides the product variation id by accessing the pre-defined variationId method from the default configuration.
The full list of Configuration options is documented here
Autocomplete component
autocomplete/index.jsx
import { useAppStateSelector, AutocompleteElement } from'@nosto/preact'exportdefault () => {const { products,keywords } =useAppStateSelector((state) => ({ products:state.response.products, keywords:state.response.keywords }))if (!products?.hits?.length&&!keywords?.hits?.length) {return }return ( <div> {keywords?.hits?.length>0&& <div> <div> Keywords </div> <div> {keywords.hits.map((hit) => ( <AutocompleteElementhit={hit} key={hit.keyword}> {hit?._highlight?.keyword? <spandangerouslySetInnerHTML={{ __html:hit._highlight.keyword }}></span>: <span>{hit.keyword}</span> } </AutocompleteElement> ))} </div> </div>} {products?.hits?.length>0&& <div> <div> Products </div> <div> {products.hits.map((hit) => ( <AutocompleteElementhit={hit} key={hit.productId} as="a"> <imgsrc={hit.imageUrl}/> <div> {hit.name} </div> <button// Allow the button to be clicked only oncedisabled={addedToCart}// Add the product to the cart when the button is clickedonClick={(event) => {// Don't navigate to the product pageevent.preventDefault()// Update the button text and disable itsetAddedToCart(true)// Add the product to the cart, this depends on the cart implementationjQuery.post("/cart/add.js", { quantity:1, id:product.productId }) }} > // Show different text if product was added to the cart {addedToCart ?"Added to cart":"Add to cart"} </button> </AutocompleteElement> ))} </div> </div>} <div> <buttontype="submit"> See all search results </button> </div> </div> )}
Element selection
Wrap each keywords and product to AutocompleteElement element - it will allow clicking or selecting the element directly with keyboard.
Search submit
To submit a search directly from the autocomplete, use the <button type="submit"> element. This will submit the search form.
History component
History component renders user search history. It is displayed when user clicks on empty search box.