Skip to content

Commit

Permalink
Add form_component to phx.gen.html
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Mar 2, 2023
1 parent f665be9 commit abb758e
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 71 deletions.
6 changes: 3 additions & 3 deletions guides/contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ description:string price:decimal views:integer

* creating lib/hello_web/controllers/product_controller.ex
* creating lib/hello_web/controllers/product_html/edit.html.heex
* creating lib/hello_web/controllers/product_html/form.html.heex
* creating lib/hello_web/controllers/product_html/form_component.html.heex
* creating lib/hello_web/controllers/product_html/index.html.heex
* creating lib/hello_web/controllers/product_html/new.html.heex
* creating lib/hello_web/controllers/product_html/show.html.heex
Expand Down Expand Up @@ -512,7 +512,7 @@ end

We added a new `category_select/2` function which uses `Phoenix.HTML.Form`'s `multiple_select/3` to generate a multiple select tag. We calculated the existing category IDs from our changeset, then used those values when we generate the select options for the input tag. We did this by enumerating over all of our categories and returning the appropriate `key`, `value`, and `selected` values. We marked an option as selected if the category ID was found in those category IDs in our changeset.

With our `category_select` function in place, we can open up `lib/hello_web/controllers/product_html/form.html.heex` and add:
With our `category_select` function in place, we can open up `lib/hello_web/controllers/product_html/form_component.html.heex` and add:

```diff
...
Expand Down Expand Up @@ -1054,7 +1054,7 @@ $ mix phx.gen.html Orders Order orders user_uuid:uuid total_price:decimal

* creating lib/hello_web/controllers/order_controller.ex
* creating lib/hello_web/controllers/order_html/edit.html.heex
* creating lib/hello_web/controllers/order_html/form.html.heex
* creating lib/hello_web/controllers/order_html/form_component.html.heex
* creating lib/hello_web/controllers/order_html/index.html.heex
* creating lib/hello_web/controllers/order_html/new.html.heex
* creating lib/hello_web/controllers/order_html/show.html.heex
Expand Down
6 changes: 3 additions & 3 deletions guides/mix_tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ The `mix phx.gen.html` task takes the following arguments: the module name of th
$ mix phx.gen.html Blog Post posts body:string word_count:integer
* creating lib/hello_web/controllers/post_controller.ex
* creating lib/hello_web/controllers/post_html/edit.html.heex
* creating lib/hello_web/controllers/post_html/form.html.heex
* creating lib/hello_web/controllers/post_html/form_component.html.heex
* creating lib/hello_web/controllers/post_html/index.html.heex
* creating lib/hello_web/controllers/post_html/new.html.heex
* creating lib/hello_web/controllers/post_html/show.html.heex
Expand Down Expand Up @@ -90,7 +90,7 @@ If we don't want to create a context or schema for our resource we can use the `
$ mix phx.gen.html Blog Post posts body:string word_count:integer --no-context
* creating lib/hello_web/controllers/post_controller.ex
* creating lib/hello_web/controllers/post_html/edit.html.heex
* creating lib/hello_web/controllers/post_html/form.html.heex
* creating lib/hello_web/controllers/post_html/form_component.html.heex
* creating lib/hello_web/controllers/post_html/index.html.heex
* creating lib/hello_web/controllers/post_html/new.html.heex
* creating lib/hello_web/controllers/post_html/show.html.heex
Expand All @@ -112,7 +112,7 @@ Similarly, if we want a context created without a schema for our resource we can
$ mix phx.gen.html Blog Post posts body:string word_count:integer --no-schema
* creating lib/hello_web/controllers/post_controller.ex
* creating lib/hello_web/controllers/post_html/edit.html.heex
* creating lib/hello_web/controllers/post_html/form.html.heex
* creating lib/hello_web/controllers/post_html/form_component.html.heex
* creating lib/hello_web/controllers/post_html/index.html.heex
* creating lib/hello_web/controllers/post_html/new.html.heex
* creating lib/hello_web/controllers/post_html/show.html.heex
Expand Down
2 changes: 1 addition & 1 deletion guides/testing/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ We've seen what Phoenix gives us with a newly generated app. Furthermore, whenev
$ mix phx.gen.html Blog Post posts title body:text
* creating lib/demo_web/controllers/post_controller.ex
* creating lib/demo_web/templates/post/edit.html.heex
* creating lib/demo_web/templates/post/form.html.heex
* creating lib/demo_web/templates/post/form_component.html.heex
* creating lib/demo_web/templates/post/index.html.heex
* creating lib/demo_web/templates/post/new.html.heex
* creating lib/demo_web/templates/post/show.html.heex
Expand Down
6 changes: 4 additions & 2 deletions lib/mix/tasks/phx.gen.html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ defmodule Mix.Tasks.Phx.Gen.Html do
Path.join([controller_pre, "#{singular}_html", "index.html.heex"])},
{:eex, "new.html.heex", Path.join([controller_pre, "#{singular}_html", "new.html.heex"])},
{:eex, "show.html.heex", Path.join([controller_pre, "#{singular}_html", "show.html.heex"])},
{:eex, "form_component.html.heex",
Path.join([controller_pre, "#{singular}_html", "form_component.html.heex"])},
{:eex, "html.ex", Path.join([controller_pre, "#{singular}_html.ex"])},
{:eex, "controller_test.exs", Path.join([test_pre, "#{singular}_controller_test.exs"])}
]
Expand Down Expand Up @@ -226,10 +228,10 @@ defmodule Mix.Tasks.Phx.Gen.Html do
end

