pstanoev / simple-svelte-autocomplete

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

Wrong item selected on enter #149

Closed Kinosnai closed 2 years ago

Kinosnai commented 2 years ago

There is a unintended (I guess) behaviour when you press enter AFTER you have selected an item from the list with enter key. To recreate the issue go to demo page http://simple-svelte-autocomplete.surge.sh/?# and use the first example called "matching keyword strategy" from "advanced usage" tab.

On that example there is an array of animals:

const animals = [

On the autocomplete input field please type "bl" and three items will show on the filtered list array which are:

Use down arrow key to select "Black Horse". The filtered list will close and black horse will stay as value on the input field. Now if you press enter AGAIN the value will change to "White Horse".

I think this behaviour is unintended. For some reason the index [1] of "Black Horse" on the filtered list gets stored and then persists on the second "enter" but this time it selects the index from the whole items list, which in the animals array is "White Horse".

I guess after the first enter event there must be some kind of reseting on the first selected value or another solution could be to prevent the second enter happening on an empty field. Pressing enter for second time with minCharactersToSearch=1 or higher wont prevent this from happening either.

I hope my description helps to fix this, I love the multiple words searching function of this component!!

pstanoev commented 2 years ago

Thanks for reporting, reproduced, will be fixed.

pstanoev commented 2 years ago

Fixed in the latest version.