hungle00 / rondo_form

Same as Cocoon, but using StimulusJS https://rubygems.org/gems/rondo_form
MIT License
65 stars 4 forks source link

Update nested-rondo stimulus controller for links with blocks #11

Closed omarowns closed 2 months ago

omarowns commented 2 months ago

Noticed the following when using the link_to_add_association with a block like this:

<%= link_to_add_association @form, :photos, class: 'w-[200px] h-[200px] flex items-center justify-center' do %>
  <svg class="w-14 h-14 text-gray-800 dark:text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
    <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h14m-7 7V5"/>
  </svg>
<% end %>

When an inner element of the link is smaller than the containing anchor, and clicking that SVG element the nested-rondo controller uses the event.target of the SVG instead of the anchor, and the dataset.association accessors return undefined, thus the [new_photos] is never updated and the controller dismisses new records to be added.

This fix aims to traverse the element that was clicked until the one with the data-association or data-associations attributes are found and use that element for the rest of the method.

hungle00 commented 2 months ago

Thanks for your contribution! I'll release a new version soon. Before I upgrade the version, you can fetch this gem from GitHub source to see your changes.