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

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 (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 Checkboxes
@@ -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 checkbox items, see {_govuk_checkboxes_fields}
22
- # @param error_message [String] the error message to be displayed
23
22
  # @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
24
23
  #
25
- # @option govuk_checkboxes_options [Hash] :form_group_options see {govuk_fields}
26
- # @option govuk_checkboxes_options [Hash] :fieldset_options see {govuk_fields}
24
+ # @option govuk_checkboxes_options [String] :error_message (nil) the error message to be displayed
25
+ # @option govuk_checkboxes_options [ActiveModel] :model (nil) optional model that can be used to find an error message
26
+ # @option govuk_checkboxes_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
27
+ # the checkbox tags and find the error message
28
+ # @option govuk_checkboxes_options [Hash] :form_group see {govuk_fields}
29
+ # @option govuk_checkboxes_options [Hash] :fieldset see {govuk_fields}
27
30
  # @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.
31
+ # @option govuk_checkboxes_options [Hash] :checkboxes ({}) the options that will be used when rendering the checkbox buttons.
32
+ # See {_govuk_checkboxes_fields} for more details.
30
33
  #
31
34
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Checkboxes
32
35
  # which can then be rendered on the page
33
36
 
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)
37
+ def govuk_checkboxes(attribute, items, **govuk_checkboxes_options)
38
+ govuk_fields(:checkboxes, attribute, **govuk_checkboxes_options) do |govuk_field_options|
39
+ if govuk_checkboxes_options[:model] || govuk_checkboxes_options[:form]
40
+ values = (govuk_checkboxes_options[:model] || govuk_checkboxes_options[:form].object).send(attribute) || []
41
+ items.each { |item| item[:checked] = values.include?(item[:value]) }
42
+ end
55
43
 
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))
44
+ concat(
45
+ if govuk_checkboxes_options[:form]
46
+ govuk_checkbox_field_form(govuk_checkboxes_options[:form], attribute, items, **govuk_field_options)
47
+ else
48
+ govuk_checkbox_field_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 checkbox buttons which is used in {govuk_checkboxes}
73
65
 
74
- def govuk_checkboxes_fields(attribute, items, **govuk_checkboxes_options)
66
+ def govuk_checkbox_field_tag(attribute, items, **govuk_checkboxes_options)
75
67
  _govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
76
- govuk_checkbox_item(attribute, checkbox_item)
68
+ govuk_checkbox_item_tag(attribute, checkbox_item)
77
69
  end
78
70
  end
79
71
 
80
- # Generates the checkboxes HTML for {govuk_checkboxes_with_form}
72
+ # Generates the checkboxes HTML for {govuk_checkboxes} when there is a ActionView::Helpers::FormBuilder
81
73
  #
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
74
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
75
+ # @param (see govuk_checkbox_field_tag)
86
76
  #
87
77
  # @option (see _govuk_checkboxes_fields)
88
78
  #
89
- # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_with_form}
79
+ # @return (see govuk_checkbox_field_tag)
90
80
 
91
- def govuk_checkboxes_fields_with_form(form, attribute, items, **govuk_checkboxes_options)
81
+ def govuk_checkbox_field_form(form, attribute, items, **govuk_checkboxes_options)
92
82
  _govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
93
- govuk_checkbox_item_with_form(form, attribute, checkbox_item)
83
+ govuk_checkbox_item_form(form, attribute, checkbox_item)
94
84
  end
95
85
  end
96
86
 
97
- # Wrapper method used by {govuk_checkboxes_fields} and {govuk_checkboxes_fields_with_form} to generate the checkboxes HTML
87
+ # Wrapper method used by {govuk_checkbox_field_tag} and {govuk_checkbox_field_form} to generate the checkboxes HTML
98
88
  #
99
89
  # @param items [Array] array of checkbox items.
100
90
  # Each item is a hash which can be:
@@ -105,18 +95,18 @@ module CrownMarketplaceUtils
105
95
  # @option govuk_checkboxes_options [String] :classes additional CSS classes for the checkboxes HTML
106
96
  # @option govuk_checkboxes_options [Hash] :attributes ({ module: 'govuk-checkboxes' }) any additional attributes that will added as part of the HTML
107
97
  #
108
- # @yield the checkbox item HTML generated by {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
98
+ # @yield the checkbox item HTML generated by {govuk_checkbox_field_tag} or {govuk_checkbox_field_form}
109
99
  #
110
100
  # @yieldparam checkbox_item [Hash] the current checkbox item to be rendered
111
101
  #
112
- # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
102
+ # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkbox_field_tag} or {govuk_checkbox_field_form}
113
103
 
114
104
  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' })
105
+ govuk_checkboxes_options[:attributes] ||= {}
106
+ govuk_checkboxes_options[:attributes][:class] = "govuk-checkboxes #{govuk_checkboxes_options[:classes]}".rstrip
107
+ (govuk_checkboxes_options[:attributes][:data] ||= {}).merge!({ module: 'govuk-checkboxes' })
118
108
 
119
- tag.div(class: govuk_checkboxes_classes, **govuk_checkboxes_options[:attributes]) do
109
+ tag.div(**govuk_checkboxes_options[:attributes]) do
120
110
  capture do
121
111
  items.each do |checkbox_item|
122
112
  concat(
@@ -133,7 +123,7 @@ module CrownMarketplaceUtils
133
123
  end
134
124
  end
135
125
 
136
- # Generates the HTML for a checkbox button for {govuk_checkboxes_fields_with_form}
126
+ # Generates the HTML for a checkbox button for {govuk_checkbox_field_form}
137
127
  #
138
128
  # @param (see _govuk_checkbox_item)
139
129
  #
@@ -141,18 +131,16 @@ module CrownMarketplaceUtils
141
131
  #
142
132
  # @return (see _govuk_checkbox_item)
143
133
 
144
- def govuk_checkbox_item(attribute, checkbox_item)
134
+ def govuk_checkbox_item_tag(attribute, checkbox_item)
145
135
  _govuk_checkbox_item(attribute, checkbox_item) do
146
136
  checkbox_item[:attributes][:id] ||= "#{sanitize_to_id(attribute)}_#{sanitize_to_id(checkbox_item[:value])}"
147
137
 
148
138
  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]))
