Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <hr/> support to options_for_select #451

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/phoenix_html/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ defmodule Phoenix.HTML.Form do
#=> <option>France</option>
#=> </optgroup>

Horizontal separators can be added:

options_for_select(["Admin", "User", :hr, "New"], nil)
#=> <option>Admin</option>
#=> <option>User</option>
#=> <hr/>
#=> <option>New</option>

options_for_select(["Admin": "admin", "User": "user", hr: nil, "New": "new"], nil)
#=> <option value="admin" selected>Admin</option>
#=> <option value="user">User</option>
#=> <hr/>
#=> <option value="new">New</option>


"""
def options_for_select(options, selected_values) do
{:safe,
Expand All @@ -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)]

Expand All @@ -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)
Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions test/phoenix_html/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -252,31 +252,40 @@ defmodule Phoenix.HTML.FormTest do
~s(<option value="value">value</option>) <>
~s(<option selected value="novalue">novalue</option>)

assert options_for_select(~w(value novalue), "novalue") |> safe_to_string() ==
assert options_for_select(["value", :hr, "novalue"], "novalue") |> safe_to_string() ==
~s(<option value="value">value</option>) <>
~s(<hr/>) <>
~s(<option selected value="novalue">novalue</option>)

assert options_for_select(
[
[value: "value", key: "Value", disabled: true],
:hr,
[value: "novalue", key: "No Value"]
],
"novalue"
)
|> safe_to_string() ==
~s(<option disabled value="value">Value</option>) <>
~s(<hr/>) <>
~s(<option selected value="novalue">No Value</option>)

assert options_for_select(~w(value novalue), ["value", "novalue"]) |> safe_to_string() ==
~s(<option selected value="value">value</option>) <>
~s(<option selected value="novalue">novalue</option>)

assert options_for_select([Label: "value", hr: nil, New: "new"], nil) |> safe_to_string() ==
~s(<option value="value">Label</option>) <>
~s(<hr/>) <>
~s(<option value="new">New</option>)
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(<optgroup label="foo">) <>
~s(<option value="bar">bar</option>) <>
~s(<hr/>) <>
~s(<option value="baz">baz</option>) <>
~s(</optgroup>) <>
~s(<optgroup label="qux">) <>
Expand Down
Loading