Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 3d3233f

Browse files
author
Vincent Voyer
committed
feat(events): add suggestions and cursorchanged events
1 parent 32edb43 commit 3d3233f

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

docs/source/javascripts/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33

44
import './responsive-navigation.js';
55

6-
places({
6+
const placesAutocomplete = places({
77
container: document.querySelector('#landing-demo')
8-
}).on('change', suggestion => console.log(suggestion));
8+
});
9+
10+
const events = ['change', 'suggestions', 'cursorchanged'];
11+
events.forEach(eventName =>
12+
placesAutocomplete.on(eventName, eventData => {
13+
if (!Array.isArray(eventData)) {
14+
eventData = [eventData];
15+
}
16+
17+
console.log(`Algolia Places: received event **${eventName}**`);
18+
if (typeof console.table === 'function') {
19+
console.table(eventData);
20+
} else {
21+
console.log('event data:', eventData);
22+
}
23+
})
24+
);

src/places.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export default function places({
3737
const source = (query, cb) => client
3838
.search({query})
3939
.then(({hits}) => hits.slice(0, 5).map(hitFormatter))
40-
.then(hits => {console.log(hits); return hits;})
40+
.then(suggestions => {
41+
placesInstance.emit('suggestions', suggestions);
42+
return suggestions;
43+
})
4144
.then(cb)
4245
.catch(err => console.error(err));
4346

@@ -49,12 +52,15 @@ export default function places({
4952
}
5053
);
5154

52-
const autocompleteEvents = ['selected', 'autocompleted'];
53-
autocompleteEvents.forEach(eventName => {
55+
const autocompleteChangeEvents = ['selected', 'autocompleted'];
56+
autocompleteChangeEvents.forEach(eventName => {
5457
autocompleteInstance.on(`autocomplete:${eventName}`, (_, suggestion) => {
5558
placesInstance.emit('change', suggestion);
5659
});
5760
});
61+
autocompleteInstance.on('autocomplete:cursorchanged', (_, suggestion) => {
62+
placesInstance.emit('cursorchanged', suggestion);
63+
});
5864

5965
const autocompleteContainer = container.parentNode;
6066
autocompleteContainer.classList.add('algolia-places');

0 commit comments

Comments
 (0)