josefarias / hotwire_combobox

An accessible autocomplete for Ruby on Rails.
https://hotwirecombobox.com
MIT License
436 stars 26 forks source link

Trigger stimulus action on other controller #137

Closed robingram closed 4 months ago

robingram commented 4 months ago

Thanks for your work on this, it looks like it will become a really nice component.

Can I ask if it is possible at the moment to call an action on another stimulus controller when an item is selected from the list? I've tried setting a data attribute directly on the combo box and also via input: ... but neither seems to be setting the action. Maybe that is still in the pipeline. :)

Thanks.

josefarias commented 4 months ago

Hey @robingram, thanks!

Yes, that's supported although not documented yet. This should work:

<div data-controller="foo">
  <%= combobox_tag :movie, [options], data: { action: "hw-combobox:selection->foo#doSomething" } %>
</div>

hw-combobox:selection is emitted when the combobox is closed and the selection is finalized. hw-combobox:preselection is emitted when the active option changes due to navigating with the keyboard or typing in the combobox.

You can use either, but my example above uses hw-combobox:selection

Finally, hw-combobox:removal is emitted in multiselect comboboxes only (not documented yet) when the user removes a selection chip.

Let me know if you run into trouble! Closing for now.

robingram commented 4 months ago

Thanks for the super speedy reply. That's working with hw-combobox:selection. 👍

omarluq commented 2 months ago

This is a super clean approach, thank you so much for the awesome lib. Can this be added to the docs please? It takes a while to find the closed issue and figure it out.