Skip to content

Commit 6b67a08

Browse files
authored
Merge pull request #451 from gilbertbw/gbw-hr
Add <hr/> support to options_for_select
2 parents c966418 + 465c303 commit 6b67a08

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Diff for: lib/phoenix_html/form.ex

+25
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,21 @@ defmodule Phoenix.HTML.Form do
297297
#=> <option>France</option>
298298
#=> </optgroup>
299299
300+
Horizontal separators can be added:
301+
302+
options_for_select(["Admin", "User", :hr, "New"], nil)
303+
#=> <option>Admin</option>
304+
#=> <option>User</option>
305+
#=> <hr/>
306+
#=> <option>New</option>
307+
308+
options_for_select(["Admin": "admin", "User": "user", hr: nil, "New": "new"], nil)
309+
#=> <option value="admin" selected>Admin</option>
310+
#=> <option value="user">User</option>
311+
#=> <hr/>
312+
#=> <option value="new">New</option>
313+
314+
300315
"""
301316
def options_for_select(options, selected_values) do
302317
{:safe,
@@ -308,6 +323,9 @@ defmodule Phoenix.HTML.Form do
308323

309324
defp escaped_options_for_select(options, selected_values) do
310325
Enum.reduce(options, [], fn
326+
{:hr, nil}, acc ->
327+
[acc | hr_tag()]
328+
311329
{option_key, option_value}, acc ->
312330
[acc | option(option_key, option_value, [], selected_values)]
313331

@@ -326,6 +344,9 @@ defmodule Phoenix.HTML.Form do
326344

327345
[acc | option(option_key, option_value, options, selected_values)]
328346

347+
:hr, acc ->
348+
[acc | hr_tag()]
349+
329350
option, acc ->
330351
[acc | option(option, option, [], selected_values)]
331352
end)
@@ -349,6 +370,10 @@ defmodule Phoenix.HTML.Form do
349370
[?<, name, attrs, ?>, body, ?<, ?/, name, ?>]
350371
end
351372

373+
defp hr_tag() do
374+
[?<, "hr", ?/, ?>]
375+
end
376+
352377
# Helper for getting field errors, handling string fields
353378
defp field_errors(errors, field)
354379
when is_list(errors) and (is_atom(field) or is_binary(field)) do

Diff for: test/phoenix_html/form_test.exs

+11-2
Original file line numberDiff line numberDiff line change
@@ -252,31 +252,40 @@ defmodule Phoenix.HTML.FormTest do
252252
~s(<option value="value">value</option>) <>
253253
~s(<option selected value="novalue">novalue</option>)
254254

255-
assert options_for_select(~w(value novalue), "novalue") |> safe_to_string() ==
255+
assert options_for_select(["value", :hr, "novalue"], "novalue") |> safe_to_string() ==
256256
~s(<option value="value">value</option>) <>
257+
~s(<hr/>) <>
257258
~s(<option selected value="novalue">novalue</option>)
258259

259260
assert options_for_select(
260261
[
261262
[value: "value", key: "Value", disabled: true],
263+
:hr,
262264
[value: "novalue", key: "No Value"]
263265
],
264266
"novalue"
265267
)
266268
|> safe_to_string() ==
267269
~s(<option disabled value="value">Value</option>) <>
270+
~s(<hr/>) <>
268271
~s(<option selected value="novalue">No Value</option>)
269272

270273
assert options_for_select(~w(value novalue), ["value", "novalue"]) |> safe_to_string() ==
271274
~s(<option selected value="value">value</option>) <>
272275
~s(<option selected value="novalue">novalue</option>)
276+
277+
assert options_for_select([Label: "value", hr: nil, New: "new"], nil) |> safe_to_string() ==
278+
~s(<option value="value">Label</option>) <>
279+
~s(<hr/>) <>
280+
~s(<option value="new">New</option>)
273281
end
274282

275283
test "with groups" do
276-
assert options_for_select([{"foo", ~w(bar baz)}, {"qux", ~w(qux quz)}], "qux")
284+
assert options_for_select([{"foo", ["bar", :hr, "baz"]}, {"qux", ~w(qux quz)}], "qux")
277285
|> safe_to_string() ==
278286
~s(<optgroup label="foo">) <>
279287
~s(<option value="bar">bar</option>) <>
288+
~s(<hr/>) <>
280289
~s(<option value="baz">baz</option>) <>
281290
~s(</optgroup>) <>
282291
~s(<optgroup label="qux">) <>

0 commit comments

Comments
 (0)