Skip to content

Commit

Permalink
Add id attribute for live_img_preview (#2691)
Browse files Browse the repository at this point in the history
When needing to use this component multiple times, we need a way to override the id.
  • Loading branch information
Neophen authored Sep 19, 2023
1 parent 633f356 commit 64cbc25
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/phoenix_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2817,20 +2817,33 @@ defmodule Phoenix.Component do
<.live_img_preview entry={entry} width="75" />
<% end %>
```
When you need to use it multiple times, make sure that they have distinct ids
```heex
<%= for entry <- @uploads.avatar.entries do %>
<.live_img_preview entry={entry} width="75" />
<% end %>
<%= for entry <- @uploads.avatar.entries do %>
<.live_img_preview id={"modal-#{entry.ref}"} entry={entry} width="500" />

Check warning on line 2830 in lib/phoenix_component.ex

View workflow job for this annotation

GitHub Actions / mix test (OTP 24.3 | Elixir 1.13.4)

variable "entry" does not exist and is being expanded to "entry()", please use parentheses to remove the ambiguity or change the variable name

Check failure on line 2830 in lib/phoenix_component.ex

View workflow job for this annotation

GitHub Actions / mix test (OTP 24.3 | Elixir 1.13.4)

** (CompileError) lib/phoenix_component.ex:2830: undefined function entry/0 (there is no such import)
<% end %>
```
"""
@doc type: :component

attr.(:entry, Phoenix.LiveView.UploadEntry,
required: true,
doc: "The `Phoenix.LiveView.UploadEntry` struct"
)

attr.(:id, :string, default: nil, doc: "Override the id if using live_img_preview more than once in the template")
attr.(:rest, :global, [])

def live_img_preview(assigns) do
~H"""
<img
id={"phx-preview-#{@entry.ref}"}
id={@id || "phx-preview-#{@entry.ref}"}
data-phx-upload-ref={@entry.upload_ref}
data-phx-entry-ref={@entry.ref}
data-phx-hook="Phoenix.LiveImgPreview"
Expand Down

0 comments on commit 64cbc25

Please sign in to comment.