Add wrappe for Phoenix.Form.submit

This commit is contained in:
Mitchell Hanberg 2019-07-01 00:25:33 -04:00
parent a372a64858
commit dd8edc025d
2 changed files with 102 additions and 0 deletions

View file

@ -100,6 +100,54 @@ defmodule Dsl.Form do
end
end
@doc """
Please see `Phoenix.HTML.Form.submit/1` for details.
"""
defmacro submit(do: block) do
quote do
{:safe, content} =
htm do
unquote(block)
end
{:safe, input} = Phoenix.HTML.Form.submit(do: content)
Utils.put_buffer(var!(buff, Dsl.Tags), input)
end
end
defmacro submit(value) do
quote do
{:safe, input} = Phoenix.HTML.Form.submit(unquote(value))
Utils.put_buffer(var!(buff, Dsl.Tags), input)
end
end
@doc """
Please see `Phoenix.HTML.Form.submit/1` for details.
"""
defmacro submit(opts, do: block) do
quote do
{:safe, content} =
htm do
unquote(block)
end
{:safe, input} = Phoenix.HTML.Form.submit(unquote(opts), do: content)
Utils.put_buffer(var!(buff, Dsl.Tags), input)
end
end
defmacro submit(value, opts) do
quote do
{:safe, input} = Phoenix.HTML.Form.submit(unquote_splicing([value, opts]))
Utils.put_buffer(var!(buff, Dsl.Tags), input)
end
end
@doc """
Please see `Phoenix.HTML.Form.label/2` for details.
"""

View file

@ -463,6 +463,60 @@ defmodule Dsl.FormTest do
assert result =~ ~s{class="reset-styles"}
end
test "generates a submit/1 input" do
{:safe, result} =
htm do
submit("Submit")
end
assert String.starts_with? result, ~s{<button}
assert result =~ ~s{type="submit}
assert result =~ ~s{Submit}
assert String.ends_with? result, ~s{</button>}
end
test "generates a submit/1 input that takes a block" do
{:safe, result} =
htm do
submit do
text "Submit"
end
end
assert String.starts_with? result, ~s{<button}
assert result =~ ~s{type="submit}
assert result =~ ~s{Submit}
assert String.ends_with? result, ~s{</button>}
end
test "generates a submit/2 input that takes text and opts" do
{:safe, result} =
htm do
submit("Submit", class: "btn")
end
assert String.starts_with? result, ~s{<button}
assert result =~ ~s{type="submit}
assert result =~ ~s{class="btn"}
assert result =~ ~s{Submit}
assert String.ends_with? result, ~s{</button>}
end
test "generates a submit/2 input that takes opts and a block" do
{:safe, result} =
htm do
submit class: "btn" do
text "Submit"
end
end
assert String.starts_with? result, ~s{<button}
assert result =~ ~s{type="submit}
assert result =~ ~s{class="btn"}
assert result =~ ~s{Submit}
assert String.ends_with? result, ~s{</button>}
end
test "generates a phx_label/2 tag" do
{:safe, result} =
htm do