trevoreyre / autocomplete

Accessible autocomplete component for vanilla JavaScript and Vue.
https://autocomplete.trevoreyre.com
MIT License
432 stars 75 forks source link

"onUpdate" being called on every event on the page. #127

Open ariademur opened 3 years ago

ariademur commented 3 years ago

Hello,

Using vanilla JS, I noticed that "onUpdate" is being called on any event on the page, not only when the results list is updated, as the documentation states.

Here's a Codepen example forked from "Simple autocomplete - @trevoreyre/autocomplete-js" https://codepen.io/ariademur/pen/wvoQErz

Even if clicking around the page, no relation to the autocomplete, the "onUpdate" is called.

If this is intentional, is there any other way to get the functionality specified in the documentation, a function that is executed when the results list is updated?

Thanks in advance.

trevoreyre commented 3 years ago

I think that's happening because of the document click listener, which always calls the function to close the results list, even if it's already closed.

https://github.com/trevoreyre/autocomplete/blob/master/packages/autocomplete-js/Autocomplete.js#L194-L199

I think a fairly easy solution would be to just add a check in that function to see if the results list is already open, and only close it if it is.

ariademur commented 3 years ago

Thanks!

For my purposes I ended up using a custom modified version of your script, but I will follow the project closely and looking forward to updates, I think it's one for the best autocomplete systems currently available, good job!