139
+ concat(govuk_label(checkbox_item[:attributes][:id], checkbox_item[:label][:text], **checkbox_item[:label]))
150
140
  end
151
141
  end
152
142
 
153
- # rubocop:disable Metrics/AbcSize
154
-
155
- # Generates the HTML for a checkbox button for {govuk_checkboxes_fields}
143
+ # Generates the HTML for a checkbox button for {govuk_checkbox_field_tag}
156
144
  #
157
145
  # @param (see _govuk_checkbox_item)
158
146
  # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
@@ -161,48 +149,54 @@ module CrownMarketplaceUtils
161
149
  #
162
150
  # @return (see _govuk_checkbox_item)
163
151
 
164
- def govuk_checkbox_item_with_form(form, attribute, checkbox_item)
152
+ def govuk_checkbox_item_form(form, attribute, checkbox_item)
165
153
  _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]
154
+ (checkbox_item[:label][:attributes] ||= {})[:value] = checkbox_item[:value]
155
+ checkbox_item[:label][:attributes][:for] = checkbox_item[:attributes][:id] if checkbox_item[:attributes][:id]
168
156
 
169
157
  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]))
158
+ concat(govuk_label(attribute, checkbox_item[:label][:text], form: form, **checkbox_item[:label]))
171
159
  end
172
160
  end
173
161
 
174
- # Wrapper method used by {govuk_checkbox_item} and {govuk_checkbox_item_with_form} to generate the checkboxes HTML
162
+ # rubocop:disable Metrics/AbcSize
163
+
164
+ # Wrapper method used by {govuk_checkbox_item_tag} and {govuk_checkbox_item_form} to generate the checkboxes HTML
175
165
  # including the label and hint, if there is one.
176
166
  #
177
167
  # @param attribute [String, Symbol] the attribute of the raido buttons
178
168
  # @param checkbox_item [Hash] the options for the checkbox item
179
169
  #
180
170
  # @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}
171
+ # @option checkbox_item [Hash] :label the parameters that will be used to create the label for the checkbox button, see {govuk_label}
172
+ # @option checkbox_item [Hash] :hint (nil) the parameters that will be used to create the hint for the checkbox button, see {govuk_hint}.
173
+ # If no hint is given then no hint will be rendered
174
+ # @option checkbox_item [Hash] :conditional (nil) content that will appear when the checkbox input 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
187
179
  # @option checkbox_item [Hash] :attributes ({}) any additional attributes that will be added as part of the checkbox button HTML
188
180
  #
