This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
forked from algolia/autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07b46af
commit 6449742
Showing
2 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters