Skip to content

Commit

Permalink
add options attribute to inputs_for/1 (#2981)
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie authored Jan 3, 2024
1 parent 9855194 commit 42ef155
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/phoenix_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,14 @@ defmodule Phoenix.Component do
"""
)

attr.(:options, :list,
default: [],
doc: """
Any additional options for the `Phoenix.HTML.FormData` protocol
implementation.
"""
)

slot.(:inner_block, required: true, doc: "The content rendered for each nested form.")

@persistent_id "_persistent_id"
Expand All @@ -2415,6 +2423,7 @@ defmodule Phoenix.Component do
parent_form.options
|> Keyword.take([:multipart])
|> Keyword.merge(options)
|> Keyword.merge(assigns.options)

forms = parent_form.impl.to_form(parent_form.source, parent_form, field_name, options)
seen_ids = for f <- forms, vid = f.params[@persistent_id], into: %{}, do: {vid, true}
Expand Down
20 changes: 20 additions & 0 deletions test/phoenix_component/components_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,26 @@ defmodule Phoenix.LiveView.ComponentsTest do
</form>
"""
end

test "with FormData implementation options" do
assigns = %{}

template = ~H"""
<.form :let={f} as={:myform}>
<.inputs_for
:let={finner}
field={f[:inner]}}
options={[foo: "bar"]}
>
<p><%= finner.options[:foo] %></p>
</.inputs_for>
</.form>
"""

html = t2h(template)
assert [p] = Floki.find(html, "p")
assert Floki.text(p) =~ "bar"
end
end

describe "live_file_input/1" do
Expand Down

0 comments on commit 42ef155

Please sign in to comment.