Skip to content

Commit

Permalink
docs: migrate example to JSX
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan committed Jan 21, 2021
1 parent cb3b47f commit a725011
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/website/docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,33 @@ You can also manually update the state using setters. It's useful to implement c
For example, let's say you want to let users fill the search input with the value of a suggestion by clicking or tapping it. You can use the [`setQuery`](state#setquery) setter provided by [`getSources`](sources#getsources) to attach an event when clicking the tap-ahead button and manually set the query.

```js
/** @jsx h */
import { h } from 'preact';
import { autocomplete } from '@algolia/autocomplete-js';

autocomplete({
// ...
getSources({ query, setQuery, refresh }) {
getSources({ setQuery, refresh }) {
return [
{
// ...
templates: {
item({ item, root }) {
const tapAheadButton = document.createElement('button');

tapAheadButton.addEventListener('click', (event) => {
event.stopPropagation();

setQuery(item.query);
refresh();
});

root.appendChild(tapAheadButton);
item({ item }) {
return (
<div>
<div>{item.query}</div>
<button
onClick={(event) => {
event.stopPropagation();

setQuery(item.query);
refresh();
}}
>
Fill query
</button>
</div>
);
},
},
},
Expand Down

0 comments on commit a725011

Please sign in to comment.