189
181
  # @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
182
+ # which is used in {govuk_checkbox_item_tag} and {govuk_checkbox_item_form}
193
183
 
194
184
  def _govuk_checkbox_item(attribute, checkbox_item)
195
185
  checkbox_item[:attributes] ||= {}
196
- checkbox_item[:label][:options] ||= {}
197
- checkbox_item[:label][:options][:classes] = "govuk-checkboxes__label #{checkbox_item[:label][:options][:classes]}".rstrip
186
+ checkbox_item[:label] ||= {}
187
+ checkbox_item[:label][:classes] = "govuk-checkboxes__label #{checkbox_item[:label][:classes]}".rstrip
198
188
 
199
- set_item_options_for_hint('checkboxes', attribute, checkbox_item) if checkbox_item[:hint]
189
+ set_item_options_for_hint('checkboxes', attribute, checkbox_item)
190
+ set_conditional_item_options('checkboxes', attribute, checkbox_item)
200
191
 
201
192
  capture do
202
193
  yield
203
- concat(govuk_hint(checkbox_item[:hint][:text], **checkbox_item[:hint][:options])) if checkbox_item[:hint]
194
+ concat(govuk_hint(checkbox_item[:hint][:text], **checkbox_item[:hint])) if checkbox_item[:hint]
195
+ concat(tag.div(checkbox_item[:conditional][:content], class: checkbox_item[:conditional][:attributes][:class], id: checkbox_item[:conditional][:attributes][:id])) if checkbox_item[:conditional]
204
196
  end
205
197
  end
198
+
199
+ # rubocop:enable Metrics/AbcSize
206
200
  end
207
201
  end
208
202
  end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+ require_relative 'input'
