pstanoev / simple-svelte-autocomplete

Simple Autocomplete / typeahead component for Svelte
http://simple-svelte-autocomplete.surge.sh/
MIT License
468 stars 79 forks source link

Changing items does not trigger updating filtered list items #144

Closed atomcat1978 closed 2 years ago

atomcat1978 commented 2 years ago

Updating items invoked prepareListItems ($: items, prepareListItems()), however the list of filteredListItems stays the same. This leads to inconsistent data view if the dropdown is opened again using the down arrow.

atomcat1978 commented 2 years ago

I bet, the list should be re-filtered. Something like what resetListToAllItemsAndOpen does.

atomcat1978 commented 2 years ago

Seems like a call to search does the trick:

  function prepareListItems() {
    let timerId
    if (debug) {
      timerId = `Autocomplete prepare list ${inputId ? `(id: ${inputId})` : ""}`
      console.time(timerId)
      console.log("Prepare items to search")
      console.log("items: " + JSON.stringify(items))
    }

    if (!Array.isArray(items)) {
      console.warn("Autocomplete items / search function did not return array but", items)
      items = []
    }

    const length = items ? items.length : 0
    listItems = new Array(length)

    if (length > 0) {
      items.forEach((item, i) => {
        const listItem = getListItem(item)
        if (listItem === undefined) {
          console.log("Undefined item for: ", item)
        }
        listItems[i] = listItem
      })
      search() // << -- Here
    }

    if (debug) {
      console.log(listItems.length + " items to search")
      console.timeEnd(timerId)
    }
  }
pstanoev commented 2 years ago

Thanks for reporting, I will have a look.

atomcat1978 commented 2 years ago

Just a comment: the suggested solution above does not handle if items are set to an empty/null list. So, possibly we should do a search call in all cases.

    if (length > 0) {
      items.forEach((item, i) => {
        const listItem = getListItem(item)
        if (listItem === undefined) {
          console.log("Undefined item for: ", item)
        }
        listItems[i] = listItem
      })
    }  
    search() // << -- Here

Also maybe search code could have a short circuit if the provided items are empty/null.

pstanoev commented 2 years ago

Should be fixed in 2.3.5.