mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Accept options in label component too
This commit is contained in:
parent
df47a8599d
commit
51bdd20246
3 changed files with 35 additions and 3 deletions
|
@ -22,10 +22,16 @@ module SimpleForm
|
|||
end
|
||||
|
||||
def label(context=nil)
|
||||
if generate_label_for_attribute?
|
||||
@builder.label(label_target, label_text, label_html_options)
|
||||
if context
|
||||
label_options = merged_label_options(context.options)
|
||||
else
|
||||
template.label_tag(nil, label_text, label_html_options)
|
||||
label_options = label_html_options
|
||||
end
|
||||
|
||||
if generate_label_for_attribute?
|
||||
@builder.label(label_target, label_text, label_options)
|
||||
else
|
||||
template.label_tag(nil, label_text, label_options)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -46,6 +52,7 @@ module SimpleForm
|
|||
if options.key?(:input_html) && options[:input_html].key?(:id)
|
||||
label_options[:for] = options[:input_html][:id]
|
||||
end
|
||||
|
||||
label_options
|
||||
end
|
||||
|
||||
|
@ -74,6 +81,14 @@ module SimpleForm
|
|||
def generate_label_for_attribute?
|
||||
true
|
||||
end
|
||||
|
||||
def merged_label_options(context_options)
|
||||
label_html_options.merge(context_options) do |_, oldval, newval|
|
||||
if Array === oldval
|
||||
oldval + Array(newval)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -221,4 +221,14 @@ class WrapperTest < ActionView::TestCase
|
|||
|
||||
assert_select "div.custom_wrapper input.string.inline-class"
|
||||
end
|
||||
|
||||
test 'label accepts attributes in the DSL' do
|
||||
swap_wrapper :default, self.custom_wrapper_with_label_class do
|
||||
with_concat_form_for @user do |f|
|
||||
concat f.input :name
|
||||
end
|
||||
end
|
||||
|
||||
assert_select "div.custom_wrapper label.string.inline-class"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -75,6 +75,13 @@ module MiscHelpers
|
|||
end
|
||||
end
|
||||
|
||||
def custom_wrapper_with_label_class
|
||||
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
|
||||
b.use :label, class: 'inline-class'
|
||||
b.use :input
|
||||
end
|
||||
end
|
||||
|
||||
def custom_wrapper_with_wrapped_input
|
||||
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
|
||||
b.wrapper tag: :div, class: 'elem' do |component|
|
||||
|
|
Loading…
Reference in a new issue