Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
docs(js): document JavaScript API
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Aug 25, 2020
1 parent 07b46af commit 6449742
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
83 changes: 83 additions & 0 deletions packages/website/docs/autocomplete-js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
id: autocomplete-js
title: autocomplete
---

This function creates a JavaScript autocomplete experience.

## Example

```js title="HTML"
<div id="autocomplete"></div>
```

```js title="JavaScript"
import algoliasearch from 'algoliasearch/lite';
import {
autocomplete,
getAlgoliaHits,
} from '@francoischalifour/autocomplete-js';

const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);

const autocomplete = autocomplete({
container: '#autocomplete',
getSources() {
return [
{
getInputValue: ({ suggestion }) => suggestion.query,
getSuggestions({ query }) {
return getAlgoliaHits({
searchClient,
queries: [
{
indexName: 'instant_search_demo_query_suggestions',
query,
params: {
hitsPerPage: 4,
},
},
],
});
},
},
];
},
});
```

## Reference

`autocomplete` accepts all the props that [`createAutocomplete`](/docs/createAutocomplete#reference) supports.

### Required props

#### `container`

> `string | HTMLElement`
The container for the autocomplete search box.

### Optional props

#### `render`

> `(params: { root: HTMLElement, sections: HTMLElement[], state: AutocompleteState<TItem> }) => void`
Function called to render the autocomplete results. It is useful for rendering sections in different row or column layouts.

The default implementation appends all the sections to the root:

```js
autocomplete({
// ...
render({ root, sections }) {
for (const section of sections) {
root.appendChild(section);
}
},
});
```
7 changes: 6 additions & 1 deletion packages/website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ module.exports = {
API: [
{
type: 'category',
label: 'Autocomplete',
label: 'Core',
items: ['createAutocomplete'],
},
{
type: 'category',
label: 'JavaScript',
items: ['autocomplete-js'],
},
{
type: 'category',
label: 'React',
Expand Down

0 comments on commit 6449742

Please sign in to comment.