mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Merge pull request #455 from krzyzak/master
Add ability not to add classes to div/label/input
This commit is contained in:
commit
2dafe48ada
7 changed files with 46 additions and 4 deletions
|
@ -138,6 +138,9 @@ SimpleForm.setup do |config|
|
|||
# You can define the class to use on all forms. Default is simple_form.
|
||||
# config.form_class = :simple_form
|
||||
|
||||
# You can define which elements should obtain additional classes
|
||||
# config.generate_additional_classes_for = [:wrapper, :label, :input]
|
||||
|
||||
# Whether attributes are required by default (or not). Default is true.
|
||||
# config.required_by_default = true
|
||||
|
||||
|
|
|
@ -73,6 +73,10 @@ module SimpleForm
|
|||
mattr_accessor :form_class
|
||||
@@form_class = :simple_form
|
||||
|
||||
# You can define which elements should obtain additional classes
|
||||
mattr_accessor :generate_additional_classes_for
|
||||
@@generate_additional_classes_for = [:wrapper, :label, :input]
|
||||
|
||||
# Whether attributes are required by default (or not).
|
||||
mattr_accessor :required_by_default
|
||||
@@required_by_default = true
|
||||
|
|
|
@ -38,7 +38,9 @@ module SimpleForm
|
|||
end
|
||||
|
||||
def label_html_options
|
||||
label_options = html_options_for(:label, [input_type, required_class, SimpleForm.label_class].compact)
|
||||
label_html_classes = SimpleForm.generate_additional_classes_for.include?(:label) ? [input_type, required_class, SimpleForm.label_class].compact : []
|
||||
|
||||
label_options = html_options_for(:label, label_html_classes)
|
||||
label_options[:for] = options[:input_html][:id] if options.key?(:input_html) && options[:input_html].key?(:id)
|
||||
label_options
|
||||
end
|
||||
|
|
|
@ -59,7 +59,8 @@ module SimpleForm
|
|||
# Notice that html_options_for receives a reference to input_html_classes.
|
||||
# This means that classes added dynamically to input_html_classes will
|
||||
# still propagate to input_html_options.
|
||||
@html_classes = [input_type, required_class, readonly_class, disabled_class].compact
|
||||
@html_classes = SimpleForm.generate_additional_classes_for.include?(:input) ? [input_type, required_class, readonly_class, disabled_class].compact : []
|
||||
|
||||
@input_html_classes = @html_classes.dup
|
||||
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
|
||||
o[:readonly] = true if has_readonly?
|
||||
|
@ -100,7 +101,7 @@ module SimpleForm
|
|||
html_options = options[:"#{namespace}_html"] || {}
|
||||
css_classes << html_options[:class] if html_options.key?(:class)
|
||||
html_options[:class] = css_classes
|
||||
html_options
|
||||
html_options.delete_if{|key, value| value.blank?}
|
||||
end
|
||||
|
||||
# Lookup translations for the given namespace using I18n, based on object name,
|
||||
|
|
|
@ -24,7 +24,7 @@ module SimpleForm
|
|||
|
||||
def html_classes(input, options)
|
||||
css = options[:wrapper_class] ? Array.wrap(options[:wrapper_class]) : @defaults[:class]
|
||||
css += input.html_classes
|
||||
css += input.html_classes if SimpleForm.generate_additional_classes_for.include?(:wrapper)
|
||||
css << (options[:wrapper_error_class] || @defaults[:error_class]) if input.has_errors?
|
||||
css << (options[:wrapper_hint_class] || @defaults[:hint_class]) if input.has_hint?
|
||||
css
|
||||
|
|
|
@ -166,6 +166,21 @@ class IsolatedLabelTest < ActionView::TestCase
|
|||
assert_select 'label.datetime'
|
||||
end
|
||||
|
||||
test 'label should not have css class from type when generate_additional_classes_for does not include :label' do
|
||||
swap SimpleForm, :generate_additional_classes_for => [:wrapper, :input] do
|
||||
with_label_for @user, :name, :string
|
||||
assert_no_select 'label.string'
|
||||
with_label_for @user, :description, :text
|
||||
assert_no_select 'label.text'
|
||||
with_label_for @user, :age, :integer
|
||||
assert_no_select 'label.integer'
|
||||
with_label_for @user, :born_at, :date
|
||||
assert_no_select 'label.date'
|
||||
with_label_for @user, :created_at, :datetime
|
||||
assert_no_select 'label.datetime'
|
||||
end
|
||||
end
|
||||
|
||||
test 'label should obtain required from ActiveModel::Validations when it is included' do
|
||||
with_label_for @validating_user, :name, :string
|
||||
assert_select 'label.required'
|
||||
|
@ -173,6 +188,15 @@ class IsolatedLabelTest < ActionView::TestCase
|
|||
assert_select 'label.optional'
|
||||
end
|
||||
|
||||
test 'label should not obtain required from ActiveModel::Validations when generate_additional_classes_for does not include :label' do
|
||||
swap SimpleForm, :generate_additional_classes_for => [:wrapper, :input] do
|
||||
with_label_for @validating_user, :name, :string
|
||||
assert_no_select 'label.required'
|
||||
with_label_for @validating_user, :status, :string
|
||||
assert_no_select 'label.optional'
|
||||
end
|
||||
end
|
||||
|
||||
test 'label should allow overriding required when ActiveModel::Validations is included' do
|
||||
with_label_for @validating_user, :name, :string, :required => false
|
||||
assert_select 'label.optional'
|
||||
|
|
|
@ -38,6 +38,14 @@ class FormBuilderTest < ActionView::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
test 'builder should allow to skip input_type class' do
|
||||
swap SimpleForm, :generate_additional_classes_for => [:label, :wrapper] do
|
||||
with_form_for @user, :post_count
|
||||
assert_no_select "form input#user_post_count.integer"
|
||||
assert_select "form input#user_post_count"
|
||||
end
|
||||
end
|
||||
|
||||
test 'builder should allow adding custom input mappings for integer input types' do
|
||||
swap SimpleForm, :input_mappings => { /lock_version/ => :hidden } do
|
||||
with_form_for @user, :lock_version
|
||||
|
|
Loading…
Reference in a new issue