defp default_options({:array, :string}),
do: Enum.map([1,2], &({"Option #{&1}", "option#{&1}"}))
do: Enum.map([1, 2], &{"Option #{&1}", "option#{&1}"})

defp default_options({:array, :integer}),
do: Enum.map([1,2], &({"#{&1}", &1}))
do: Enum.map([1, 2], &{"#{&1}", &1})

defp default_options({:array, _}), do: []

Expand Down
10 changes: 1 addition & 9 deletions priv/templates/phx.gen.html/edit.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
<:subtitle>Use this form to manage <%= schema.singular %> records in your database.</:subtitle>
</.header>

<.simple_form :let={f} for={@changeset} method="put" action={~p"<%= schema.route_prefix %>/#{@<%= schema.singular %>}"}>
<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<%= Mix.Tasks.Phx.Gen.Html.indent_inputs(inputs, 2) %>
<:actions>
<.button>Save <%= schema.human_singular %></.button>
</:actions>
</.simple_form>
<.form_component changeset={@changeset} action={~p"<%= schema.route_prefix %>/#{@<%= schema.singular %>}"} />

<.back navigate={~p"<%= schema.route_prefix %>"}>Back to <%= schema.plural %></.back>
9 changes: 9 additions & 0 deletions priv/templates/phx.gen.html/form_component.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<.simple_form :let={f} for={@changeset} action={@action}>
<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<%= Mix.Tasks.Phx.Gen.Html.indent_inputs(inputs, 2) %>
<:actions>
<.button>Save <%= schema.human_singular %></.button>
</:actions>
</.simple_form>
8 changes: 8 additions & 0 deletions priv/templates/phx.gen.html/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
use <%= inspect context.web_module %>, :html

