Skip to content

Commit

Permalink
better passing of vars to autosuggest
Browse files Browse the repository at this point in the history
  • Loading branch information
fergiemcdowall committed Nov 14, 2023
1 parent 1b02b71 commit 9f44c49
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
6 changes: 5 additions & 1 deletion demo2/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ Promise.all([
paging: { elementId: 'paging', pageSize: 2 },
searchInput: {
elementId: 'searchbox',
suggestionsElementId: 'suggestions'
suggestions: {
elementId: 'suggestions'
// limit: 10,
// threshold: 1
}
}
})
)
14 changes: 10 additions & 4 deletions src/ui/Autocomplete.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export class Autocomplete {
constructor (searchboxEl, DICTIONARY, search, suggestionsElementId) {
this.DICTIONARY = DICTIONARY
constructor(searchboxEl, search, suggestions = {}) {
this.autoCompleteFunction = suggestions.autoCompleteFunction
this.currentFocus = -1
this.searchboxEl = searchboxEl
this.search = search
this.limit = suggestions.limit || 20
this.threshold = suggestions.threshold || 2
this.autocompleteListId = searchboxEl.id + '-autocomplete-list'
this.suggestionsElementId = suggestionsElementId
this.suggestionsElementId = suggestions.elementId
searchboxEl.addEventListener('input', this.searchBoxInputListener)
searchboxEl.addEventListener('keydown', this.searchBoxKeydownListener)
document.addEventListener('click', e => {
Expand Down Expand Up @@ -54,12 +56,16 @@ export class Autocomplete {
}

searchBoxInputListener = async e => {
const suggestions = await this.DICTIONARY(e.target.value)
const suggestions = (await this.autoCompleteFunction(e.target.value)).slice(
0,
this.limit
)
let b
const val = e.target.value
/* close any already open lists of autocompleted values */
this.closeAllLists()
if (!val) return false
if (val.length < this.threshold) return false
this.currentFocus = -1
/* create a DIV element that will contain the items (values): */
const a = document.createElement('DIV')
Expand Down
16 changes: 5 additions & 11 deletions src/ui/SearchInput.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Autocomplete } from './Autocomplete.js'

export class SearchInput {
constructor (
constructor(
{
elementId = 'searchInput',
suggestionsElementId = 'suggestions',
el = document.getElementById(elementId),
autoCompleteFunction = new Promise()
suggestions = {},
el = document.getElementById(elementId)
// autoCompleteFunction = new Promise()
},
search,
paging
Expand All @@ -16,13 +16,7 @@ export class SearchInput {
search('searchInput')
})

// TODO: this api should probably be cleaned up
this.autocomplete = new Autocomplete(
el,
autoCompleteFunction,
search,
suggestionsElementId
)
this.autocomplete = new Autocomplete(el, search, suggestions)

this.el = el
}
Expand Down
10 changes: 7 additions & 3 deletions src/ui/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Facet } from './Facet.js'
import { SearchInput } from './SearchInput.js'

export class UI {
constructor ({
constructor({
index = null,
count = {},
hits = {},
Expand All @@ -20,8 +20,12 @@ export class UI {
this.hits = new Hits(hits)
this.searchInput = new SearchInput(
{
autoCompleteFunction: this.index.DICTIONARY,
...searchInput
// autoCompleteFunction: this.index.DICTIONARY,
...searchInput,
suggestions: {
autoCompleteFunction: this.index.DICTIONARY,
...searchInput.suggestions
}
},
this.search,
this.paging
Expand Down

0 comments on commit 9f44c49

Please sign in to comment.