You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/assets/js/liveview/suggestions_dropdown.js

41 lines
1.1 KiB
JavaScript

// Courtesy of Benjamin von Polheim:
// https://blog.devgenius.io/build-a-performat-autocomplete-using-phoenix-liveview-and-alpine-js-8bcbbed17ba7
let suggestionsDropdown = function(id) {
return {
isOpen: false,
id: id,
open() { this.isOpen = true },
close() { this.isOpen = false },
focus: 0,
setFocus(f) {
this.focus = f;
},
select() {
this.$refs[`dropdown-${this.id}-option-${this.focus}`]?.click()
this.focusPrev()
},
scrollTo(idx) {
this.$refs[`dropdown-${this.id}-option-${idx}`]?.scrollIntoView(
{ block: 'center', behavior: 'smooth', inline: 'start' }
)
},
focusNext() {
const nextIndex = this.focus + 1
const total = this.$refs.suggestions?.childElementCount ?? 0
if (this.isOpen && nextIndex < total) {
this.setFocus(nextIndex)
this.scrollTo(nextIndex);
}
},
focusPrev() {
const nextIndex = this.focus - 1
if (this.isOpen && nextIndex >= 0) {
this.setFocus(nextIndex)
this.scrollTo(nextIndex)
}
},
}
}
window.suggestionsDropdown = suggestionsDropdown