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 :password, :to => SimpleForm::Inputs::PasswordInput
|
||||||
map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
|
map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
|
||||||
map_type :range, :to => SimpleForm::Inputs::RangeInput
|
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 :grouped_select, :to => SimpleForm::Inputs::GroupedCollectionInput
|
||||||
map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput
|
map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput
|
||||||
map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput
|
map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput
|
||||||
|
|
|
@ -1,18 +1,20 @@
|
||||||
module SimpleForm
|
module SimpleForm
|
||||||
module Inputs
|
module Inputs
|
||||||
autoload :Base, 'simple_form/inputs/base'
|
autoload :Base, 'simple_form/inputs/base'
|
||||||
autoload :BlockInput, 'simple_form/inputs/block_input'
|
autoload :BlockInput, 'simple_form/inputs/block_input'
|
||||||
autoload :BooleanInput, 'simple_form/inputs/boolean_input'
|
autoload :BooleanInput, 'simple_form/inputs/boolean_input'
|
||||||
autoload :CollectionInput, 'simple_form/inputs/collection_input'
|
autoload :CollectionInput, 'simple_form/inputs/collection_input'
|
||||||
autoload :GroupedCollectionInput, 'simple_form/inputs/grouped_collection_input'
|
autoload :CollectionRadioInput, 'simple_form/inputs/collection_radio_input'
|
||||||
autoload :DateTimeInput, 'simple_form/inputs/date_time_input'
|
autoload :CollectionCheckBoxesInput, 'simple_form/inputs/collection_check_boxes_input'
|
||||||
autoload :FileInput, 'simple_form/inputs/file_input'
|
autoload :GroupedCollectionInput, 'simple_form/inputs/grouped_collection_input'
|
||||||
autoload :HiddenInput, 'simple_form/inputs/hidden_input'
|
autoload :DateTimeInput, 'simple_form/inputs/date_time_input'
|
||||||
autoload :NumericInput, 'simple_form/inputs/numeric_input'
|
autoload :FileInput, 'simple_form/inputs/file_input'
|
||||||
autoload :PasswordInput, 'simple_form/inputs/password_input'
|
autoload :HiddenInput, 'simple_form/inputs/hidden_input'
|
||||||
autoload :PriorityInput, 'simple_form/inputs/priority_input'
|
autoload :NumericInput, 'simple_form/inputs/numeric_input'
|
||||||
autoload :RangeInput, 'simple_form/inputs/range_input'
|
autoload :PasswordInput, 'simple_form/inputs/password_input'
|
||||||
autoload :StringInput, 'simple_form/inputs/string_input'
|
autoload :PriorityInput, 'simple_form/inputs/priority_input'
|
||||||
autoload :TextInput, 'simple_form/inputs/text_input'
|
autoload :RangeInput, 'simple_form/inputs/range_input'
|
||||||
|
autoload :StringInput, 'simple_form/inputs/string_input'
|
||||||
|
autoload :TextInput, 'simple_form/inputs/text_input'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
def input
|
||||||
label_method, value_method = detect_collection_methods
|
label_method, value_method = detect_collection_methods
|
||||||
@builder.send(:"collection_#{input_type}", attribute_name, collection,
|
@builder.send(:"collection_#{input_type}", attribute_name, collection,
|
||||||
value_method, label_method, input_options, input_html_options,
|
value_method, label_method, input_options, input_html_options)
|
||||||
&collection_block_for_nested_boolean_style)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def input_options
|
def input_options
|
||||||
options = super
|
options = super
|
||||||
options[:include_blank] = true unless skip_include_blank?
|
options[:include_blank] = true unless skip_include_blank?
|
||||||
apply_default_collection_options!(options) if check_boxes? || radio?
|
|
||||||
options
|
options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -35,10 +33,8 @@ module SimpleForm
|
||||||
end
|
end
|
||||||
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?
|
def has_required?
|
||||||
super && (input_options[:include_blank] || multiple?) && !check_boxes?
|
super && (input_options[:include_blank] || multiple?)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Check if :include_blank must be included by default.
|
# Check if :include_blank must be included by default.
|
||||||
|
@ -50,14 +46,6 @@ module SimpleForm
|
||||||
!!options[:input_html].try(:[], :multiple)
|
!!options[:input_html].try(:[], :multiple)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_boxes?
|
|
||||||
input_type == :check_boxes
|
|
||||||
end
|
|
||||||
|
|
||||||
def radio?
|
|
||||||
input_type == :radio
|
|
||||||
end
|
|
||||||
|
|
||||||
def apply_default_collection_options!(options)
|
def apply_default_collection_options!(options)
|
||||||
unless options.key?(:item_wrapper_tag)
|
unless options.key?(:item_wrapper_tag)
|
||||||
options[:item_wrapper_tag] = SimpleForm.item_wrapper_tag
|
options[:item_wrapper_tag] = SimpleForm.item_wrapper_tag
|
||||||
|
@ -70,20 +58,6 @@ module SimpleForm
|
||||||
].compact.presence
|
].compact.presence
|
||||||
end
|
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.
|
# 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
|
# 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
|
# 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