embed_templates "<%= schema.singular %>_html/*"
@doc """
Renders a <%= schema.singular %> form.
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true

def form_component(assigns)
end
10 changes: 1 addition & 9 deletions priv/templates/phx.gen.html/new.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
<:subtitle>Use this form to manage <%= schema.singular %> records in your database.</:subtitle>
</.header>

<.simple_form :let={f} for={@changeset} action={~p"<%= schema.route_prefix %>"}>
<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<%= Mix.Tasks.Phx.Gen.Html.indent_inputs(inputs, 2) %>
<:actions>
<.button>Save <%= schema.human_singular %></.button>
</:actions>
</.simple_form>
<.form_component changeset={@changeset} action={~p"<%= schema.route_prefix %>"} />

<.back navigate={~p"<%= schema.route_prefix %>"}>Back to <%= schema.plural %></.back>
80 changes: 36 additions & 44 deletions test/mix/tasks/phx.gen.html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -129,59 +129,55 @@ defmodule Mix.Tasks.Phx.Gen.HtmlTest do
assert file =~ "defmodule PhoenixWeb.PostHTML"
end)

assert_file("lib/phoenix_web/controllers/post_html/edit.html.heex", fn file ->
assert file =~ ~s|~p"/posts|
end)

assert_file("lib/phoenix_web/controllers/post_html/index.html.heex", fn file ->
assert file =~ ~s|~p"/posts|
end)

assert_file("lib/phoenix_web/controllers/post_html/new.html.heex", fn file ->
assert file =~ ~s|~p"/posts|
assert file =~ ~S(action={~p"/posts"})
end)

assert_file("lib/phoenix_web/controllers/post_html/form_component.html.heex")

assert_file("lib/phoenix_web/controllers/post_html/show.html.heex", fn file ->
assert file =~ ~s|~p"/posts|
end)

assert_file("lib/phoenix_web/controllers/post_html/new.html.heex", fn file ->
assert file =~ ~S(<.simple_form :let={f} for={@changeset} action={~p"/posts"}>)
assert_file("lib/phoenix_web/controllers/post_html/edit.html.heex", fn file ->
assert file =~ ~S(action={~p"/posts/#{@post}"})
end)

assert_file("lib/phoenix_web/controllers/post_html/edit.html.heex", fn file ->
assert file =~
~S(<.simple_form :let={f} for={@changeset} method="put" action={~p"/posts/#{@post}"}>)
end)

for filename <- ["new.html.heex", "edit.html.heex"] do
assert_file("lib/phoenix_web/controllers/post_html/#{filename}", fn file ->
assert file =~ ~s(<.input field={f[:title]} type="text")
assert file =~ ~s(<.input field={f[:votes]} type="number")
assert file =~ ~s(<.input field={f[:cost]} type="number" label="Cost" step="any")
assert file =~ """
<.input
field={f[:tags]}
type="select"
multiple
"""
assert file =~ ~s(<.input field={f[:popular]} type="checkbox")
assert file =~ ~s(<.input field={f[:drafted_at]} type="datetime-local")
assert file =~ ~s(<.input field={f[:published_at]} type="datetime-local")
assert file =~ ~s(<.input field={f[:deleted_at]} type="datetime-local")
assert file =~ ~s(<.input field={f[:announcement_date]} type="date")
assert file =~ ~s(<.input field={f[:alarm]} type="time")
assert file =~ ~s(<.input field={f[:secret]} type="text" label="Secret" />)
assert file =~ """
<.input
field={f[:status]}
type="select"
"""
assert file =~ ~s|Ecto.Enum.values(Phoenix.Blog.Post, :status)|
assert_file("lib/phoenix_web/controllers/post_html/form_component.html.heex", fn file ->
assert file =~ ~S(<.simple_form :let={f} for={@changeset} action={@action}>)
assert file =~ ~s(<.input field={f[:title]} type="text")
assert file =~ ~s(<.input field={f[:votes]} type="number")
assert file =~ ~s(<.input field={f[:cost]} type="number" label="Cost" step="any")

refute file =~ ~s(<.input field={f[:user_id]})
end)
end
assert file =~ """
<.input
field={f[:tags]}
type="select"
multiple
"""

assert file =~ ~s(<.input field={f[:popular]} type="checkbox")
assert file =~ ~s(<.input field={f[:drafted_at]} type="datetime-local")
assert file =~ ~s(<.input field={f[:published_at]} type="datetime-local")
assert file =~ ~s(<.input field={f[:deleted_at]} type="datetime-local")
assert file =~ ~s(<.input field={f[:announcement_date]} type="date")
assert file =~ ~s(<.input field={f[:alarm]} type="time")
assert file =~ ~s(<.input field={f[:secret]} type="text" label="Secret" />)

assert file =~ """
<.input
field={f[:status]}
type="select"
"""

assert file =~ ~s|Ecto.Enum.values(Phoenix.Blog.Post, :status)|

refute file =~ ~s(<.input field={f[:user_id]})
end)

send(self(), {:mix_shell_input, :yes?, true})
Gen.Html.run(~w(Blog Comment comments title:string))
Expand Down Expand Up @@ -450,11 +446,7 @@ defmodule Mix.Tasks.Phx.Gen.HtmlTest do
in_tmp_project(config.test, fn ->
Gen.Html.run(~w(Blog Post posts status:enum:new))

assert_file("lib/phoenix_web/controllers/post_html/new.html.heex", fn file ->
assert file =~ ~s|Ecto.Enum.values(Phoenix.Blog.Post, :status)|
end)

assert_file("lib/phoenix_web/controllers/post_html/edit.html.heex", fn file ->
assert_file("lib/phoenix_web/controllers/post_html/form_component.html.heex", fn file ->
assert file =~ ~s|Ecto.Enum.values(Phoenix.Blog.Post, :status)|
end)
end)
Expand Down

0 comments on commit abb758e

Please sign in to comment.