Skip to content

Commit

Permalink
Setting unique ids to live_components
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinst committed Sep 8, 2023
1 parent c9022f8 commit 9ad444f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/components/live_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule CrunchBerry.Components.LiveHelpers do
@moduledoc """
Helpers for LiveViews.
"""
import Phoenix.LiveView.Helpers
import Phoenix.Component

alias CrunchBerry.Components.FlashMessage
Expand Down Expand Up @@ -58,10 +57,16 @@ defmodule CrunchBerry.Components.LiveHelpers do
@spec live_modal(map) :: Phoenix.LiveView.Component.t()
def live_modal(assigns) do
assigns
|> Map.merge(%{id: :modal, module: Modal, opts: assigns})
|> Map.merge(%{id: "modal-#{unique_id()}", module: Modal, opts: assigns})
|> live_component()
end

defp unique_id do
make_ref()
|> inspect()
|> String.replace(~r"#Reference\<(\d+)\.(\d+)\.(\d+)\.(\d+)\>", "\\1_\\2_\\3_\\4")
end

@doc """
Renders `CrunchBerry.Components.Pagination`.
Expand Down Expand Up @@ -114,7 +119,7 @@ defmodule CrunchBerry.Components.LiveHelpers do
@spec live_pagination(map) :: Phoenix.LiveView.Component.t()
def live_pagination(assigns) do
assigns
|> Map.merge(%{id: :pagination, module: Pagination})
|> Map.merge(%{id: "pagination-#{unique_id()}", module: Pagination})
|> live_component()
end

Expand Down Expand Up @@ -247,7 +252,7 @@ defmodule CrunchBerry.Components.LiveHelpers do
@spec live_type_ahead(map) :: Phoenix.LiveView.Component.t()
def live_type_ahead(assigns) do
assigns
|> Map.merge(%{id: :type_ahead, module: TypeAhead})
|> Map.merge(%{id: "type-ahead-#{unique_id()}", module: TypeAhead})
|> live_component()
end

Expand Down

0 comments on commit 9ad444f

Please sign in to comment.