phoenix_live_view icon indicating copy to clipboard operation
phoenix_live_view copied to clipboard

`render_hook/3` fails without explicit `phx-target`

Open LostKobrakai opened this issue 1 year ago • 1 comments

Environment

  • Elixir version (elixir -v): 1.17.0
  • Phoenix version (mix deps): 1.7.0
  • Phoenix LiveView version (mix deps): 1.0.0-rc.4
  • Operating system: Mac OS
  • Browsers you attempted to reproduce this bug on (the more the merrier):
  • Does the problem persist after removing "assets/node_modules" and trying again? Yes/no:

Actual behavior

Using render_hook(element(view, "#hook-el-id"), "event", map) requires #hook-el-id to have a phx-target attribute for the render_hook/3 to work.

Expected behavior

The phx-target attribute seems to not be required outside of tests and it would seems to hold the same value as data-phx-component anyways. My expectation would be that phx-target shouldn't be required for render_hook/3 as well.

LostKobrakai avatar Jun 21 '24 08:06 LostKobrakai

The documentation says:

If you are pushing events from a hook to a component, then you must pass an element, created with element/3, as first argument and it must point to a single element on the page with a phx-target attribute in it:

The requirement that it must be a single element seems inconvenient, especially when my phx-hook (and therefore phx-target) is on a div containing other elements.

I had a test for a sortable list <div phx-hook="Sortable" working just fine with render_hook(view, "update-sorting", %{"ids" => sort}), but the moment I tried to use it in the Table component <tbody phx-hook="Sortable" phx-target="sortable-table, render_hook no longer returns the updated result, or complains about the selector not returning a single element.

foucist avatar Jun 28 '24 19:06 foucist