crown_marketplace_utils 0.1.0.beta.2 → 0.1.0.beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CrownMarketplaceUtils
6
+ module GovUkHelper
7
+ module Field
8
+ # = GOV.UK Checkboxes
9
+ #
10
+ # This helper is used for generating the checkboxes component from the
11
+ # {https://design-system.service.gov.uk/components/checkboxes GDS - Components - Checkboxes}
12
+ #
13
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
14
+
15
+ module Checkboxes
16
+ include Field
17
+
18
+ # Generates the HTML for the GOV.UK Checkboxes component
19
+ #
20
+ # @param attribute [String, Symbol] the attribute of the raido buttons
21
+ # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
22
+ # @param error_message [String] the error message to be displayed
23
+ # @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
24
+ #
25
+ # @option govuk_checkboxes_options [Hash] :form_group_options see {govuk_fields}
26
+ # @option govuk_checkboxes_options [Hash] :fieldset_options see {govuk_fields}
27
+ # @option govuk_checkboxes_options [Hash] :hint see {govuk_field}
28
+ # @option govuk_checkboxes_options [Hash] :checkboxes_options ({}) the options that will be used when rendering the checkbox buttons.
29
+ # See {govuk_checkboxes_fields} for more details.
30
+ #
31
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Checkboxes
32
+ # which can then be rendered on the page
33
+
34
+ def govuk_checkboxes(attribute, items, error_message = nil, **govuk_checkboxes_options)
35
+ govuk_fields(:checkboxes, attribute, error_message, **govuk_checkboxes_options) do |govuk_field_options|
36
+ concat(govuk_checkboxes_fields(attribute, items, **govuk_field_options))
37
+ end
38
+ end
39
+
40
+ # Generates the HTML for the GOV.UK Checkboxes component using an ActionView::Helpers::FormBuilder.
41
+ # Unlike {govuk_checkboxes}, the method will be able to automatically determine if the error message needs to be shown.
42
+ #
43
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
44
+ # @param attribute [String, Symbol] the attribute of the raido buttons
45
+ # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
46
+ # @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
47
+ #
48
+ # @option govuk_checkboxes_options [Hash] :form_group_options see {govuk_fields_with_form}
49
+ # @option govuk_checkboxes_options [Hash] :fieldset_options see {govuk_fields_with_form}
50
+ # @option govuk_checkboxes_options [Hash] :hint see {govuk_fields_with_form}
51
+ # @option govuk_checkboxes_options [Hash] :checkboxes_options ({}) the options that will be used when rendering the checkbox buttons.
52
+ # See {govuk_checkboxes_fields_with_form} for more details.
53
+ #
54
+ # @return (see govuk_checkboxes)
55
+
56
+ def govuk_checkboxes_with_form(form, attribute, items, **govuk_checkboxes_options)
57
+ govuk_fields_with_form(:checkboxes, form, attribute, **govuk_checkboxes_options) do |govuk_field_options|
58
+ concat(govuk_checkboxes_fields_with_form(form, attribute, items, **govuk_field_options))
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ # Generates the checkboxes HTML for {govuk_checkboxes}
65
+ #
66
+ # @param attribute [String, Symbol] the attribute of the raido buttons
67
+ # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
68
+ # @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
69
+ #
70
+ # @option (see _govuk_checkboxes_fields)
71
+ #
72
+ # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes}
73
+
74
+ def govuk_checkboxes_fields(attribute, items, **govuk_checkboxes_options)
75
+ _govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
76
+ govuk_checkbox_item(attribute, checkbox_item)
77
+ end
78
+ end
79
+
80
+ # Generates the checkboxes HTML for {govuk_checkboxes_with_form}
81
+ #
82
+ # @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the checkbox buttons
83
+ # @param attribute [String, Symbol] the attribute of the raido buttons
84
+ # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
85
+ # @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
86
+ #
87
+ # @option (see _govuk_checkboxes_fields)
88
+ #
89
+ # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_with_form}
90
+
91
+ def govuk_checkboxes_fields_with_form(form, attribute, items, **govuk_checkboxes_options)
92
+ _govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
93
+ govuk_checkbox_item_with_form(form, attribute, checkbox_item)
94
+ end
95
+ end
96
+
97
+ # Wrapper method used by {govuk_checkboxes_fields} and {govuk_checkboxes_fields_with_form} to generate the checkboxes HTML
98
+ #
99
+ # @param items [Array] array of checkbox items.
100
+ # Each item is a hash which can be:
101
+ # - +:divider+ text to separate checkbox items
102
+ # - checkbox item, see {_govuk_checkbox_item}
103
+ # @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
104
+ #
105
+ # @option govuk_checkboxes_options [String] :classes additional CSS classes for the checkboxes HTML
106
+ # @option govuk_checkboxes_options [Hash] :attributes ({ module: 'govuk-checkboxes' }) any additional attributes that will added as part of the HTML
107
+ #
108
+ # @yield the checkbox item HTML generated by {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
109
+ #
110
+ # @yieldparam checkbox_item [Hash] the current checkbox item to be rendered
111
+ #
112
+ # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
113
+
114
+ def _govuk_checkboxes_fields(items, **govuk_checkboxes_options)
115
+ govuk_checkboxes_classes = ['govuk-checkboxes']
116
+ govuk_checkboxes_classes << govuk_checkboxes_options[:classes]
117
+ ((govuk_checkboxes_options[:attributes] ||= {})[:data] ||= {}).merge!({ module: 'govuk-checkboxes' })
118
+
119
+ tag.div(class: govuk_checkboxes_classes, **govuk_checkboxes_options[:attributes]) do
120
+ capture do
121
+ items.each do |checkbox_item|
122
+ concat(
123
+ if checkbox_item[:divider]
124
+ tag.div(checkbox_item[:divider], class: 'govuk-checkboxes__divider')
125
+ else
126
+ tag.div(class: 'govuk-checkboxes__item') do
127
+ yield(checkbox_item)
128
+ end
129
+ end
130
+ )
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ # Generates the HTML for a checkbox button for {govuk_checkboxes_fields_with_form}
137
+ #
138
+ # @param (see _govuk_checkbox_item)
139
+ #
140
+ # @option (see _govuk_checkbox_item)
141
+ #
142
+ # @return (see _govuk_checkbox_item)
143
+
144
+ def govuk_checkbox_item(attribute, checkbox_item)
145
+ _govuk_checkbox_item(attribute, checkbox_item) do
146
+ checkbox_item[:attributes][:id] ||= "#{sanitize_to_id(attribute)}_#{sanitize_to_id(checkbox_item[:value])}"
147
+
148
+ concat(check_box_tag("#{attribute}[]", checkbox_item[:value], checkbox_item[:checked], class: 'govuk-checkboxes__input', **checkbox_item[:attributes]))
149
+ concat(govuk_label(checkbox_item[:attributes][:id], checkbox_item[:label][:text], **checkbox_item[:label][:options]))
150
+ end
151
+ end
152
+
153
+ # rubocop:disable Metrics/AbcSize
154
+
155
+ # Generates the HTML for a checkbox button for {govuk_checkboxes_fields}
156
+ #
157
+ # @param (see _govuk_checkbox_item)
158
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
159
+ #
160
+ # @option (see _govuk_checkbox_item)
161
+ #
162
+ # @return (see _govuk_checkbox_item)
163
+
164
+ def govuk_checkbox_item_with_form(form, attribute, checkbox_item)
165
+ _govuk_checkbox_item(attribute, checkbox_item) do
166
+ (checkbox_item[:label][:options][:attributes] ||= {})[:value] = checkbox_item[:value]
167
+ checkbox_item[:label][:options][:attributes][:for] = checkbox_item[:attributes][:id] if checkbox_item[:attributes][:id]
168
+
169
+ concat(form.check_box(attribute, checkbox_item[:attributes].merge({ class: 'govuk-checkboxes__input', multiple: true, include_hidden: false }), checkbox_item[:value]))
170
+ concat(govuk_label_with_form(form, attribute, checkbox_item[:label][:text], **checkbox_item[:label][:options]))
171
+ end
172
+ end
173
+
174
+ # Wrapper method used by {govuk_checkbox_item} and {govuk_checkbox_item_with_form} to generate the checkboxes HTML
175
+ # including the label and hint, if there is one.
176
+ #
177
+ # @param attribute [String, Symbol] the attribute of the raido buttons
178
+ # @param checkbox_item [Hash] the options for the checkbox item
179
+ #
180
+ # @option checkbox_item [String] :classes additional CSS classes for the checkbox button HTML
181
+ # @option checkbox_item [Hash] :label the parameters that will be used to create the label for the checkbox button:
182
+ # - +:text+ (required) - the label text
183
+ # - +:options+ - default: { } - the options for govuk_label {govuk_label}
184
+ # @option checkbox_item [Hash] :hint (nil) the parameters that will be used to create the hint for the field
185
+ # - +:text+ (required) - the hint text
186
+ # - +:options+ - default: { } - the options for govuk_hint {govuk_hint}
187
+ # @option checkbox_item [Hash] :attributes ({}) any additional attributes that will be added as part of the checkbox button HTML
188
+ #
189
+ # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons, label and hint
190
+ # which is used in {govuk_checkbox_item} and {govuk_checkbox_item_with_form}
191
+
192
+ # rubocop:enable Metrics/AbcSize
193
+
194
+ def _govuk_checkbox_item(attribute, checkbox_item)
195
+ checkbox_item[:attributes] ||= {}
196
+ checkbox_item[:label][:options] ||= {}
197
+ checkbox_item[:label][:options][:classes] = "govuk-checkboxes__label #{checkbox_item[:label][:options][:classes]}".rstrip
198
+
199
+ set_item_options_for_hint('checkboxes', attribute, checkbox_item) if checkbox_item[:hint]
200
+
201
+ capture do
202
+ yield
203
+ concat(govuk_hint(checkbox_item[:hint][:text], **checkbox_item[:hint][:options])) if checkbox_item[:hint]
204
+ end
205
+ end
206
+ end
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CrownMarketplaceUtils
6
+ module GovUkHelper
7
+ module Field
8
+ # = GOV.UK Input
9
+ #
10
+ # This helper is used for generating the input component from the
11
+ # {https://design-system.service.gov.uk/components/text-input GDS - Components - Text Input}
12
+ #
13
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
14
+
15
+ module Input
16
+ include Field
17
+
18
+ # Generates the HTML for the GOV.UK input component
19
+ #
20
+ # @param attribute [String, Symbol] the attribute of the input
21
+ # @param error_message [String] the error message to be displayed
22
+ # @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
23
+ #
24
+ # @option govuk_input_options [Hash] :form_group_options see {govuk_field}
25
+ # @option govuk_input_options [Hash] :label see {govuk_field}
26
+ # @option govuk_input_options [Hash] :hint see {govuk_field}
27
+ # @option govuk_input_options [Hash] :input_options ({}) the options that will be used when rendering the input.
28
+ # See {govuk_input_field} for more details.
29
+ #
30
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Input
31
+ # which can then be rendered on the page
32
+
33
+ def govuk_input(attribute, error_message = nil, **govuk_input_options)
34
+ govuk_field(:input, attribute, error_message, **govuk_input_options) do |govuk_field_options|
35
+ concat(govuk_input_field(attribute, error_message, **govuk_field_options))
36
+ end
37
+ end
38
+
39
+ # Generates the HTML for the GOV.UK input component using an ActionView::Helpers::FormBuilder.
40
+ # Unlike {govuk_input}, the method will be able to automatically determine if the error message needs to be shown.
41
+ #
42
+ # @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the input
43
+ # @param attribute [String, Symbol] the attribute of the input
44
+ # @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
45
+ #
46
+ # @option govuk_input_options [Hash] :form_group_options see {govuk_field_with_form}
47
+ # @option govuk_input_options [Hash] :label see {govuk_field_with_form}
48
+ # @option govuk_input_options [Hash] :hint see {govuk_field_with_form}
49
+ # @option govuk_input_options [Hash] :input_options ({}) the options that will be used when rendering the input.
50
+ # See {govuk_input_field_with_form} for more details.
51
+ #
52
+ # @return (see govuk_input)
53
+
54
+ def govuk_input_with_form(form, attribute, **govuk_input_options)
55
+ govuk_field_with_form(:input, form, attribute, **govuk_input_options) do |govuk_field_options, error_message|
56
+ concat(govuk_input_field_with_form(form, attribute, error_message, **govuk_field_options))
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ # Generates the input HTML for {govuk_input}
63
+ #
64
+ # @param attribute [String, Symbol] the attribute of the input
65
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
66
+ # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
67
+ #
68
+ # @option (see _govuk_input_field)
69
+ #
70
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input}
71
+
72
+ def govuk_input_field(attribute, error_message, **govuk_text_input_options)
73
+ _govuk_input_field(error_message, **govuk_text_input_options) do |govuk_text_input_classes, govuk_text_input_attributes|
74
+ text_field_tag(attribute, govuk_text_input_options[:value], class: govuk_text_input_classes, **govuk_text_input_attributes)
75
+ end
76
+ end
77
+
78
+ # Generates the input HTML for {govuk_input_with_form}
79
+ #
80
+ # @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the input
81
+ # @param attribute [String, Symbol] the attribute of the input
82
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
83
+ # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
84
+ #
85
+ # @option (see _govuk_input_field)
86
+ #
87
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_with_form}
88
+
89
+ def govuk_input_field_with_form(form, attribute, error_message, **govuk_text_input_options)
90
+ _govuk_input_field(error_message, **govuk_text_input_options) do |govuk_text_input_classes, govuk_text_input_attributes|
91
+ form.text_field(attribute, class: govuk_text_input_classes, **govuk_text_input_attributes)
92
+ end
93
+ end
94
+
95
+ # Wrapper method used by {govuk_input_field} and {govuk_input_field_with_form} to generate the Input HTML
96
+ #
97
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
98
+ # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
99
+ #
100
+ # @option govuk_text_input_options [String] :classes additional CSS classes for the input HTML
101
+ # @option govuk_text_input_options [String] :value (nil) the value of the input
102
+ # @option govuk_text_input_options [Hash] :prefix (nil) optional prefix for the input field. See {govuk_fix} for more details.
103
+ # @option govuk_text_input_options [Hash] :suffix (nil) optional suffix for the input field. See {govuk_fix} for more details.
104
+ # @option govuk_text_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
105
+ #
106
+ # @yield the input HTML generated by {govuk_input_field} or {govuk_input_field_with_form}
107
+ #
108
+ # @yieldparam govuk_text_input_classes [Array] the classes for the input HTML
109
+ # @yieldparam govuk_text_input_attributes [Hash] additional attributes that will added as part of the HTML
110
+ #
111
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_field} or {govuk_input_field_with_form}
112
+
113
+ def _govuk_input_field(error_message, **govuk_text_input_options)
114
+ govuk_text_input_classes = ['govuk-input']
115
+ govuk_text_input_classes += ['govuk-input--error'] if error_message
116
+ govuk_text_input_classes << govuk_text_input_options[:classes]
117
+ govuk_text_input_options[:attributes] ||= {}
118
+
119
+ input_html = yield(govuk_text_input_classes, govuk_text_input_options[:attributes])
120
+
121
+ prefix = govuk_text_input_options[:prefix]
122
+ suffix = govuk_text_input_options[:suffix]
123
+
124
+ if prefix || suffix
125
+ tag.div(class: 'govuk-input__wrapper') do
126
+ capture do
127
+ concat(govuk_fix('pre', prefix)) if prefix
128
+ concat(input_html)
129
+ concat(govuk_fix('suf', suffix)) if suffix
130
+ end
131
+ end
132
+ else
133
+ input_html
134
+ end
135
+ end
136
+
137
+ # Generates the prefix and suffix HTML for {_govuk_input_field}
138
+ #
139
+ # @param fix [String] either +"pre"+ or +"suf"+
140
+ # @param govuk_fix_options [Hash] options that will be used in customising the HTML
141
+ #
142
+ # @option govuk_fix_options [String] :classes additional CSS classes for the input HTML
143
+ # @option govuk_text_input_options [Hash] :attributes ({ aria: { hidden: true } }) any additional attributes that will added as part of the HTML
144
+ #
145
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_field} or {govuk_input_field_with_form}
146
+
147
+ def govuk_fix(fix, govuk_fix_options)
148
+ govuk_fix_classes = ["govuk-input__#{fix}fix"]
149
+ govuk_fix_classes << govuk_fix_options[:classes]
150
+ govuk_fix_options[:attributes] ||= {}
151
+ (govuk_fix_options[:attributes][:aria] ||= {}).merge!({ hidden: true })
152
+
153
+ tag.div(govuk_fix_options[:text], class: govuk_fix_classes, **govuk_fix_options[:attributes])
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
159
+
160
+ ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CrownMarketplaceUtils
6
+ module GovUkHelper
7
+ module Field
8
+ # = GOV.UK Radios
9
+ #
10
+ # This helper is used for generating the radios component from the
11
+ # {https://design-system.service.gov.uk/components/radios GDS - Components - Radios}
12
+ #
13
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
14
+
15
+ module Radios
16
+ include Field
17
+
18
+ # Generates the HTML for the GOV.UK Radios component
19
+ #
20
+ # @param attribute [String, Symbol] the attribute of the raido buttons
21
+ # @param items [Array] array of radio items, see {_govuk_radios_fields}
22
+ # @param error_message [String] the error message to be displayed
23
+ # @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
24
+ #
25
+ # @option govuk_radios_options [Hash] :form_group_options see {govuk_fields}
26
+ # @option govuk_radios_options [Hash] :fieldset_options see {govuk_fields}
27
+ # @option govuk_radios_options [Hash] :hint see {govuk_field}
28
+ # @option govuk_radios_options [Hash] :radios_options ({}) the options that will be used when rendering the radio buttons.
29
+ # See {govuk_radios_fields} for more details.
30
+ #
31
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Radios
32
+ # which can then be rendered on the page
33
+
34
+ def govuk_radios(attribute, items, error_message = nil, **govuk_radios_options)
35
+ govuk_fields(:radios, attribute, error_message, **govuk_radios_options) do |govuk_field_options|
36
+ concat(govuk_radios_fields(attribute, items, **govuk_field_options))
37
+ end
38
+ end
39
+
40
+ # Generates the HTML for the GOV.UK Radios component using an ActionView::Helpers::FormBuilder.
41
+ # Unlike {govuk_radios}, the method will be able to automatically determine if the error message needs to be shown.
42
+ #
43
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
44
+ # @param attribute [String, Symbol] the attribute of the raido buttons
45
+ # @param items [Array] array of radio items, see {_govuk_radios_fields}
46
+ # @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
47
+ #
48
+ # @option govuk_radios_options [Hash] :form_group_options see {govuk_fields_with_form}
49
+ # @option govuk_radios_options [Hash] :fieldset_options see {govuk_fields_with_form}
50
+ # @option govuk_radios_options [Hash] :hint see {govuk_fields_with_form}
51
+ # @option govuk_radios_options [Hash] :radios_options ({}) the options that will be used when rendering the radio buttons.
52
+ # See {govuk_radios_fields_with_form} for more details.
53
+ #
54
+ # @return (see govuk_radios)
55
+
56
+ def govuk_radios_with_form(form, attribute, items, **govuk_radios_options)
57
+ govuk_fields_with_form(:radios, form, attribute, **govuk_radios_options) do |govuk_field_options|
58
+ concat(govuk_radios_fields_with_form(form, attribute, items, **govuk_field_options))
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ # Generates the radios HTML for {govuk_radios}
65
+ #
66
+ # @param attribute [String, Symbol] the attribute of the raido buttons
67
+ # @param items [Array] array of radio items, see {_govuk_radios_fields}
68
+ # @param govuk_radios_options [Hash] options that will be used in customising the HTML
69
+ #
70
+ # @option (see _govuk_radios_fields)
71
+ #
72
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios}
73
+
74
+ def govuk_radios_fields(attribute, items, **govuk_radios_options)
75
+ _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
76
+ govuk_radio_item(attribute, radio_item)
77
+ end
78
+ end
79
+
80
+ # Generates the radios HTML for {govuk_radios_with_form}
81
+ #
82
+ # @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the radio buttons
83
+ # @param attribute [String, Symbol] the attribute of the raido buttons
84
+ # @param items [Array] array of radio items, see {_govuk_radios_fields}
85
+ # @param govuk_radios_options [Hash] options that will be used in customising the HTML
86
+ #
87
+ # @option (see _govuk_radios_fields)
88
+ #
89
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios_with_form}
90
+
91
+ def govuk_radios_fields_with_form(form, attribute, items, **govuk_radios_options)
92
+ _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
93
+ govuk_radio_item_with_form(form, attribute, radio_item)
94
+ end
95
+ end
96
+
97
+ # Wrapper method used by {govuk_radios_fields} and {govuk_radios_fields_with_form} to generate the radios HTML
98
+ #
99
+ # @param items [Array] array of radio items.
100
+ # Each item is a hash which can be:
101
+ # - +:divider+ text to separate radio items
102
+ # - radio item, see {_govuk_radio_item}
103
+ # @param govuk_radios_options [Hash] options that will be used in customising the HTML
104
+ #
105
+ # @option govuk_radios_options [String] :classes additional CSS classes for the radios HTML
106
+ # @option govuk_radios_options [Hash] :attributes ({ module: 'govuk-radios' }) any additional attributes that will added as part of the HTML
107
+ #
108
+ # @yield the radio item HTML generated by {govuk_radios_fields} or {govuk_radios_fields_with_form}
109
+ #
110
+ # @yieldparam radio_item [Hash] the current radio item to be rendered
111
+ #
112
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios_fields} or {govuk_radios_fields_with_form}
113
+
114
+ def _govuk_radios_fields(items, **govuk_radios_options)
115
+ govuk_radios_classes = ['govuk-radios']
116
+ govuk_radios_classes << govuk_radios_options[:classes]
117
+ ((govuk_radios_options[:attributes] ||= {})[:data] ||= {}).merge!({ module: 'govuk-radios' })
118
+
119
+ tag.div(class: govuk_radios_classes, **govuk_radios_options[:attributes]) do
120
+ capture do
121
+ items.each do |radio_item|
122
+ concat(
123
+ if radio_item[:divider]
124
+ tag.div(radio_item[:divider], class: 'govuk-radios__divider')
125
+ else
126
+ tag.div(class: 'govuk-radios__item') do
127
+ yield(radio_item)
128
+ end
129
+ end
130
+ )
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ # Generates the HTML for a radio button for {govuk_radios_fields_with_form}
137
+ #
138
+ # @param (see _govuk_radio_item)
139
+ #
140
+ # @option (see _govuk_radio_item)
141
+ #
142
+ # @return (see _govuk_radio_item)
143
+
144
+ def govuk_radio_item(attribute, radio_item)
145
+ _govuk_radio_item(attribute, radio_item) do
146
+ label_attribute = radio_item[:attributes][:id] || "#{attribute}[#{radio_item[:value]}]"
147
+
148
+ concat(radio_button_tag(attribute, radio_item[:value], radio_item[:checked], class: 'govuk-radios__input', **radio_item[:attributes]))
149
+ concat(govuk_label(label_attribute, radio_item[:label][:text], **radio_item[:label][:options]))
150
+ end
151
+ end
152
+
153
+ # Generates the HTML for a radio button for {govuk_radios_fields}
154
+ #
155
+ # @param (see _govuk_radio_item)
156
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
157
+ #
158
+ # @option (see _govuk_radio_item)
159
+ #
160
+ # @return (see _govuk_radio_item)
161
+
162
+ def govuk_radio_item_with_form(form, attribute, radio_item)
163
+ _govuk_radio_item(attribute, radio_item) do
164
+ (radio_item[:label][:options][:attributes] ||= {})[:value] = radio_item[:value]
165
+ radio_item[:label][:options][:attributes][:for] = radio_item[:attributes][:id] if radio_item[:attributes][:id]
166
+
167
+ concat(form.radio_button(attribute, radio_item[:value], class: 'govuk-radios__input', **radio_item[:attributes]))
168
+ concat(govuk_label_with_form(form, attribute, radio_item[:label][:text], **radio_item[:label][:options]))
169
+ end
170
+ end
171
+
172
+ # Wrapper method used by {govuk_radio_item} and {govuk_radio_item_with_form} to generate the radios HTML
173
+ # including the label and hint, if there is one.
174
+ #
175
+ # @param attribute [String, Symbol] the attribute of the raido buttons
176
+ # @param radio_item [Hash] the options for the radio item
177
+ #
178
+ # @option radio_item [String] :classes additional CSS classes for the radio button HTML
179
+ # @option radio_item [Hash] :label the parameters that will be used to create the label for the radio button:
180
+ # - +:text+ (required) - the label text
181
+ # - +:options+ - default: { } - the options for govuk_label {govuk_label}
182
+ # @option radio_item [Hash] :hint (nil) the parameters that will be used to create the hint for the field
183
+ # - +:text+ (required) - the hint text
184
+ # - +:options+ - default: { } - the options for govuk_hint {govuk_hint}
185
+ # @option radio_item [Hash] :attributes ({}) any additional attributes that will be added as part of the radio button HTML
186
+ #
187
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons, label and hint
188
+ # which is used in {govuk_radio_item} and {govuk_radio_item_with_form}
189
+
190
+ def _govuk_radio_item(attribute, radio_item)
191
+ radio_item[:attributes] ||= {}
192
+ radio_item[:label][:options] ||= {}
193
+ radio_item[:label][:options][:classes] = "govuk-radios__label #{radio_item[:label][:options][:classes]}".rstrip
194
+
195
+ set_item_options_for_hint('radios', attribute, radio_item) if radio_item[:hint]
196
+
197
+ capture do
198
+ yield
199
+ concat(govuk_hint(radio_item[:hint][:text], **radio_item[:hint][:options])) if radio_item[:hint]
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end