5
+
6
+ module CrownMarketplaceUtils::Helpers
7
+ module GovUkHelper
8
+ module Field
9
+ # = GOV.UK Date Input
10
+ #
11
+ # This helper is used for generating the date input component from the
12
+ # {https://design-system.service.gov.uk/components/date-input GDS - Components - Date Input}
13
+ #
14
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
15
+
16
+ module DateInput
17
+ include Field
18
+ include Input
19
+
20
+ # Generates the HTML for the GOV.UK date input component
21
+ #
22
+ # @param attribute [String, Symbol] the attribute of the date input
23
+ # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
24
+ #
25
+ # @option govuk_date_input_options [String] :error_message (nil) the error message to be displayed
26
+ # @option govuk_date_input_options [ActiveModel] :model (nil) optional model that can be used to find an error message
27
+ # @option govuk_date_input_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
28
+ # the date tag and find the error message
29
+ # @option govuk_date_input_options [Hash] :form_group see {govuk_fields}
30
+ # @option govuk_date_input_options [Hash] :fieldset see {govuk_fields}
31
+ # @option govuk_date_input_options [Hash] :hint see {govuk_field}
32
+ # @option govuk_date_input_options [Hash] :date_input_options ({}) the options that will be used when rendering the date input.
33
+ # See {_govuk_date_input_fields} for more details.
34
+ #
35
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Date Input
36
+ # which can then be rendered on the page
37
+
38
+ def govuk_date_input(attribute, **govuk_date_input_options)
39
+ set_govuk_date_input_fieldset_options(**govuk_date_input_options)
40
+
41
+ govuk_fields(:date_input, attribute, **govuk_date_input_options) do |govuk_field_options, error_message|
42
+ concat(_govuk_date_input_fields(attribute, error_message, **govuk_field_options) do |date_item_attribute, date_item_input_options|
43
+ govuk_input(
44
+ date_item_attribute,
45
+ model: govuk_date_input_options[:model],
46
+ form: govuk_date_input_options[:form],
47
+ **date_item_input_options
48
+ )
49
+ end)
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ # Sets the role for the fieldset to group
56
+ #
57
+ # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
58
+
59
+ def set_govuk_date_input_fieldset_options(**govuk_date_input_options)
60
+ (govuk_date_input_options[:fieldset][:attributes] ||= {})[:role] = 'group'
61
+ end
62
+
63
+ # Wrapper method used by {govuk_date_input} to generate the date inputs HTML
64
+ #
65
+ # @param attribute [String, Symbol] the attribute of the date input
66
+ # @param any_errors [Boolean] flag to indicate if the inputs need the error class
67
+ # @param govuk_date_input_options [Hash] options that will be used in customising the HTML
68
+ #
69
+ # @option govuk_date_input_options [String] :classes additional CSS classes for the date inputs HTML
70
+ # @option govuk_date_input_options[Array] :date_items an array of the date items that will be rendered.
71
+ # Each date item hash must contain the +:name+ to add as suffix to attribute.
72
+ # Anything else in the hash will be used as options in a {govuk_input}.
73
+ # @option govuk_date_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
74
+ #
75
+ # @yield the date input item HTML generated by the parent method
76
+ #
77
+ # @yieldparam date_item_attribute [String] the attribute for individual date input
78
+ # @yieldparam date_item_input_options [Hash] the HTML options used for the date input item
79
+ #
80
+ # @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in {govuk_date_input}
81
+
82
+ def _govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options)
83
+ govuk_date_input_options[:attributes] ||= {}
84
+ govuk_date_input_options[:attributes][:class] = "govuk-date-input #{govuk_date_input_options[:classes]}".rstrip
85
+ date_items = govuk_date_input_options[:date_items] || govuk_default_date_items
86
+
87
+ tag.div(**govuk_date_input_options[:attributes]) do
88
+ capture do
89
+ enumerate_and_set_date_input_options(date_items, any_errors) do |date_item_input_options|
90
+ concat(tag.div(class: 'govuk-date-input__item') do
91
+ concat(yield("#{attribute}_#{date_item_input_options[:name]}", date_item_input_options))
92
+ end)
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ # rubocop:disable Metrics/CyclomaticComplexity
99
+
100
+ # Enumerates and inititalises the attributes of each date input item and is used by {_govuk_date_input_fields}
101
+ #
102
+ # @param date_items [Array] an array of the date item hashes that will be inititalised
103
+ # @param any_errors [Boolean] flag to indicate if the inputs need the error class
104
+ #
105
+ # @yield the date input item HTML generated by teach date input item
106
+ #
107
+ # @yieldparam date_item_input_options [Hash] the HTML options used for the date input item
108
+
109
+ def enumerate_and_set_date_input_options(date_items, any_errors)
110
+ date_items.each do |date_item_input_options|
111
+ (date_item_input_options[:input] ||= {})[:attributes] ||= {}
112
+
113
+ date_item_input_options[:input][:classes] = "govuk-date-input__input #{date_item_input_options[:input][:classes]} #{'govuk-input--error' if any_errors}".rstrip
114
+
115
+ date_item_input_options[:input][:attributes][:inputmode] ||= 'numeric'
116
+
117
+ date_item_input_options[:label] ||= {}
118
+ date_item_input_options[:label][:text] ||= date_item_input_options[:name].capitalize
119
+ date_item_input_options[:label][:classes] = 'govuk-date-input__label'
120
+
121
+ yield(date_item_input_options)
122
+ end
123
+ end
124
+ # rubocop:enable Metrics/CyclomaticComplexity
125
+
126
+ # The default date items used in {_govuk_date_input_fields} if no date items are provided
127
+
128
+ def govuk_default_date_items
129
+ [
130
+ {
131
+ name: 'day',
132
+ input: {
133
+ classes: 'govuk-input--width-2'
134
+ }
135
+ },
136
+ {
137
+ name: 'month',
138
+ input: {
139
+ classes: 'govuk-input--width-2'
140
+ }
141
+ },
142
+ {
143
+ name: 'year',
144
+ input: {
145
+ classes: 'govuk-input--width-4'
146
+ }
147
+ }
148
+ ]
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CrownMarketplaceUtils::Helpers
6
+ module GovUkHelper
7
+ module Field
8
+ # = GOV.UK File Upload
9
+ #
10
+ # This helper is used for generating the file upload component from the
11
+ # {https://design-system.service.gov.uk/components/file-upload GDS - Components - File Upload}
12
+ #
13
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
14
+
15
+ module FileUpload
16
+ include Field
17
+
18
+ # Generates the HTML for the GOV.UK file upload component
19
+ #
20
+ # @param attribute [String, Symbol] the attribute of the file upload
21
+ # @param govuk_file_upload_options [Hash] options that will be used for the parts of the form group, label, hint and file upload
22
+ #
23
+ # @option govuk_file_upload_options [String] :error_message (nil) the error message to be displayed
24
+ # @option govuk_file_upload_options [ActiveModel] :model (nil) optional model that can be used to find an error message
25
+ # @option govuk_file_upload_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
26
+ # the file upload input and find the error message
27
+ # @option govuk_file_upload_options [Hash] :form_group see {govuk_field}
28
+ # @option govuk_file_upload_options [Hash] :label see {govuk_field}
29
+ # @option govuk_file_upload_options [Hash] :hint see {govuk_field}
30
+ # @option govuk_file_upload_options [Hash] :file_upload ({}) the options that will be used when rendering the file upload.
31
+ # See {set_file_upload_options} for more details.
32
+ #
33
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK File Upload
34
+ # which can then be rendered on the page
35
+
36
+ def govuk_file_upload(attribute, **govuk_file_upload_options)
37
+ govuk_field(:file_upload, attribute, **govuk_file_upload_options) do |govuk_field_options, error_message|
38
+ set_file_upload_options(error_message, govuk_field_options)
39
+
40
+ concat(
41
+ if govuk_file_upload_options[:form]
42
+ govuk_file_upload_form(govuk_file_upload_options[:form], attribute, **govuk_field_options)
43
+ else
44
+ govuk_file_upload_tag(attribute, **govuk_field_options)
45
+ end
46
+ )
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ # Generates the file upload HTML for {govuk_file_upload}
53
+ #
54
+ # @param attribute [String, Symbol] the attribute of the file upload
55
+ # @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
56
+ #
57
+ # @option (see set_file_upload_options)
58
+ #
59
+ # @return [ActiveSupport::SafeBuffer] the HTML for the file upload field which is used in {govuk_file_upload}
60
+
61
+ def govuk_file_upload_tag(attribute, **govuk_file_upload_options)
62
+ file_field_tag(attribute, **govuk_file_upload_options[:attributes])
63
+ end
64
+
65
+ # Generates the file upload HTML for {govuk_file_upload} when there is a ActionView::Helpers::FormBuilder
66
+ #
67
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the file upload input
68
+ # @param (see govuk_file_upload_tag)
69
+ #
70
+ # @option (see set_file_upload_options)
71
+ #
72
+ # @return (see govuk_file_upload_tag)
73
+
74
+ def govuk_file_upload_form(form, attribute, **govuk_file_upload_options)
75
+ form.file_field(attribute, **govuk_file_upload_options[:attributes])
76
+ end
77
+
78
+ # Initialises the attributes for the file upload input
79
+ #
80
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
81
+ # @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
82
+ #
83
+ # @option govuk_file_upload_options [String] :classes additional CSS classes for the file iupload HTML
84
+ # @option govuk_file_upload_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
85
+
86
+ def set_file_upload_options(error_message, govuk_file_upload_options)
87
+ govuk_file_upload_options[:attributes] ||= {}
88
+ govuk_file_upload_options[:attributes][:class] = "govuk-file-upload #{govuk_file_upload_options[:classes]}".rstrip
89
+ govuk_file_upload_options[:attributes][:class] << ' govuk-file-upload--error' if error_message
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CrownMarketplaceUtils::Helpers
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 govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
22
+ #
23
+ # @option govuk_input_options [String] :error_message (nil) the error message to be displayed
24
+ # @option govuk_input_options [ActiveModel] :model (nil) optional model that can be used to find an error message
25
+ # @option govuk_label_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
26
+ # the input tag and find the error message
27
+ # @option govuk_input_options [Hash] :form_group see {govuk_field}
28
+ # @option govuk_input_options [Hash] :label see {govuk_field}
29
+ # @option govuk_input_options [Hash] :hint see {govuk_field}
30
+ # @option govuk_input_options [Hash] :input ({}) the options that will be used when rendering the input.
31
+ # See {_govuk_input_field} for more details.
32
+ #
33
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Input
34
+ # which can then be rendered on the page
35
+
36
+ def govuk_input(attribute, **govuk_input_options)
37
+ govuk_field(:input, attribute, **govuk_input_options) do |govuk_field_options, error_message|
38
+ concat(_govuk_input_field(error_message, **govuk_field_options) do
39
+ field_type = get_field_type(govuk_field_options[:field_type])
40
+ govuk_field_options[:value] = govuk_input_options[:model].send(attribute) if govuk_input_options[:model]
41
+
42
+ if govuk_input_options[:form]
43
+ govuk_input_form(govuk_input_options[:form], field_type, attribute, **govuk_field_options)
44
+ else
45
+ govuk_input_tag(field_type, attribute, **govuk_field_options)
46
+ end
47
+ end)
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ # Generates the input HTML for {govuk_input}
54
+ #
55
+ # @param field_type [String] the inout field type
56
+ # @param attribute [String, Symbol] the attribute of the input
57
+ # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
58
+ #
59
+ # @option (see _govuk_input_field)
60
+ #
61
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input}
62
+
63
+ def govuk_input_tag(field_type, attribute, **govuk_text_input_options)
64
+ send("#{field_type}_tag", attribute, govuk_text_input_options[:value], **govuk_text_input_options[:attributes])
65
+ end
66
+
67
+ # Generates the input HTML for {govuk_input} when there is a ActionView::Helpers::FormBuilder
68
+ #
69
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the input tag
70
+ # @param (see govuk_input_tag)
71
+ #
72
+ # @option (see _govuk_input_field)
73
+ #
74
+ # @return (see govuk_input_tag)
75
+
76
+ def govuk_input_form(form, field_type, attribute, **govuk_text_input_options)
77
+ form.send(field_type, attribute, **govuk_text_input_options[:attributes])
78
+ end
79
+
80
+ # Wrapper method used by {govuk_input} to generate the Input HTML
81
+ #
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 govuk_text_input_options [String] :classes additional CSS classes for the input HTML
86
+ # @option govuk_text_input_options [String] :value (nil) the value of the input
87
+ # @option govuk_text_input_options [Symbol] :field_type the type of the input field, see {get_field_type} for options
88
+ # @option govuk_text_input_options [Hash] :prefix (nil) optional prefix for the input field. See {govuk_fix} for more details.
89
+ # @option govuk_text_input_options [Hash] :suffix (nil) optional suffix for the input field. See {govuk_fix} for more details.
90
+ # @option govuk_text_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
91
+ #
92
+ # @yield the input HTML generated by {govuk_input_tag} or {govuk_input_form}
93
+ #
94
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used by {govuk_input}
95
+
96
+ def _govuk_input_field(error_message, **govuk_text_input_options)
97
+ govuk_text_input_options[:attributes] ||= {}
98
+ govuk_text_input_options[:attributes][:class] = "govuk-input #{govuk_text_input_options[:classes]}".rstrip
99
+ govuk_text_input_options[:attributes][:class] << ' govuk-input--error' if error_message
100
+
101
+ input_html = yield
102
+
103
+ prefix = govuk_text_input_options[:prefix]
104
+ suffix = govuk_text_input_options[:suffix]
105
+
106
+ if prefix || suffix
107
+ tag.div(class: 'govuk-input__wrapper') do
108
+ capture do
109
+ concat(govuk_fix('pre', prefix)) if prefix
110
+ concat(input_html)
111
+ concat(govuk_fix('suf', suffix)) if suffix
112
+ end
113
+ end
114
+ else
115
+ input_html
116
+ end
117
+ end
118
+
119
+ # Generates the prefix and suffix HTML for {_govuk_input_field}
120
+ #
121
+ # @param fix [String] either +"pre"+ or +"suf"+
122
+ # @param govuk_fix_options [Hash] options that will be used in customising the HTML
123
+ #
124
+ # @option govuk_fix_options [String] :classes additional CSS classes for the input HTML
125
+ # @option govuk_text_input_options [Hash] :attributes ({ aria: { hidden: true } }) any additional attributes that will added as part of the HTML
126
+ #
127
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {_govuk_input_field}
128
+
129
+ def govuk_fix(fix, govuk_fix_options)
130
+ govuk_fix_classes = ["govuk-input__#{fix}fix"]
131
+ govuk_fix_classes << govuk_fix_options[:classes]
132
+ govuk_fix_options[:attributes] ||= {}
133
+ (govuk_fix_options[:attributes][:aria] ||= {}).merge!({ hidden: true })
134
+
135
+ tag.div(govuk_fix_options[:text], class: govuk_fix_classes, **govuk_fix_options[:attributes])
136
+ end
137
+
138
+ # Returns the field type used to create the rails input tag
139
+ #
140
+ # @param field_type [Symbol] the type of the field, defaults to text
141
+ # Allowed values are:
142
+ # - +:email+
143
+ # - +:password+
144
+
145
+ def get_field_type(field_type)
146
+ case field_type
147
+ when :email, :password
148
+ :"#{field_type}_field"
149
+ else
150
+ :text_field
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end