josefarias / hotwire_combobox

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

Multiselect combobox empty after morph. #145

Closed thanosbellos closed 4 months ago

thanosbellos commented 4 months ago

Hello

We are using Hotwire_combobox with turbo 8 and morphing enabled. everything works as expected after morphing, for comboboxes with single selection enabled. But with multiselection and morphing we have encountered an issue:

https://github.com/josefarias/hotwire_combobox/assets/425283/ea661d03-af2d-497b-b7d4-65d6a6d3e34d

In the attached video you can notice that when we update the user with the visited states and we are redirected back to the edit page again, the combobox will be empty. The hidden field has the correct value (ids of the visited states). When you try to select some new options, the already selected ones won't be available. So at some degree it works. But the initialization and the profiling of the combobox may not work as expected.

Reconnecting the controllers after the element I s morphed is a great way to reconnect. I followed along the code and as I understand it works perfectly for single selection comboboxes because the HwComboboxController#_connectSelection will catch the prefilled value and display it as the query. https://github.com/josefarias/hotwire_combobox/blob/cbdbdc4a853194d0d9f542787e6990ad3d410995/app/presenters/hotwire_combobox/component.rb#L339 https://github.com/josefarias/hotwire_combobox/blob/cbdbdc4a853194d0d9f542787e6990ad3d410995/app/assets/javascripts/controllers/hw_combobox_controller.js#L72 https://github.com/josefarias/hotwire_combobox/blob/cbdbdc4a853194d0d9f542787e6990ad3d410995/app/assets/javascripts/hw_combobox/models/combobox/selection.js#L10-L14 I think that it may be needed to do something similar for multiselection. Just for experimentation I just used HwComboboxController #this._initializeMultiselect() in the HwComboboxController#idempotentConnect and it worked after the morph.

I have updated the repo I was using for another issue if you want to quickly test it out.

https://github.com/thanosbellos/hotwire-combobox-prefilled-value/tree/main

josefarias commented 4 months ago

Thanks @thanosbellos! Great find and report. Appreciate it. Should be fixed on master now.