Split collection radio/check boxes in different components
This commit is contained in:
parent
ec10a9669e
commit
85612cb9e9
|
@ -19,7 +19,9 @@ module SimpleForm
|
|||
map_type :password, :to => SimpleForm::Inputs::PasswordInput
|
||||
map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
|
||||
map_type :range, :to => SimpleForm::Inputs::RangeInput
|
||||
map_type :select, :radio, :check_boxes, :to => SimpleForm::Inputs::CollectionInput
|
||||
map_type :check_boxes, :to => SimpleForm::Inputs::CollectionCheckBoxesInput
|
||||
map_type :radio, :to => SimpleForm::Inputs::CollectionRadioInput
|
||||
map_type :select, :to => SimpleForm::Inputs::CollectionInput
|
||||
map_type :grouped_select, :to => SimpleForm::Inputs::GroupedCollectionInput
|
||||
map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput
|
||||
map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput
|
||||
|
|
|
@ -4,6 +4,8 @@ module SimpleForm
|
|||
autoload :BlockInput, 'simple_form/inputs/block_input'
|
||||
autoload :BooleanInput, 'simple_form/inputs/boolean_input'
|
||||
autoload :CollectionInput, 'simple_form/inputs/collection_input'
|
||||
autoload :CollectionRadioInput, 'simple_form/inputs/collection_radio_input'
|
||||
autoload :CollectionCheckBoxesInput, 'simple_form/inputs/collection_check_boxes_input'
|
||||
autoload :GroupedCollectionInput, 'simple_form/inputs/grouped_collection_input'
|
||||
autoload :DateTimeInput, 'simple_form/inputs/date_time_input'
|
||||
autoload :FileInput, 'simple_form/inputs/file_input'
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
module SimpleForm
|
||||
module Inputs
|
||||
class CollectionCheckBoxesInput < CollectionInput
|
||||
def input
|
||||
label_method, value_method = detect_collection_methods
|
||||
|
||||
@builder.collection_check_boxes(
|
||||
attribute_name, collection, value_method, label_method,
|
||||
input_options, input_html_options, &collection_block_for_nested_boolean_style
|
||||
)
|
||||
end
|
||||
|
||||
def input_options
|
||||
options = super
|
||||
apply_default_collection_options!(options)
|
||||
options
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Checkbox components does not use the required html tag.
|
||||
# See more info here - https://github.com/plataformatec/simple_form/issues/340#issuecomment-2871956
|
||||
def has_required?
|
||||
false
|
||||
end
|
||||
|
||||
def collection_block_for_nested_boolean_style
|
||||
return unless nested_boolean_style?
|
||||
|
||||
proc do |label_for, text, value, html_options|
|
||||
@builder.label(label_for, text) { @builder.check_box(attribute_name, html_options, value) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -15,14 +15,12 @@ module SimpleForm
|
|||
def input
|
||||
label_method, value_method = detect_collection_methods
|
||||
@builder.send(:"collection_#{input_type}", attribute_name, collection,
|
||||
value_method, label_method, input_options, input_html_options,
|
||||
&collection_block_for_nested_boolean_style)
|
||||
value_method, label_method, input_options, input_html_options)
|
||||
end
|
||||
|
||||
def input_options
|
||||
options = super
|
||||
options[:include_blank] = true unless skip_include_blank?
|
||||
apply_default_collection_options!(options) if check_boxes? || radio?
|
||||
options
|
||||
end
|
||||
|
||||
|
@ -35,10 +33,8 @@ module SimpleForm
|
|||
end
|
||||
end
|
||||
|
||||
# Checkbox components does not use the required html tag.
|
||||
# See more info here - https://github.com/plataformatec/simple_form/issues/340#issuecomment-2871956
|
||||
def has_required?
|
||||
super && (input_options[:include_blank] || multiple?) && !check_boxes?
|
||||
super && (input_options[:include_blank] || multiple?)
|
||||
end
|
||||
|
||||
# Check if :include_blank must be included by default.
|
||||
|
@ -50,14 +46,6 @@ module SimpleForm
|
|||
!!options[:input_html].try(:[], :multiple)
|
||||
end
|
||||
|
||||
def check_boxes?
|
||||
input_type == :check_boxes
|
||||
end
|
||||
|
||||
def radio?
|
||||
input_type == :radio
|
||||
end
|
||||
|
||||
def apply_default_collection_options!(options)
|
||||
unless options.key?(:item_wrapper_tag)
|
||||
options[:item_wrapper_tag] = SimpleForm.item_wrapper_tag
|
||||
|
@ -70,20 +58,6 @@ module SimpleForm
|
|||
].compact.presence
|
||||
end
|
||||
|
||||
def collection_block_for_nested_boolean_style
|
||||
return unless nested_boolean_style?
|
||||
|
||||
if check_boxes?
|
||||
proc do |label_for, text, value, html_options|
|
||||
@builder.label(label_for, text) { @builder.check_box(attribute_name, html_options, value) }
|
||||
end
|
||||
elsif radio?
|
||||
proc do |label_for, text, value, html_options|
|
||||
@builder.label(label_for, text) { @builder.radio_button(attribute_name, value, html_options) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Detect the right method to find the label and value for a collection.
|
||||
# If no label or value method are defined, will attempt to find them based
|
||||
# on default label and value methods that can be configured through
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
module SimpleForm
|
||||
module Inputs
|
||||
class CollectionRadioInput < CollectionInput
|
||||
def input
|
||||
label_method, value_method = detect_collection_methods
|
||||
|
||||
@builder.collection_radio(
|
||||
attribute_name, collection, value_method, label_method,
|
||||
input_options, input_html_options, &collection_block_for_nested_boolean_style
|
||||
)
|
||||
end
|
||||
|
||||
def input_options
|
||||
options = super
|
||||
apply_default_collection_options!(options)
|
||||
options
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def collection_block_for_nested_boolean_style
|
||||
return unless nested_boolean_style?
|
||||
|
||||
proc do |label_for, text, value, html_options|
|
||||
@builder.label(label_for, text) { @builder.radio_button(attribute_name, value, html_options) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue