pstanoev / simple-svelte-autocomplete

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

Long delay when clearing the search term #222

Closed rjnskl closed 2 months ago

rjnskl commented 2 months ago

I'm loading a fairly long list of options (over 3000 entries) but the search and select functionality is working great. There appears to be an issue when a search string doesn't yield any results, or the user doesn't click on any, and then I try to clear the search string by either slowly deleting the characters or trying to select all and delete.

When deleting the very last search character or using select-all + delete, the component seems to hang for a very long time before resetting. It takes longer than the initial page load. Debug console doesn't appear to log anything helpful when this happens.

rjnskl commented 2 months ago

Wrapping the following code snippet in if (keywordLen > 0) { ... } seems to fix it fwiw!

if (keywordLen > 0) { 

  let pos1 = 0
  do {
    pos1 = labelLowercaseNoAc.indexOf(keyword, pos1)
    if (pos1 >= 0) {
      let pos2 = pos1 + keywordLen
      positions.push([pos1, pos2])
      pos1 = pos2
    }
  } while (pos1 !== -1)

}