-
-
Save Softknobs/435c2f0e3aac9b2ad3e7c352c6db8694 to your computer and use it in GitHub Desktop.
Problem using the same value on a re-rendered “modal” livecomponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TestWeb.FieldComponent do | |
use Phoenix.LiveComponent | |
def render(assigns) do | |
TestWeb.MainView.render("field_component.html", assigns) | |
end | |
def mount(socket) do | |
socket = | |
socket | |
|> assign(:search, "") | |
|> assign(:criteria, "") | |
{:ok, socket} | |
end | |
def handle_event("hide_component", _, socket) do | |
send self(), {:show_component, false} | |
{:noreply, socket |> reset_values} | |
end | |
def handle_event("criteria", %{"criteria_field" => criteria}, socket) do | |
{:noreply, assign(socket, :criteria, criteria)} | |
end | |
defp reset_values(socket) do | |
socket = | |
socket | |
|> assign(:search, "") | |
|> assign(:criteria, "") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="component_id" style="<%= unless @show_component do %>display: none;<% end %>"> | |
<form class="field has-addons is-100-percent" phx-change="criteria" phx-target="#component_id"> | |
<input type="text" value="<%= @search %>" name="criteria_field"/> | |
</form> | |
<p><%= @criteria %></p> | |
<button class="button" phx-click="hide_component" phx-target="#component_id">Hide</button> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<button class="button" phx-click="show_component">Show</button> | |
<%= live_component @socket, SynthsonlineWeb.FieldComponent, show_component: @show_component, id: "component_1" %> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TestWeb.MainLive do | |
use Phoenix.LiveView | |
def mount(session, socket) do | |
socket = | |
socket | |
|> assign(:show_component, false) | |
{:ok, socket} | |
end | |
def render(assigns) do | |
Phoenix.View.render(TestWeb.MainView, "main.html", assigns) | |
end | |
def handle_event("show_component", _, socket) do | |
send self(), {:show_component, true} | |
{:noreply, socket} | |
end | |
def handle_info({:show_component, show}, socket) do | |
{:noreply, assign(socket, :show_component, show)} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TestWeb.MainLiveTest do | |
use TestWeb.ConnCase | |
use Phoenix.ConnTest | |
import Phoenix.LiveViewTest | |
alias Phoenix.LiveView.Socket | |
test "click on show button shows component", %{conn: conn} do | |
# When | |
{:ok, view, html} = live(conn, "/main") | |
# Then | |
# Click shows the component, thus "display: none" should not be there | |
refute render_click(view, :show_component, true) =~ "display: none" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment