phoenixframework / phoenix_live_view

Rich, real-time user experiences with server-rendered HTML
https://hex.pm/packages/phoenix_live_view
MIT License
5.99k stars 902 forks source link

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

Open LostKobrakai opened 2 weeks ago

LostKobrakai commented 2 weeks ago

Environment

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.

foucist commented 1 week ago

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.