Skip to content

Commit

Permalink
Adds the boolean attributes to checkbox and textarea helper methods. F…
Browse files Browse the repository at this point in the history
…ixes #952
  • Loading branch information
jwoertink committed Oct 23, 2019
1 parent 6ae6ab1 commit d6d0416
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
10 changes: 10 additions & 0 deletions spec/lucky/input_helpers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ describe Lucky::InputHelpers do
view.checkbox(false_field).to_s.should contain <<-HTML
<input type="hidden" id="" name="user:admin" value="false">
HTML
view.checkbox(false_field, attrs: [:checked]).to_s.should contain <<-HTML
<input type="checkbox" id="user_admin" name="user:admin" value="true" checked>
HTML

true_field = form.admin(true)
view.checkbox(true_field).to_s.should contain <<-HTML
Expand All @@ -99,6 +102,9 @@ describe Lucky::InputHelpers do
view.checkbox(true_field).to_s.should contain <<-HTML
<input type="hidden" id="" name="user:admin" value="false">
HTML
view.checkbox(true_field, attrs: [:required]).to_s.should contain <<-HTML
<input type="checkbox" id="user_admin" name="user:admin" value="true" checked="true" required>
HTML
end
end

Expand Down Expand Up @@ -232,6 +238,10 @@ describe Lucky::InputHelpers do
view.textarea(form.first_name, rows: 5, cols: 15).to_s.should contain <<-HTML
<textarea id="user_first_name" name="user:first_name" rows="5" cols="15">My name</textarea>
HTML

view.textarea(form.first_name, attrs: [:required]).to_s.should contain <<-HTML
<textarea id="user_first_name" name="user:first_name" required>My name</textarea>
HTML
end

it "renders time inputs" do
Expand Down
36 changes: 33 additions & 3 deletions src/lucky/tags/input_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,35 @@ module Lucky::InputHelpers
end

generate_helpful_error_for textarea


# Returns a textarea field.
#
# ```
# textarea(attribute)
# # => <textarea id="param_key_attribute_name" name="param_key:attribute_name"></textarea>
# ```
def textarea(field : Avram::PermittedAttribute, **html_options)
textarea field.param.to_s, merge_options(html_options, {
"id" => input_id(field),
"name" => input_name(field),
})
end

def checkbox(field : Avram::PermittedAttribute(T),
# Similar to textarea; this allows for Boolean attributes
# through `attrs`.
#
# ```
# textarea(attribute, attrs: [:required])
# # => <textarea id="param_key_attribute_name" name="param_key:attribute_name" required></textarea>
# ```
def textarea(field : Avram::PermittedAttribute, attrs : Array(Symbol), **html_options)
textarea field.param.to_s, merge_options(html_options, {
"id" => input_id(field),
"name" => input_name(field),
}), attrs: attrs
end

def checkbox(field : Avram::PermittedAttribute(T),
unchecked_value : String,
checked_value : String,
**html_options) forall T
Expand All @@ -59,7 +79,17 @@ module Lucky::InputHelpers
generate_input(field, "checkbox", html_options)
end

generate_helpful_error_for checkbox
def checkbox(field : Avram::PermittedAttribute(Bool?), attrs : Array(Symbol), **html_options)
unchecked_value = "false"
if field.value
html_options = merge_options(html_options, {"checked" => "true"})
end
html_options = merge_options(html_options, {"value" => "true"})
generate_input(field, "hidden", {"id" => ""}, {"value" => unchecked_value})
generate_input(field, "checkbox", html_options, attrs: attrs)
end

generate_helpful_error_for checkbox

{% for input_type in ["text", "email", "file", "color", "hidden", "number", "url", "search", "range"] %}
generate_helpful_error_for {{input_type.id}}_input
Expand Down

0 comments on commit d6d0416

Please sign in to comment.