ccs-frontend_helpers 0.1.0.rc.1

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.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/.rubocop.yml +127 -0
  4. data/CHANGELOG.md +44 -0
  5. data/Gemfile +10 -0
  6. data/Gemfile.lock +241 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +102 -0
  9. data/Rakefile +10 -0
  10. data/ccs-frontend_helpers.gemspec +47 -0
  11. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_panels.rb +79 -0
  12. data/lib/ccs/frontend_helpers/ccs_frontend/footer.rb +141 -0
  13. data/lib/ccs/frontend_helpers/ccs_frontend/header.rb +205 -0
  14. data/lib/ccs/frontend_helpers/ccs_frontend/logo.rb +49 -0
  15. data/lib/ccs/frontend_helpers/ccs_frontend.rb +20 -0
  16. data/lib/ccs/frontend_helpers/govuk_frontend/accordion.rb +115 -0
  17. data/lib/ccs/frontend_helpers/govuk_frontend/back_link.rb +39 -0
  18. data/lib/ccs/frontend_helpers/govuk_frontend/breadcrumbs.rb +76 -0
  19. data/lib/ccs/frontend_helpers/govuk_frontend/button.rb +127 -0
  20. data/lib/ccs/frontend_helpers/govuk_frontend/cookie_banner.rb +136 -0
  21. data/lib/ccs/frontend_helpers/govuk_frontend/details.rb +46 -0
  22. data/lib/ccs/frontend_helpers/govuk_frontend/error_message.rb +67 -0
  23. data/lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb +100 -0
  24. data/lib/ccs/frontend_helpers/govuk_frontend/field/character_count.rb +165 -0
  25. data/lib/ccs/frontend_helpers/govuk_frontend/field/checkboxes.rb +200 -0
  26. data/lib/ccs/frontend_helpers/govuk_frontend/field/date_input.rb +153 -0
  27. data/lib/ccs/frontend_helpers/govuk_frontend/field/file_upload.rb +83 -0
  28. data/lib/ccs/frontend_helpers/govuk_frontend/field/input.rb +153 -0
  29. data/lib/ccs/frontend_helpers/govuk_frontend/field/radios.rb +201 -0
  30. data/lib/ccs/frontend_helpers/govuk_frontend/field/select.rb +124 -0
  31. data/lib/ccs/frontend_helpers/govuk_frontend/field/textarea.rb +106 -0
  32. data/lib/ccs/frontend_helpers/govuk_frontend/field.rb +213 -0
  33. data/lib/ccs/frontend_helpers/govuk_frontend/fieldset.rb +71 -0
  34. data/lib/ccs/frontend_helpers/govuk_frontend/footer.rb +183 -0
  35. data/lib/ccs/frontend_helpers/govuk_frontend/form_group.rb +50 -0
  36. data/lib/ccs/frontend_helpers/govuk_frontend/header.rb +161 -0
  37. data/lib/ccs/frontend_helpers/govuk_frontend/hint.rb +38 -0
  38. data/lib/ccs/frontend_helpers/govuk_frontend/inset_text.rb +44 -0
  39. data/lib/ccs/frontend_helpers/govuk_frontend/label.rb +92 -0
  40. data/lib/ccs/frontend_helpers/govuk_frontend/notification_banner.rb +136 -0
  41. data/lib/ccs/frontend_helpers/govuk_frontend/pagination.rb +336 -0
  42. data/lib/ccs/frontend_helpers/govuk_frontend/panel.rb +51 -0
  43. data/lib/ccs/frontend_helpers/govuk_frontend/phase_banner.rb +49 -0
  44. data/lib/ccs/frontend_helpers/govuk_frontend/skip_link.rb +40 -0
  45. data/lib/ccs/frontend_helpers/govuk_frontend/step_by_step_navigation.rb +215 -0
  46. data/lib/ccs/frontend_helpers/govuk_frontend/summary_list.rb +226 -0
  47. data/lib/ccs/frontend_helpers/govuk_frontend/table.rb +124 -0
  48. data/lib/ccs/frontend_helpers/govuk_frontend/tabs.rb +95 -0
  49. data/lib/ccs/frontend_helpers/govuk_frontend/tag.rb +42 -0
  50. data/lib/ccs/frontend_helpers/govuk_frontend/warning_text.rb +53 -0
  51. data/lib/ccs/frontend_helpers/govuk_frontend.rb +82 -0
  52. data/lib/ccs/frontend_helpers/shared_methods.rb +27 -0
  53. data/lib/ccs/frontend_helpers/version.rb +7 -0
  54. data/lib/ccs/frontend_helpers.rb +20 -0
  55. data/sig/ccs/frontend_helpers.rbs +6 -0
  56. metadata +241 -0
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CCS
6
+ module FrontendHelpers
7
+ module GovUKFrontend
8
+ module Field
9
+ # = GOV.UK Input
10
+ #
11
+ # This helper is used for generating the input component from the
12
+ # {https://design-system.service.gov.uk/components/text-input GDS - Components - Text Input}
13
+ #
14
+ # This is considered a Field module and so makes use of the methods in {CCS::FrontendHelpers::GovUKFrontend::Field}
15
+
16
+ module Input
17
+ include Field
18
+
19
+ # Generates the HTML for the GOV.UK input component
20
+ #
21
+ # @param attribute [String, Symbol] the attribute of the input
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 [String] :error_message (nil) the error message to be displayed
25
+ # @option govuk_input_options [ActiveModel] :model (nil) optional model that can be used to find an error message
26
+ # @option govuk_label_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
27
+ # the input tag and find the error message
28
+ # @option govuk_input_options [Hash] :form_group see {govuk_field}
29
+ # @option govuk_input_options [Hash] :label see {govuk_field}
30
+ # @option govuk_input_options [Hash] :hint see {govuk_field}
31
+ # @option govuk_input_options [Hash] :input ({}) the options that will be used when rendering the input.
32
+ # See {_govuk_input_field} for more details.
33
+ #
34
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Input
35
+ # which can then be rendered on the page
36
+
37
+ def govuk_input(attribute, **govuk_input_options)
38
+ govuk_field(:input, attribute, **govuk_input_options) do |govuk_field_options, error_message|
39
+ concat(_govuk_input_field(error_message, **govuk_field_options) do
40
+ field_type = get_field_type(govuk_field_options[:field_type])
41
+ govuk_field_options[:value] = govuk_input_options[:model].send(attribute) if govuk_input_options[:model]
42
+
43
+ if govuk_input_options[:form]
44
+ govuk_input_form(govuk_input_options[:form], field_type, attribute, **govuk_field_options)
45
+ else
46
+ govuk_input_tag(field_type, attribute, **govuk_field_options)
47
+ end
48
+ end)
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ # Generates the input HTML for {govuk_input}
55
+ #
56
+ # @param field_type [String] the inout field type
57
+ # @param attribute [String, Symbol] the attribute of the input
58
+ # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
59
+ #
60
+ # @option (see _govuk_input_field)
61
+ #
62
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input}
63
+
64
+ def govuk_input_tag(field_type, attribute, **govuk_text_input_options)
65
+ send("#{field_type}_tag", attribute, govuk_text_input_options[:value], **govuk_text_input_options[:attributes])
66
+ end
67
+
68
+ # Generates the input HTML for {govuk_input} when there is a ActionView::Helpers::FormBuilder
69
+ #
70
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the input tag
71
+ # @param (see govuk_input_tag)
72
+ #
73
+ # @option (see _govuk_input_field)
74
+ #
75
+ # @return (see govuk_input_tag)
76
+
77
+ def govuk_input_form(form, field_type, attribute, **govuk_text_input_options)
78
+ form.send(field_type, attribute, **govuk_text_input_options[:attributes])
79
+ end
80
+
81
+ # Wrapper method used by {govuk_input} to generate the Input HTML
82
+ #
83
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
84
+ # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
85
+ #
86
+ # @option govuk_text_input_options [String] :classes additional CSS classes for the input HTML
87
+ # @option govuk_text_input_options [String] :value (nil) the value of the input
88
+ # @option govuk_text_input_options [Symbol] :field_type the type of the input field, see {get_field_type} for options
89
+ # @option govuk_text_input_options [Hash] :prefix (nil) optional prefix for the input field. See {govuk_fix} for more details.
90
+ # @option govuk_text_input_options [Hash] :suffix (nil) optional suffix for the input field. See {govuk_fix} for more details.
91
+ # @option govuk_text_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
92
+ #
93
+ # @yield the input HTML generated by {govuk_input_tag} or {govuk_input_form}
94
+ #
95
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used by {govuk_input}
96
+
97
+ def _govuk_input_field(error_message, **govuk_text_input_options)
98
+ initialise_attributes_and_set_classes(govuk_text_input_options, "govuk-input #{'govuk-input--error' if error_message}".rstrip)
99
+
100
+ input_html = yield
101
+
102
+ prefix = govuk_text_input_options[:prefix]
103
+ suffix = govuk_text_input_options[:suffix]
104
+
105
+ if prefix || suffix
106
+ tag.div(class: 'govuk-input__wrapper') do
107
+ concat(govuk_fix('pre', prefix)) if prefix
108
+ concat(input_html)
109
+ concat(govuk_fix('suf', suffix)) if suffix
110
+ end
111
+ else
112
+ input_html
113
+ end
114
+ end
115
+
116
+ # Generates the prefix and suffix HTML for {_govuk_input_field}
117
+ #
118
+ # @param fix [String] either +"pre"+ or +"suf"+
119
+ # @param govuk_fix_options [Hash] options that will be used in customising the HTML
120
+ #
121
+ # @option govuk_fix_options [String] :classes additional CSS classes for the input HTML
122
+ # @option govuk_text_input_options [Hash] :attributes ({ aria: { hidden: true } }) any additional attributes that will added as part of the HTML
123
+ #
124
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {_govuk_input_field}
125
+
126
+ def govuk_fix(fix, govuk_fix_options)
127
+ initialise_attributes_and_set_classes(govuk_fix_options, "govuk-input__#{fix}fix")
128
+
129
+ (govuk_fix_options[:attributes][:aria] ||= {})[:hidden] = true
130
+
131
+ tag.div(govuk_fix_options[:text], **govuk_fix_options[:attributes])
132
+ end
133
+
134
+ # Returns the field type used to create the rails input tag
135
+ #
136
+ # @param field_type [Symbol] the type of the field, defaults to text
137
+ # Allowed values are:
138
+ # - +:email+
139
+ # - +:password+
140
+
141
+ def get_field_type(field_type)
142
+ case field_type
143
+ when :email, :password
144
+ :"#{field_type}_field"
145
+ else
146
+ :text_field
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,201 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CCS
6
+ module FrontendHelpers
7
+ module GovUKFrontend
8
+ module Field
9
+ # = GOV.UK Radios
10
+ #
11
+ # This helper is used for generating the radios component from the
12
+ # {https://design-system.service.gov.uk/components/radios GDS - Components - Radios}
13
+ #
14
+ # This is considered a Field module and so makes use of the methods in {CCS::FrontendHelpers::GovUKFrontend::Field}
15
+
16
+ module Radios
17
+ include Field
18
+
19
+ # Generates the HTML for the GOV.UK Radios component
20
+ #
21
+ # @param attribute [String, Symbol] the attribute of the raido buttons
22
+ # @param items [Array] array of radio items, see {_govuk_radios_fields}
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 [String] :error_message (nil) the error message to be displayed
26
+ # @option govuk_radios_options [ActiveModel] :model (nil) optional model that can be used to find an error message
27
+ # @option govuk_radios_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
28
+ # the radio tags and find the error message
29
+ # @option govuk_radios_options [Hash] :form_group see {govuk_fields}
30
+ # @option govuk_radios_options [Hash] :fieldset see {govuk_fields}
31
+ # @option govuk_radios_options [Hash] :hint see {govuk_field}
32
+ # @option govuk_radios_options [Hash] :radios ({}) the options that will be used when rendering the radio buttons.
33
+ # See {govuk_radio_fields_tag} for more details.
34
+ #
35
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Radios
36
+ # which can then be rendered on the page
37
+
38
+ def govuk_radios(attribute, items, **govuk_radios_options)
39
+ govuk_fields(:radios, attribute, **govuk_radios_options) do |govuk_field_options|
40
+ if govuk_radios_options[:model] || govuk_radios_options[:form]
41
+ value = (govuk_radios_options[:model] || govuk_radios_options[:form].object).send(attribute)
42
+ items.each { |item| item[:checked] = item[:value] == value }
43
+ end
44
+
45
+ concat(
46
+ if govuk_radios_options[:form]
47
+ govuk_radio_fields_form(govuk_radios_options[:form], attribute, items, **govuk_field_options)
48
+ else
49
+ govuk_radio_fields_tag(attribute, items, **govuk_field_options)
50
+ end
51
+ )
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ # Generates the radios HTML for {govuk_radios}
58
+ #
59
+ # @param attribute [String, Symbol] the attribute of the raido buttons
60
+ # @param items [Array] array of radio items, see {_govuk_radios_fields}
61
+ # @param govuk_radios_options [Hash] options that will be used in customising the HTML
62
+ #
63
+ # @option (see _govuk_radios_fields)
64
+ #
65
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios}
66
+
67
+ def govuk_radio_fields_tag(attribute, items, **govuk_radios_options)
68
+ _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
69
+ govuk_radio_item_tag(attribute, radio_item)
70
+ end
71
+ end
72
+
73
+ # Generates the radios HTML for {govuk_radios} when there is a ActionView::Helpers::FormBuilder
74
+ #
75
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
76
+ # @param (see govuk_radio_fields_tag)
77
+ #
78
+ # @option (see _govuk_radios_fields)
79
+ #
80
+ # @return (see govuk_radio_fields_tag)
81
+
82
+ def govuk_radio_fields_form(form, attribute, items, **govuk_radios_options)
83
+ _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
84
+ govuk_radio_item_form(form, attribute, radio_item)
85
+ end
86
+ end
87
+
88
+ # Wrapper method used by {govuk_radio_fields_tag} and {govuk_radio_fields_form} to generate the radios HTML
89
+ #
90
+ # @param items [Array] array of radio items.
91
+ # Each item is a hash which can be:
92
+ # - +:divider+ text to separate radio items
93
+ # - radio item, see {_govuk_radio_item}
94
+ # @param govuk_radios_options [Hash] options that will be used in customising the HTML
95
+ #
96
+ # @option govuk_radios_options [String] :classes additional CSS classes for the radios HTML
97
+ # @option govuk_radios_options [Hash] :attributes ({ module: 'govuk-radios' }) any additional attributes that will added as part of the HTML
98
+ #
99
+ # @yield the radio item HTML generated by {govuk_radio_fields_tag} or {govuk_radio_fields_form}
100
+ #
101
+ # @yieldparam radio_item [Hash] the current radio item to be rendered
102
+ #
103
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radio_fields_tag} or {govuk_radio_fields_form}
104
+
105
+ def _govuk_radios_fields(items, **govuk_radios_options)
106
+ initialise_attributes_and_set_classes(govuk_radios_options, 'govuk-radios')
107
+ set_data_module(govuk_radios_options, 'govuk-radios')
108
+
109
+ tag.div(**govuk_radios_options[:attributes]) do
110
+ items.each do |radio_item|
111
+ concat(
112
+ if radio_item[:divider]
113
+ tag.div(radio_item[:divider], class: 'govuk-radios__divider')
114
+ else
115
+ tag.div(class: 'govuk-radios__item') do
116
+ yield(radio_item)
117
+ end
118
+ end
119
+ )
120
+ end
121
+ end
122
+ end
123
+
124
+ # Generates the HTML for a radio button for {govuk_radio_fields_form}
125
+ #
126
+ # @param (see _govuk_radio_item)
127
+ #
128
+ # @option (see _govuk_radio_item)
129
+ #
130
+ # @return (see _govuk_radio_item)
131
+
132
+ def govuk_radio_item_tag(attribute, radio_item)
133
+ _govuk_radio_item(attribute, radio_item) do
134
+ label_attribute = radio_item[:attributes][:id] || "#{attribute}[#{radio_item[:value]}]"
135
+
136
+ concat(radio_button_tag(attribute, radio_item[:value], radio_item[:checked], **radio_item[:attributes]))
137
+ concat(govuk_label(label_attribute, radio_item[:label][:text], **radio_item[:label]))
138
+ end
139
+ end
140
+
141
+ # Generates the HTML for a radio button for {govuk_radio_fields_tag}
142
+ #
143
+ # @param (see _govuk_radio_item)
144
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
145
+ #
146
+ # @option (see _govuk_radio_item)
147
+ #
148
+ # @return (see _govuk_radio_item)
149
+
150
+ def govuk_radio_item_form(form, attribute, radio_item)
151
+ _govuk_radio_item(attribute, radio_item) do
152
+ (radio_item[:label][:attributes] ||= {})[:value] = radio_item[:value]
153
+ radio_item[:label][:attributes][:for] = radio_item[:attributes][:id] if radio_item[:attributes][:id]
154
+
155
+ concat(form.radio_button(attribute, radio_item[:value], **radio_item[:attributes]))
156
+ concat(govuk_label(attribute, radio_item[:label][:text], form: form, **radio_item[:label]))
157
+ end
158
+ end
159
+
160
+ # rubocop:disable Metrics/AbcSize
161
+
162
+ # Wrapper method used by {govuk_radio_item_tag} and {govuk_radio_item_form} to generate the radios HTML
163
+ # including the label and hint, if there is one.
164
+ #
165
+ # @param attribute [String, Symbol] the attribute of the raido buttons
166
+ # @param radio_item [Hash] the options for the radio item
167
+ #
168
+ # @option radio_item [String] :classes additional CSS classes for the radio button HTML
169
+ # @option radio_item [Hash] :label the parameters that will be used to create the label for the radio button, see {govuk_label}
170
+ # @option radio_item [Hash] :hint (nil) the parameters that will be used to create the hint for the radio button, see {govuk_hint}.
171
+ # If no hint is given then no hint will be rendered
172
+ # @option radio_item [Hash] :conditional (nil) content that will appear when the radio button is checked.
173
+ # It can have the following options:
174
+ # - +[:content]+ the content that will be shown
175
+ # - +[:attributes][:id]+ the id of the conditional section
176
+ # If no conditional is given then no conditional content will be rendered
177
+ # @option radio_item [Hash] :attributes ({}) any additional attributes that will be added as part of the radio button HTML
178
+ #
179
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons, label and hint
180
+ # which is used in {govuk_radio_item_tag} and {govuk_radio_item_form}
181
+
182
+ def _govuk_radio_item(attribute, radio_item)
183
+ (radio_item[:attributes] ||= {})[:class] = 'govuk-radios__input'
184
+
185
+ radio_item[:label] ||= {}
186
+ radio_item[:label][:classes] = "govuk-radios__label #{radio_item[:label][:classes]}".rstrip
187
+
188
+ set_item_options_for_hint('radios', attribute, radio_item)
189
+ set_conditional_item_options('radios', attribute, radio_item)
190
+
191
+ yield
192
+ concat(govuk_hint(radio_item[:hint][:text], **radio_item[:hint])) if radio_item[:hint]
193
+ concat(tag.div(radio_item[:conditional][:content], class: radio_item[:conditional][:attributes][:class], id: radio_item[:conditional][:attributes][:id])) if radio_item[:conditional]
194
+ end
195
+
196
+ # rubocop:enable Metrics/AbcSize
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+ require_relative '../field'
5
+
6
+ module CCS
7
+ module FrontendHelpers
8
+ module GovUKFrontend
9
+ module Field
10
+ # = GOV.UK Select
11
+ #
12
+ # This helper is used for generating the select component from the
13
+ # {https://design-system.service.gov.uk/components/select GDS - Components - Select}
14
+ #
15
+ # This is considered a Field module and so makes use of the methods in {CCS::FrontendHelpers::GovUKFrontend::Field}
16
+
17
+ module Select
18
+ include ActionView::Helpers::FormOptionsHelper
19
+ include Field
20
+
21
+ # Generates the HTML for the GOV.UK Select component
22
+ #
23
+ # @param attribute [String, Symbol] the attribute of the select field
24
+ # @param items [Array] array of option items for the select, see {govuk_map_select_items}
25
+ # @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
26
+ #
27
+ # @option govuk_select_options [String] :error_message (nil) the error message to be displayed
28
+ # @option govuk_select_options [ActiveModel] :model (nil) optional model that can be used to find an error message
29
+ # @option govuk_select_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
30
+ # the select tag and find the error message
31
+ # @option govuk_select_options [Hash] :form_group see {govuk_field}
32
+ # @option govuk_select_options [Hash] :label see {govuk_field}
33
+ # @option govuk_select_options [Hash] :hint see {govuk_field}
34
+ # @option govuk_select_options [Hash] :select ({}) the options that will be used when rendering the select field.
35
+ # See {govuk_select_tag} for more details.
36
+ #
37
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Select
38
+ # which can then be rendered on the page
39
+
40
+ def govuk_select(attribute, items, **govuk_select_options)
41
+ govuk_field(:select, attribute, **govuk_select_options) do |govuk_field_options, error_message|
42
+ initialise_attributes_and_set_classes(govuk_field_options, "govuk-select #{'govuk-select--error' if error_message}".rstrip)
43
+
44
+ (govuk_select_options[:select] ||= {})[:selected] = govuk_select_options[:model].send(attribute) if govuk_select_options[:model]
45
+
46
+ concat(
47
+ if govuk_select_options[:form]
48
+ govuk_select_form(govuk_select_options[:form], attribute, items, **govuk_field_options)
49
+ else
50
+ govuk_select_tag(attribute, items, **govuk_field_options)
51
+ end
52
+ )
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ # Generates the select HTML for {govuk_select}
59
+ #
60
+ # @param attribute [String, Symbol] the attribute of the select field
61
+ # @param items [Array] array of option items for the select, see {govuk_map_select_items}
62
+ # @param govuk_select_options [Hash] options that will be used in customising the HTML
63
+ #
64
+ # @option govuk_select_options [String] :classes additional CSS classes for the select HTML
65
+ # @option govuk_select_options [String] :selected (nil) the selected option
66
+ # @option govuk_select_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
67
+ #
68
+ # @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select}
69
+
70
+ def govuk_select_tag(attribute, items, **govuk_select_options)
71
+ select_tag(
72
+ attribute,
73
+ options_for_select(
74
+ govuk_map_select_items(items),
75
+ govuk_select_options[:selected]
76
+ ),
77
+ **govuk_select_options[:attributes]
78
+ )
79
+ end
80
+
81
+ # Generates the select HTML for {govuk_select} when there is a ActionView::Helpers::FormBuilder
82
+ #
83
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the select field
84
+ # @param (see govuk_select_tag)
85
+ #
86
+ # @option (see govuk_select_tag)
87
+ #
88
+ # @return (see govuk_select_tag)
89
+
90
+ def govuk_select_form(form, attribute, items, **govuk_select_options)
91
+ form.select(
92
+ attribute,
93
+ govuk_map_select_items(items),
94
+ govuk_select_options[:select_options] || {},
95
+ **govuk_select_options[:attributes]
96
+ )
97
+ end
98
+
99
+ # Maps the items into an array that can be used to generate the options for
100
+ # {govuk_select_tag} and {govuk_select_form}
101
+ #
102
+ # @param items [Array] array of option items for the select
103
+ #
104
+ # @option items [String] :text the text of the option item.
105
+ # If this is blank the value is used
106
+ # @option items [String] :value the value of the option item
107
+ # @option items [Hash] :attributes ({}) any additional attributes that will added as part of the option HTML
108
+ #
109
+ # @return [Array] array of option params that are used in {govuk_select_tag} and {govuk_select_form}
110
+
111
+ def govuk_map_select_items(items)
112
+ items.map do |item|
113
+ [
114
+ item[:text] || item[:value],
115
+ item[:value],
116
+ (item[:attributes] || {})
117
+ ]
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CCS
6
+ module FrontendHelpers
7
+ module GovUKFrontend
8
+ module Field
9
+ # = GOV.UK Textarea
10
+ #
11
+ # This helper is used for generating the textarea component from the
12
+ # {https://design-system.service.gov.uk/components/textarea GDS - Components - Textarea}
13
+ #
14
+ # This is considered a Field module and so makes use of the methods in {CCS::FrontendHelpers::GovUKFrontend::Field}
15
+
16
+ module Textarea
17
+ include Field
18
+
19
+ # Generates the HTML for the GOV.UK textarea component
20
+ #
21
+ # @param attribute [String, Symbol] the attribute of the textarea
22
+ # @param govuk_textarea_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
23
+ #
24
+ # @option govuk_textarea_options [String] :error_message (nil) the error message to be displayed
25
+ # @option govuk_textarea_options [ActiveModel] :model (nil) optional model that can be used to find an error message
26
+ # @option govuk_textarea_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
27
+ # the textarea tag and find the error message
28
+ # @option govuk_textarea_options [Hash] :form_group see {govuk_field}
29
+ # @option govuk_textarea_options [Hash] :label see {govuk_field}
30
+ # @option govuk_textarea_options [Hash] :hint see {govuk_field}
31
+ # @option govuk_textarea_options [Hash] :textarea ({}) the options that will be used when rendering the textarea.
32
+ # See {set_govuk_textarea_field_options} for more details.
33
+ #
34
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK textarea
35
+ # which can then be rendered on the page
36
+
37
+ def govuk_textarea(attribute, **govuk_textarea_options)
38
+ govuk_field(:textarea, attribute, **govuk_textarea_options) do |govuk_field_options, error_message|
39
+ set_govuk_textarea_field_options(error_message, govuk_field_options)
40
+ (govuk_textarea_options[:textarea] ||= {})[:content] = govuk_textarea_options[:model].send(attribute) if govuk_textarea_options[:model]
41
+
42
+ concat(
43
+ if govuk_textarea_options[:form]
44
+ govuk_textarea_form(govuk_textarea_options[:form], attribute, **govuk_field_options)
45
+ else
46
+ govuk_textarea_tag(attribute, **govuk_field_options)
47
+ end
48
+ )
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ # Generates the textarea HTML for {govuk_textarea}
55
+ #
56
+ # @param attribute [String, Symbol] the attribute of the textarea
57
+ # @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
58
+ #
59
+ # @option (see set_govuk_textarea_field_options)
60
+ #
61
+ # @return [ActiveSupport::SafeBuffer] the HTML for the textarea field which is used in {govuk_textarea}
62
+
63
+ def govuk_textarea_tag(attribute, **govuk_text_textarea_options)
64
+ text_area_tag(
65
+ attribute,
66
+ govuk_text_textarea_options[:content],
67
+ **govuk_text_textarea_options[:attributes]
68
+ )
69
+ end
70
+
71
+ # Generates the textarea HTML for {govuk_textarea} when there is a ActionView::Helpers::FormBuilder
72
+ #
73
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the textarea
74
+ # @param (see govuk_textarea_tag)
75
+ #
76
+ # @option (see set_govuk_textarea_field_options)
77
+ #
78
+ # @return (see govuk_textarea_tag)
79
+
80
+ def govuk_textarea_form(form, attribute, **govuk_text_textarea_options)
81
+ form.text_area(
82
+ attribute,
83
+ **govuk_text_textarea_options[:attributes]
84
+ )
85
+ end
86
+
87
+ # Initialises the attributes for the textarea input
88
+ #
89
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
90
+ # @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
91
+ #
92
+ # @option govuk_text_textarea_options [String] :classes additional CSS classes for the textarea HTML
93
+ # @option govuk_text_textarea_options [String] :content (nil) the content of the textarea
94
+ # @option govuk_text_textarea_options [String] :rows (5) the number of rows for the text area
95
+ # @option govuk_text_textarea_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
96
+
97
+ def set_govuk_textarea_field_options(error_message, govuk_text_textarea_options)
98
+ initialise_attributes_and_set_classes(govuk_text_textarea_options, "govuk-textarea #{'govuk-textarea--error' if error_message}".rstrip)
99
+
100
+ govuk_text_textarea_options[:attributes][:rows] ||= govuk_text_textarea_options[:rows] || 5
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end