How to trigger a (stimulus) action after selecting an entry? #207
Replies: 4 comments
-
I'm having the same issue. The documentation should include a list of events that the combo box can trigger and a list of all the properties. |
Beta Was this translation helpful? Give feedback.
-
I was able to do that by adding action to parent controller element
See https://stimulus.hotwired.dev/reference/controllers#cross-controller-coordination-with-events for more info Following should also work
You can find other events in https://github.com/josefarias/hotwire_combobox/blob/main/app/assets/javascripts/hw_combobox/models/combobox/events.js |
Beta Was this translation helpful? Give feedback.
-
Here's how I managed to get it working with builder.combobox(input_name, path, value:, options:) do |combobox|
combobox.customize_fieldset(data: { action: "hw-combobox:selection->auto-submit#submit" })
end Unrelated: In order to have the record title shown in the async combobox on page load, I use: options = HotwireCombobox::Listbox::Item.collection_for(
template, [record], render_in: {}, include_blank: nil, display: :to_combobox_display
) Please let me know if there's simpler way to achieve this. The only thing I'd prefer for this solution would be for combobox to submit on clear/"X" pressed. But instead, it clears the hidden field value and opens the combobox. It dispatches @vlado how/where did you add Another solution that kind-of worked (with clearing on "X" as well) was by adding: this.hiddenFieldTarget.dispatchEvent(new Event("change")); below here: And: builder.combobox(input_name, path, value: current_value, options:) do |combobox|
combobox.customize_hidden_field(data: { action: "change->auto-submit#submit" })
end When clearing the currently selected item by pressing "X", the input gets cleared immediately, @josefarias what do you think: should changing hidden field value fire DOM or Custom events? Or how would you recommend I solve this or just leave it as-is without immediately submitting on clear/"X"? |
Beta Was this translation helpful? Give feedback.
-
@vfonic great question 👍 I see now that my answer is a bit confusing. In my case I've added it to the parent receiving controller element as described in https://stimulus.hotwired.dev/reference/controllers#cross-controller-coordination-with-events (see updated answer above). |
Beta Was this translation helpful? Give feedback.
-
I want to replace the current Rails form helper:
... with the equivalent Hotwire Combobox:
While the code works I have not yet found a way to trigger my stimulus action (
input->search-form#search
) after an entry was selected.How to integrate frontend interactivity?
Beta Was this translation helpful? Give feedback.
All reactions