crown_marketplace_utils 0.1.0.beta.3 → 0.1.0.beta.5

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