From d30c20b65922395cbae86e64744d990d5fb2b1ea Mon Sep 17 00:00:00 2001 From: Gilbert Bishop-White Date: Fri, 18 Oct 2024 11:53:59 +0100 Subject: [PATCH 1/3] Add
to options_for_select --- lib/phoenix_html/form.ex | 25 +++++++++++++++++++++++++ test/phoenix_html/form_test.exs | 17 +++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex index 1ff839d..9ae2fbd 100644 --- a/lib/phoenix_html/form.ex +++ b/lib/phoenix_html/form.ex @@ -297,6 +297,21 @@ defmodule Phoenix.HTML.Form do #=> #=> + Horizontal seperators can be added: + + options_for_select(["Admin", "User", :hr, "New"], nil) + #=> + #=> + #=>
+ #=> + + options_for_select(["Admin": "admin", "User": "user", hr: nil, "New": "new"], nil) + #=> + #=> + #=>
+ #=> + + """ def options_for_select(options, selected_values) do {:safe, @@ -308,6 +323,9 @@ defmodule Phoenix.HTML.Form do defp escaped_options_for_select(options, selected_values) do Enum.reduce(options, [], fn + {:hr, nil}, acc -> + [acc | hr_tag()] + {option_key, option_value}, acc -> [acc | option(option_key, option_value, [], selected_values)] @@ -326,6 +344,9 @@ defmodule Phoenix.HTML.Form do [acc | option(option_key, option_value, options, selected_values)] + :hr, acc -> + [acc | hr_tag()] + option, acc -> [acc | option(option, option, [], selected_values)] end) @@ -349,6 +370,10 @@ defmodule Phoenix.HTML.Form do [?<, name, attrs, ?>, body, ?<, ?/, name, ?>] end + defp hr_tag() do + [?<, "hr", ?/, ?>] + end + # Helper for getting field errors, handling string fields defp field_errors(errors, field) when is_list(errors) and (is_atom(field) or is_binary(field)) do diff --git a/test/phoenix_html/form_test.exs b/test/phoenix_html/form_test.exs index 75fb40f..592c77b 100644 --- a/test/phoenix_html/form_test.exs +++ b/test/phoenix_html/form_test.exs @@ -252,31 +252,40 @@ defmodule Phoenix.HTML.FormTest do ~s() <> ~s() - assert options_for_select(~w(value novalue), "novalue") |> safe_to_string() == - ~s() <> - ~s() + assert options_for_select(["value", :hr, "novalue"], "novalue") |> safe_to_string() == + ~s() <> + ~s(
) <> + ~s() assert options_for_select( [ [value: "value", key: "Value", disabled: true], + :hr, [value: "novalue", key: "No Value"] ], "novalue" ) |> safe_to_string() == ~s() <> + ~s(
) <> ~s() assert options_for_select(~w(value novalue), ["value", "novalue"]) |> safe_to_string() == ~s() <> ~s() + + assert options_for_select(["Label": "value", hr: nil, "New": "new"], nil) |> safe_to_string() == + ~s() <> + ~s(
) <> + ~s() end test "with groups" do - assert options_for_select([{"foo", ~w(bar baz)}, {"qux", ~w(qux quz)}], "qux") + assert options_for_select([{"foo", ["bar", :hr, "baz"]}, {"qux", ~w(qux quz)}], "qux") |> safe_to_string() == ~s() <> ~s() <> + ~s(
) <> ~s() <> ~s(
) <> ~s() <> From 2cea68e9820099ad89e88fa2d7ee3f6a7fdebcbd Mon Sep 17 00:00:00 2001 From: Gilbert Bishop-White Date: Fri, 18 Oct 2024 13:29:51 +0100 Subject: [PATCH 2/3] Fix formatting --- test/phoenix_html/form_test.exs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/phoenix_html/form_test.exs b/test/phoenix_html/form_test.exs index 592c77b..523b76f 100644 --- a/test/phoenix_html/form_test.exs +++ b/test/phoenix_html/form_test.exs @@ -253,9 +253,9 @@ defmodule Phoenix.HTML.FormTest do ~s() assert options_for_select(["value", :hr, "novalue"], "novalue") |> safe_to_string() == - ~s() <> - ~s(
) <> - ~s() + ~s() <> + ~s(
) <> + ~s() assert options_for_select( [ @@ -274,10 +274,10 @@ defmodule Phoenix.HTML.FormTest do ~s() <> ~s() - assert options_for_select(["Label": "value", hr: nil, "New": "new"], nil) |> safe_to_string() == - ~s() <> - ~s(
) <> - ~s() + assert options_for_select([Label: "value", hr: nil, New: "new"], nil) |> safe_to_string() == + ~s() <> + ~s(
) <> + ~s() end test "with groups" do From 465c303eb90570da812dee7860107372ae424a98 Mon Sep 17 00:00:00 2001 From: Steffen Deusch Date: Mon, 21 Oct 2024 14:48:30 +0200 Subject: [PATCH 3/3] Update lib/phoenix_html/form.ex --- lib/phoenix_html/form.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex index 9ae2fbd..0699aeb 100644 --- a/lib/phoenix_html/form.ex +++ b/lib/phoenix_html/form.ex @@ -297,7 +297,7 @@ defmodule Phoenix.HTML.Form do #=> #=>
- Horizontal seperators can be added: + Horizontal separators can be added: options_for_select(["Admin", "User", :hr, "New"], nil) #=>