josefarias / hotwire_combobox

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

Support for turbo's morphing with `prefilled_display_value` #128

Closed mmhan closed 4 months ago

mmhan commented 4 months ago

I'm using this gem together with turbo's morphing turned on. Everything seems to be working perfectly except there is one problem with prefilled_display_value.

Although turbo did morph the component (the <fieldset>) the value in prefilled_display_value does not show up as prefilled.

Steps to reproduce the behavior:

  1. Set up combobox with associated object. E.g Post with belongs_to :category
  2. Turn on turbo morph with %meta{name: "turbo-refresh-method", content: "morph"} (haml)
  3. In posts#new set up the post with a default category - @post = Post.new category_id: params[:category_id]
  4. Verify that prefilled_display_value exist in returned HTML in posts#new and that the combobox shows the prefilled category's name in combobox <input>
  5. Submit the form to posts#create with some validation errors in it. Perhaps an empty title.
  6. Verify that fieldset has prefilled_display_value is the same category name as before.

Expected behavior prefilled_display_value should show up in combobox <input>

Screenshots Screenshot 2024-03-30 at 11 53 17

Sidenote: It looks as though there is no actual input[value] attribute for both cases (with or without morph). I wonder how the prefilled value was used to display it.

mmhan commented 4 months ago

It looks like the value is set here, via connect(). I believe connect() is never recalled for turbo morphing. Not sure how one would go about resolving this bug yet.

https://github.com/josefarias/hotwire_combobox/blob/068cbca5bc7ccfe1e841b7258a165cfb3a7120ca/app/assets/javascripts/hw_combobox/models/combobox/selection.js#L10-L15

josefarias commented 4 months ago

Great catch! Thanks for the report. Fixed in https://github.com/josefarias/hotwire_combobox/pull/132