amberframework / jasper-helpers

A library of helpers for working with html for apps Crystal
https://amberframework.org
MIT License
19 stars 7 forks source link

form label with block shouldn't insert name automatically #27

Open robacarp opened 6 years ago

robacarp commented 6 years ago

With this code:

== form action: "/", method: :post do
  == csrf_tag
  == label name: "alpha" do
    | beta
    == check_box id="beta"

I get the following html output:

<form action="/" method="post">
  <input type="hidden" name="_csrf" value="..." />
  <label for="alpha" id="alpha_label">
    beta
    <input type="checkbox" name="beta" id="beta" value="1"/>
    <input type="hidden" name="beta" id="beta_default" value="0"/>
+    Alpha
  </label>
</form>

When giving a block to a label, I expect the block to be the contents of the label and nothing more.

currently jasper automatically appends the name

14tinchov commented 6 years ago

Hi guys ! I have two ideas for this issue

Option 1

In Rails when you pass a block to a label, the name is not appended, maybe we could remove this

  def label(name : String | Symbol)
    content = "#{yield}"
    label(name, content: content, for: name, id: "#{Kit.css_safe(name)}_label")
  end

So...now we need a method to display our block, maybe we could do it somethings like this

  def wrapper_field(*args)
    return "" unless args.first?
    args.join("")
  end

in our views:

      label(:name) do
        wrapper_field(
          check_box(:allowed),
          "name"
        )

Benefit: in this way you can concatenate many elements in a single method

I created a PR for it

Option 2

We could use a boolean to show the name label but this will only remove the "appends"