Skip to content

Commit 2daaf98

Browse files
docs(js): add doc for elements API (#500)
Co-authored-by: Sarah Dayan <[email protected]>
1 parent 7628d09 commit 2daaf98

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

packages/website/docs/autocomplete-js.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ type ClassNames = Partial<{
147147

148148
### `render`
149149

150-
> `(params: { children: VNode, state: AutocompleteState<TItem>, sections: VNode[], createElement: Pragma, Fragment: PragmaFrag }) => void`
150+
> `(params: { children: VNode, elements: Elements, sections: VNode[], state: AutocompleteState<TItem>, createElement: Pragma, Fragment: PragmaFrag }) => void`
151151
152152
The function that renders the autocomplete panel. This is useful to customize the rendering, for example, using multi-row or multi-column layouts.
153153

@@ -164,6 +164,70 @@ autocomplete({
164164
});
165165
```
166166

167+
You can use `sections`, which holds the components tree of your autocomplete, to customize the wrapping layout.
168+
169+
```js
170+
import { render } from 'preact';
171+
172+
autocomplete({
173+
// ...
174+
render({ sections }, root) {
175+
render(
176+
<div className="aa-PanelLayout aa-Panel--scrollable">{sections}</div>,
177+
root
178+
);
179+
},
180+
});
181+
```
182+
183+
If you need to split the content across a more complex layout, you can use `elements` instead to pick which source to display based on its [`sourceId`](sources#sourceid).
184+
185+
```js
186+
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';
187+
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
188+
import algoliasearch from 'algoliasearch';
189+
import { render } from 'preact';
190+
191+
const searchClient = algoliasearch(
192+
'latency',
193+
'6be0576ff61c053d5f9a3225e2a90f76'
194+
);
195+
const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
196+
key: 'search',
197+
});
198+
const querySuggestionsPlugin = createQuerySuggestionsPlugin({
199+
searchClient,
200+
indexName: 'instant_search_demo_query_suggestions',
201+
});
202+
203+
autocomplete({
204+
// ...
205+
plugins: [recentSearchesPlugin, querySuggestionsPlugin],
206+
getSources({ query }) {
207+
return [
208+
{
209+
sourceId: 'products',
210+
// ...
211+
},
212+
];
213+
},
214+
render({ elements }, root) {
215+
const { recentSearchesPlugin, querySuggestionsPlugin, products } = elements;
216+
217+
render(
218+
<div className="aa-PanelLayout aa-Panel--scrollable">
219+
<div>
220+
{recentSearchesPlugin}
221+
{querySuggestionsPlugin}
222+
</div>
223+
<div>{products}</div>
224+
</div>,
225+
root
226+
);
227+
},
228+
});
229+
```
230+
167231
### `renderNoResults`
168232

169233
> `(params: { children: VNode, state: AutocompleteState<TItem>, sections: VNode[], createElement: Pragma, Fragment: PragmaFrag }) => void`

0 commit comments

Comments
 (0)