@@ -7,10 +7,12 @@ module InputsCollection
77
88 private
99
10- def inputs_collection ( name , collection , value , text , options = { } )
10+ def inputs_collection ( name , collection , value , text , options = { } , & )
1111 options [ :label ] ||= { class : group_label_class ( field_layout ( options ) ) }
1212 options [ :inline ] ||= layout_inline? ( options [ :layout ] )
1313
14+ return group_inputs_collection ( name , collection , value , text , options , &) if BootstrapForm . config . group_around_collections
15+
1416 form_group_builder ( name , options ) do
1517 inputs = ActiveSupport ::SafeBuffer . new
1618
@@ -56,6 +58,63 @@ def form_group_collection_input_checked?(checked, obj, input_value)
5658 checked == input_value || Array ( checked ) . try ( :include? , input_value ) ||
5759 checked == obj || Array ( checked ) . try ( :include? , obj )
5860 end
61+
62+ def group_inputs_collection ( name , collection , value , text , options = { } , &)
63+ group_builder ( name , options ) do
64+ render_collection ( name , collection , value , text , options , &)
65+ end
66+ end
67+
68+ def render_collection ( name , collection , value , text , options = { } , &)
69+ inputs = ActiveSupport ::SafeBuffer . new
70+
71+ collection . each_with_index do |obj , i |
72+ input_value = value . respond_to? ( :call ) ? value . call ( obj ) : obj . send ( value )
73+ input_options = form_group_collection_input_options ( options , text , obj , i , input_value , collection )
74+ inputs << yield ( name , input_value , input_options )
75+ end
76+
77+ inputs
78+ end
79+
80+ def group_builder ( method , options , html_options = nil , &)
81+ form_group_builder_wrapper ( method , options , html_options ) do |form_group_options , no_wrapper |
82+ if no_wrapper
83+ yield
84+ else
85+ field_group ( method , form_group_options , &)
86+ end
87+ end
88+ end
89+
90+ def field_group ( name , options , &)
91+ options [ :class ] = form_group_classes ( options )
92+
93+ tag . div (
94+ **options . except (
95+ :add_control_col_class , :append , :control_col , :floating , :help , :icon , :id ,
96+ :input_group_class , :label , :label_col , :layout , :prepend
97+ ) ,
98+ aria : { labelledby : options [ :id ] || default_id ( name ) } ,
99+ role : :group
100+ ) do
101+ group_label_div = generate_group_label_div ( name , options )
102+ prepare_label_options ( options [ :id ] , name , options [ :label ] , options [ :label_col ] , options [ :layout ] )
103+ form_group_content ( group_label_div , generate_help ( name , options [ :help ] ) , options , &)
104+ end
105+ end
106+
107+ def generate_group_label_div ( name , options )
108+ group_label_div_class = options . dig ( :label , :class ) || "form-label"
109+ id = options [ :id ] || default_id ( name )
110+
111+ tag . div (
112+ **{ class : group_label_div_class } . compact ,
113+ id :
114+ ) { label_text ( name , options . dig ( :label , :text ) ) }
115+ end
116+
117+ def default_id ( name ) = raw ( "#{ object_name } _#{ name } " ) # rubocop:disable Rails/OutputSafety
59118 end
60119 end
61120end
0 commit comments