The @appbaseio/autocomplete-suggestions-plugin is a Suggestions plugin that adds Query Suggestions powered by appbase-js client, to your autocomplete. It also provides rich customisations of UI supported by autocomplete-js.
API Definition
The @appbaseio/autocomplete-suggestions-plugin comes with a rich API to customize the search experience.
The createSuggestionsPlugin() function accepts three parameters, and returns a plugin object accepted by the autocomplete-js.
createSuggestionsPlugin(
appbaseClientConfig,
queryConfig,
renderConfig,
) => { // plugin object } -
appbaseClientConfig
ObjectRequiredIt is the first parameter accepted by
createSuggestionsPlugin(), used for connecting to the appbase client. It accepts the following properties:- enableTelemetry
Booleanwhen set tofalse, it disables telemetry. Defaults totrue. - settings
Objectan object consisting of the properties to control your search experience. Read more here.
Property
Type
Required
Description
url StringtrueAppbase.io cluster URL. app Stringtrueappbase.io search index name as displayed in the dashboard. username Stringfalseusername as displayed in the access control dashboard. password Stringfalsepassword as displayed in the access control dashboard. credentials Stringtrue*StringRequiredAPI key to access the cluster.credentialsare not required if,urlalready contains it.enableTelemetry Booleanfalsewhen set to false, it disables telemetry. Defaults totrue.settings Objectfalsean object consisting of the properties to control your search experience. Read more here. - enableTelemetry
-
queryConfig
ObjectRequiredIt is an object representing a ReactiveSearch suggestion query. Read about the properties accepted in this object here.
-
renderConfig
ObjectAlthough the plugin already comes with a default UI interface, One can customize the UI/ UX according to their needs using the renderConfig object**.**
It accepts the following properties:
-
renderItem
FunctionIt is a callback that accepts parameters API ref, one of them is the suggestion item itself, utilize it to render a custom UI for every suggestion.
createSuggestionsPlugin( ..., ..., { renderItem: (props) => { const { item, setQuery, refresh } = props; if (item.type === "index") { return ( <a className="aa-item product-item" href={item._source.url} target="_blank" rel="noreferrer" > <div className="product-image"> <img src={item._source.image} alt={item.value} /> </div> <div className="product-details"> <h4>{item.value}</h4> <p>{item._source.shortDescription}</p> </div> </a> ); } }, } ) -
onItemSelect
FunctionIt is a callback that accepts parameters API ref, one of them is the
setQueryfunction, utilize it to customize the behavior of what happens when an individual suggestion item is clicked.createSuggestionsPlugin( ..., ..., { renderItem: (props) => { ... }, onItemSelect: (props) => { const { item: { url, label }, setQuery, refresh } = props; if (url) { setQuery(label); window.open(url); } else { setQuery(label); refresh(); } }, } ) -
renderHeader
FunctionIt is a callback that accepts parameters API ref, one may utilize it to render a custom header before the suggestions.
createSuggestionsPlugin( ..., ..., { renderItem: (props) => { ... }, onItemSelect: (props) => { ... }, renderHeader: (props) => { return ( <h4> Products Listing <hr style={{ borderColor: "#d7d5f5" }} /> </h4> ); }, } ) -
renderFooter
FunctionIt is a callback that accepts parameters API ref, one may utilize it to render a custom footer after the suggestions.
createSuggestionsPlugin( ..., ..., { renderItem: (props) => { ... }, onItemSelect: (props) => { ... }, renderHeader: (props) => { ... }, renderFooter: (props) => { return <hr style={{ borderColor: "#d7d5f5" }} />; }, } ) -
renderNoResults
FunctionIt is a callback that accepts parameters API ref, one may utilize it to render a custom UI when no results are found.
createSuggestionsPlugin( ..., ..., { renderItem: (props) => { ... }, onItemSelect: (props) => { ... }, renderHeader: (props) => { ... }, renderFooter: (props) => { ... }, renderNoResults: (props) => { if (props.state.query === "") { return <p>Search for something to get direct product suggestions!</p>; } else { return <p>No products found! Try searching for something else!</p>; } } } ) -
useContextValue
BooleanWhen set to true, the context value is set with the following value:{ total: ..., // total results found for the entered query time: ..., // total time taken to perform the search resultsJson: ... // the results that were found in json format }One can use this context value to display results stats.
createSuggestionsPlugin( ..., ..., { renderItem: (props) => { ... }, onItemSelect: (props) => { ... }, renderHeader: (props) => { ... }, renderFooter: (props) => { ... }, renderNoResults: (props) => { if (props.state.query === "") { return <p>Search for something to get direct product suggestions!</p>; } else { return <p>No products found! Try searching for something else!</p>; } }, useContextValue: true } )
-
Example with all properties
Click here to checkout the advanced example to see all properties in action.