afcapel / stimulus-autocomplete

Stimulus autocomplete component
MIT License
478 stars 61 forks source link

Support button to open/close results #141

Open tmaier opened 1 year ago

tmaier commented 1 year ago

Suppose you have such a combobox:

image

When one closes the combobox, one should be able to re-open it by clicking on the button (to the right).

Please add a toggleResults or showResults method, one could bind to the button.

Lxxrxns commented 5 months ago

Yes, we need this method. It seems it doesn't even let me trigger showing the results with a .trigger("change") or keyup or whatever ...

Lxxrxns commented 5 months ago

Update:

I added a single line of code to the autocomplete.js file to allow external (programmatic) triggering of the search.

Add this to the connect() function in autocomplete.js:

document.addEventListener("doSearch", this.onInputChange);

Then, in your website/app, you can trigger this event manually by doing:

const event = new Event("doSearch"); 
document.dispatchEvent(event); 

For example, if you want to trigger the search using a button press, in jQuery you could do:

$('#yourButton').click(function(e){
  const event = new Event("doSearch"); 
  document.dispatchEvent(event);    
});

And voila, pressing the button with id="yourButton" will trigger the search.

It's a bit hacky, but I'm not a javascript expert.