From 7b01085a7fbe90f585f09e9b50e98e9556de3921 Mon Sep 17 00:00:00 2001 From: Johan Tell Date: Sat, 17 Mar 2018 22:28:21 +0100 Subject: [PATCH 1/3] Allow value option on labels This will allow labels to be used with dynamically created radiobuttons and checkboxes to have the correct prefix --- lib/phoenix_html/form.ex | 17 +++++++++++++++-- test/phoenix_html/form_test.exs | 5 +++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex index 190a5a02..12f13525 100644 --- a/lib/phoenix_html/form.ex +++ b/lib/phoenix_html/form.ex @@ -1433,6 +1433,9 @@ defmodule Phoenix.HTML.Form do "E-mail Address" end #=> + + label :user, :newsletters, "Recieve all newsletters", value: "all" + #=> """ def label(form, field) do label(form, field, humanize(field), []) @@ -1457,12 +1460,22 @@ defmodule Phoenix.HTML.Form do See `label/2`. """ def label(form, field, text, opts) when is_binary(text) and is_list(opts) do - opts = Keyword.put_new(opts, :for, input_id(form, field)) + {opts, input_id} = case Keyword.pop(opts, :value) do + {nil, opts} -> {opts, input_id(form, field)} + {value, opts} -> {opts, input_id(form, field, value)} + end + + opts = Keyword.put_new(opts, :for, input_id) content_tag(:label, text, opts) end def label(form, field, opts, do: block) do - opts = Keyword.put_new(opts, :for, input_id(form, field)) + {opts, input_id} = case Keyword.pop(opts, :value) do + {nil, opts} -> {opts, input_id(form, field)} + {value, opts} -> {opts, input_id(form, field, value)} + end + + opts = Keyword.put_new(opts, :for, input_id) content_tag(:label, opts, do: block) end diff --git a/test/phoenix_html/form_test.exs b/test/phoenix_html/form_test.exs index ec1228b0..bc6221b8 100644 --- a/test/phoenix_html/form_test.exs +++ b/test/phoenix_html/form_test.exs @@ -1173,6 +1173,11 @@ defmodule Phoenix.HTML.FormTest do ~s() end + test "label/4 with for_value option" do + assert safe_form(&label(&1, :key, value: "1")) == + ~s() + end + test "label/3 with a block" do assert safe_form(&label(&1, :key, do: "Hello")) == ~s() end From 5d26d94638fc781b619d93aee5cada604bcd0fea Mon Sep 17 00:00:00 2001 From: Johan Tell Date: Sat, 17 Mar 2018 22:53:38 +0100 Subject: [PATCH 2/3] Format with mix formatter --- lib/phoenix_html/form.ex | 18 ++++++++++-------- test/phoenix_html/form_test.exs | 3 +-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex index 12f13525..3bfe075a 100644 --- a/lib/phoenix_html/form.ex +++ b/lib/phoenix_html/form.ex @@ -1460,20 +1460,22 @@ defmodule Phoenix.HTML.Form do See `label/2`. """ def label(form, field, text, opts) when is_binary(text) and is_list(opts) do - {opts, input_id} = case Keyword.pop(opts, :value) do - {nil, opts} -> {opts, input_id(form, field)} - {value, opts} -> {opts, input_id(form, field, value)} - end + {opts, input_id} = + case Keyword.pop(opts, :value) do + {nil, opts} -> {opts, input_id(form, field)} + {value, opts} -> {opts, input_id(form, field, value)} + end opts = Keyword.put_new(opts, :for, input_id) content_tag(:label, text, opts) end def label(form, field, opts, do: block) do - {opts, input_id} = case Keyword.pop(opts, :value) do - {nil, opts} -> {opts, input_id(form, field)} - {value, opts} -> {opts, input_id(form, field, value)} - end + {opts, input_id} = + case Keyword.pop(opts, :value) do + {nil, opts} -> {opts, input_id(form, field)} + {value, opts} -> {opts, input_id(form, field, value)} + end opts = Keyword.put_new(opts, :for, input_id) content_tag(:label, opts, do: block) diff --git a/test/phoenix_html/form_test.exs b/test/phoenix_html/form_test.exs index bc6221b8..dc76e634 100644 --- a/test/phoenix_html/form_test.exs +++ b/test/phoenix_html/form_test.exs @@ -1174,8 +1174,7 @@ defmodule Phoenix.HTML.FormTest do end test "label/4 with for_value option" do - assert safe_form(&label(&1, :key, value: "1")) == - ~s() + assert safe_form(&label(&1, :key, value: "1")) == ~s() end test "label/3 with a block" do From b62ba5e8c4041572652c78e6e0e3d192be5bfa88 Mon Sep 17 00:00:00 2001 From: Johan Tell Date: Sat, 17 Mar 2018 22:59:26 +0100 Subject: [PATCH 3/3] Extract code for making the label options to its own function --- lib/phoenix_html/form.ex | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex index 3bfe075a..574fb819 100644 --- a/lib/phoenix_html/form.ex +++ b/lib/phoenix_html/form.ex @@ -1460,25 +1460,23 @@ defmodule Phoenix.HTML.Form do See `label/2`. """ def label(form, field, text, opts) when is_binary(text) and is_list(opts) do - {opts, input_id} = - case Keyword.pop(opts, :value) do - {nil, opts} -> {opts, input_id(form, field)} - {value, opts} -> {opts, input_id(form, field, value)} - end - - opts = Keyword.put_new(opts, :for, input_id) + opts = label_options(form, field, opts) content_tag(:label, text, opts) end def label(form, field, opts, do: block) do + opts = label_options(form, field, opts) + content_tag(:label, opts, do: block) + end + + defp label_options(form, field, opts) do {opts, input_id} = case Keyword.pop(opts, :value) do {nil, opts} -> {opts, input_id(form, field)} {value, opts} -> {opts, input_id(form, field, value)} end - opts = Keyword.put_new(opts, :for, input_id) - content_tag(:label, opts, do: block) + Keyword.put_new(opts, :for, input_id) end # Normalize field name to string version