Skip to content

Commit

Permalink
Auto-display labels whenever possible
Browse files Browse the repository at this point in the history
Translation is yet to be done...
  • Loading branch information
yuki24 committed Jul 6, 2022
1 parent 1f38be7 commit c1e3a95
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 4 additions & 4 deletions app/helpers/shoelace/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def color_field(method, **options)
alias color_picker color_field

def range_field(method, **options)
ShoelaceRange.new(object_name, method, @template, options.with_defaults(object: @object)).render
ShoelaceRange.new(object_name, method, @template, options.with_defaults(object: @object, label: method.to_s.humanize)).render
end
alias range range_field

Expand All @@ -211,19 +211,19 @@ def switch_field(method, **options, &block)
alias switch switch_field

def text_area(method, **options, &block)
ShoelaceTextArea.new(object_name, method, @template, options.with_defaults(object: @object, resize: 'auto')).render(&block)
ShoelaceTextArea.new(object_name, method, @template, options.with_defaults(object: @object, label: method.to_s.humanize, resize: 'auto')).render(&block)
end

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0", &block)
ShoelaceCheckBox.new(object_name, method, @template, checked_value, unchecked_value, options.merge(object: @object)).render(&block)
end

def select(method, choices = nil, options = {}, html_options = {}, &block)
ShoelaceSelect.new(object_name, method, @template, choices, options.with_defaults(object: @object), html_options, &block).render
ShoelaceSelect.new(object_name, method, @template, choices, options.with_defaults(object: @object, label: method.to_s.humanize), html_options, &block).render
end

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
ShoelaceCollectionSelect.new(object_name, method, @template, collection, value_method, text_method, options.with_defaults(object: @object), html_options, &block).render
ShoelaceCollectionSelect.new(object_name, method, @template, collection, value_method, text_method, options.with_defaults(object: @object, label: method.to_s.humanize), html_options, &block).render
end

def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Expand Down
18 changes: 17 additions & 1 deletion test/helpers/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ class FormHelperTest < ActionView::TestCase
test "#range_field" do
sl_form_for(User.new, url: "/") do |form|
assert_dom_equal <<~HTML, form.range_field(:name)
<sl-range label="Name" name="user[name]" id="user_name"></sl-range>
HTML
end
end

test "#range_field without a label" do
sl_form_for(User.new, url: "/") do |form|
assert_dom_equal <<~HTML, form.range_field(:name, label: nil)
<sl-range name="user[name]" id="user_name"></sl-range>
HTML
end
Expand All @@ -179,6 +187,14 @@ class FormHelperTest < ActionView::TestCase
test "#text_area" do
sl_form_for(User.new, url: "/") do |form|
assert_dom_equal <<~HTML, form.text_area(:name)
<sl-textarea label="Name" resize="auto" name="user[name]" id="user_name"></sl-textarea>
HTML
end
end

test "#text_area without a label" do
sl_form_for(User.new, url: "/") do |form|
assert_dom_equal <<~HTML, form.text_area(:name, label: nil)
<sl-textarea resize="auto" name="user[name]" id="user_name"></sl-textarea>
HTML
end
Expand All @@ -187,7 +203,7 @@ class FormHelperTest < ActionView::TestCase
test "#text_area with a block" do
sl_form_for(User.new, url: "/") do |form|
expected = <<~HTML
<sl-textarea resize="auto" name="user[name]" id="user_name">
<sl-textarea label="Name" resize="auto" name="user[name]" id="user_name">
<div slot="help-text">Name can not be blank.</div>
</sl-textarea>
HTML
Expand Down

0 comments on commit c1e3a95

Please sign in to comment.