crown_marketplace_utils 0.1.0.beta.2 → 0.1.0.beta.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +7 -0
- data/Gemfile +0 -4
- data/Gemfile.lock +44 -29
- data/README.md +37 -13
- data/crown_marketplace_utils.gemspec +9 -8
- data/lib/crown_marketplace_utils/gov_uk_helper/button.rb +2 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/details.rb +1 -1
- data/lib/crown_marketplace_utils/gov_uk_helper/error_message.rb +1 -1
- data/lib/crown_marketplace_utils/gov_uk_helper/field/character_count.rb +190 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/checkboxes.rb +219 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/date_input.rb +234 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/file_upload.rb +125 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/input.rb +194 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/radios.rb +219 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/select.rb +170 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/textarea.rb +140 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field.rb +305 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/fieldset.rb +75 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/form_group.rb +0 -3
- data/lib/crown_marketplace_utils/gov_uk_helper/header.rb +172 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/label.rb +97 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/notification_banner.rb +139 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/pagination.rb +214 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/step_by_step_navigation.rb +225 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/tag.rb +39 -0
- data/lib/crown_marketplace_utils/gov_uk_helper.rb +32 -0
- data/lib/crown_marketplace_utils/version.rb +1 -1
- metadata +60 -30
@@ -0,0 +1,219 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module GovUkHelper
|
7
|
+
module Field
|
8
|
+
# = GOV.UK Checkboxes
|
9
|
+
#
|
10
|
+
# This helper is used for generating the checkboxes component from the
|
11
|
+
# {https://design-system.service.gov.uk/components/checkboxes GDS - Components - Checkboxes}
|
12
|
+
#
|
13
|
+
# This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
|
14
|
+
|
15
|
+
module Checkboxes
|
16
|
+
include Field
|
17
|
+
|
18
|
+
# Generates the HTML for the GOV.UK Checkboxes component
|
19
|
+
#
|
20
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
21
|
+
# @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
|
22
|
+
# @param error_message [String] the error message to be displayed
|
23
|
+
# @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
|
24
|
+
#
|
25
|
+
# @option govuk_checkboxes_options [Hash] :form_group see {govuk_fields}
|
26
|
+
# @option govuk_checkboxes_options [Hash] :fieldset see {govuk_fields}
|
27
|
+
# @option govuk_checkboxes_options [Hash] :hint see {govuk_field}
|
28
|
+
# @option govuk_checkboxes_options [Hash] :checkboxes ({}) the options that will be used when rendering the checkbox buttons.
|
29
|
+
# See {govuk_checkboxes_fields} for more details.
|
30
|
+
#
|
31
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Checkboxes
|
32
|
+
# which can then be rendered on the page
|
33
|
+
|
34
|
+
def govuk_checkboxes(attribute, items, error_message = nil, **govuk_checkboxes_options)
|
35
|
+
govuk_fields(:checkboxes, attribute, error_message, **govuk_checkboxes_options) do |govuk_field_options|
|
36
|
+
concat(govuk_checkboxes_fields(attribute, items, **govuk_field_options))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Generates the HTML for the GOV.UK Checkboxes component using an ActiveModel.
|
41
|
+
# Unlike {govuk_checkboxes}, the method will be able to automatically determine if the error message needs to be shown.
|
42
|
+
#
|
43
|
+
# @param model [ActiveModel] model that will be used to find an error message and the checked checkboxes
|
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 (see govuk_checkboxes)
|
49
|
+
#
|
50
|
+
# @return (see govuk_checkboxes)
|
51
|
+
|
52
|
+
def govuk_checkboxes_with_model(model, attribute, items, **govuk_checkboxes_options)
|
53
|
+
values = model.send(attribute) || []
|
54
|
+
items.each { |item| item[:checked] = values.include?(item[:value]) }
|
55
|
+
|
56
|
+
govuk_fields_with_model(:checkboxes, model, attribute, **govuk_checkboxes_options) do |govuk_field_options|
|
57
|
+
concat(govuk_checkboxes_fields(attribute, items, **govuk_field_options))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Generates the HTML for the GOV.UK Checkboxes component using an ActionView::Helpers::FormBuilder.
|
62
|
+
# Unlike {govuk_checkboxes}, the method will be able to automatically determine if the error message needs to be shown.
|
63
|
+
#
|
64
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
|
65
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
66
|
+
# @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
|
67
|
+
# @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
|
68
|
+
#
|
69
|
+
# @option (see govuk_checkboxes)
|
70
|
+
#
|
71
|
+
# @return (see govuk_checkboxes)
|
72
|
+
|
73
|
+
def govuk_checkboxes_with_form(form, attribute, items, **govuk_checkboxes_options)
|
74
|
+
govuk_fields_with_form(:checkboxes, form, attribute, **govuk_checkboxes_options) do |govuk_field_options|
|
75
|
+
concat(govuk_checkboxes_fields_with_form(form, attribute, items, **govuk_field_options))
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
# Generates the checkboxes HTML for {govuk_checkboxes}
|
82
|
+
#
|
83
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
84
|
+
# @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
|
85
|
+
# @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
|
86
|
+
#
|
87
|
+
# @option (see _govuk_checkboxes_fields)
|
88
|
+
#
|
89
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes}
|
90
|
+
|
91
|
+
def govuk_checkboxes_fields(attribute, items, **govuk_checkboxes_options)
|
92
|
+
_govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
|
93
|
+
govuk_checkbox_item(attribute, checkbox_item)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Generates the checkboxes HTML for {govuk_checkboxes_with_form}
|
98
|
+
#
|
99
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
|
100
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
101
|
+
# @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
|
102
|
+
# @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
|
103
|
+
#
|
104
|
+
# @option (see _govuk_checkboxes_fields)
|
105
|
+
#
|
106
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_with_form}
|
107
|
+
|
108
|
+
def govuk_checkboxes_fields_with_form(form, attribute, items, **govuk_checkboxes_options)
|
109
|
+
_govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
|
110
|
+
govuk_checkbox_item_with_form(form, attribute, checkbox_item)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Wrapper method used by {govuk_checkboxes_fields} and {govuk_checkboxes_fields_with_form} to generate the checkboxes HTML
|
115
|
+
#
|
116
|
+
# @param items [Array] array of checkbox items.
|
117
|
+
# Each item is a hash which can be:
|
118
|
+
# - +:divider+ text to separate checkbox items
|
119
|
+
# - checkbox item, see {_govuk_checkbox_item}
|
120
|
+
# @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
|
121
|
+
#
|
122
|
+
# @option govuk_checkboxes_options [String] :classes additional CSS classes for the checkboxes HTML
|
123
|
+
# @option govuk_checkboxes_options [Hash] :attributes ({ module: 'govuk-checkboxes' }) any additional attributes that will added as part of the HTML
|
124
|
+
#
|
125
|
+
# @yield the checkbox item HTML generated by {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
|
126
|
+
#
|
127
|
+
# @yieldparam checkbox_item [Hash] the current checkbox item to be rendered
|
128
|
+
#
|
129
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
|
130
|
+
|
131
|
+
def _govuk_checkboxes_fields(items, **govuk_checkboxes_options)
|
132
|
+
govuk_checkboxes_classes = ['govuk-checkboxes']
|
133
|
+
govuk_checkboxes_classes << govuk_checkboxes_options[:classes]
|
134
|
+
((govuk_checkboxes_options[:attributes] ||= {})[:data] ||= {}).merge!({ module: 'govuk-checkboxes' })
|
135
|
+
|
136
|
+
tag.div(class: govuk_checkboxes_classes, **govuk_checkboxes_options[:attributes]) do
|
137
|
+
capture do
|
138
|
+
items.each do |checkbox_item|
|
139
|
+
concat(
|
140
|
+
if checkbox_item[:divider]
|
141
|
+
tag.div(checkbox_item[:divider], class: 'govuk-checkboxes__divider')
|
142
|
+
else
|
143
|
+
tag.div(class: 'govuk-checkboxes__item') do
|
144
|
+
yield(checkbox_item)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# Generates the HTML for a checkbox button for {govuk_checkboxes_fields_with_form}
|
154
|
+
#
|
155
|
+
# @param (see _govuk_checkbox_item)
|
156
|
+
#
|
157
|
+
# @option (see _govuk_checkbox_item)
|
158
|
+
#
|
159
|
+
# @return (see _govuk_checkbox_item)
|
160
|
+
|
161
|
+
def govuk_checkbox_item(attribute, checkbox_item)
|
162
|
+
_govuk_checkbox_item(attribute, checkbox_item) do
|
163
|
+
checkbox_item[:attributes][:id] ||= "#{sanitize_to_id(attribute)}_#{sanitize_to_id(checkbox_item[:value])}"
|
164
|
+
|
165
|
+
concat(check_box_tag("#{attribute}[]", checkbox_item[:value], checkbox_item[:checked], class: 'govuk-checkboxes__input', **checkbox_item[:attributes]))
|
166
|
+
concat(govuk_label(checkbox_item[:attributes][:id], checkbox_item[:label][:text], **checkbox_item[:label]))
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# Generates the HTML for a checkbox button for {govuk_checkboxes_fields}
|
171
|
+
#
|
172
|
+
# @param (see _govuk_checkbox_item)
|
173
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
|
174
|
+
#
|
175
|
+
# @option (see _govuk_checkbox_item)
|
176
|
+
#
|
177
|
+
# @return (see _govuk_checkbox_item)
|
178
|
+
|
179
|
+
def govuk_checkbox_item_with_form(form, attribute, checkbox_item)
|
180
|
+
_govuk_checkbox_item(attribute, checkbox_item) do
|
181
|
+
(checkbox_item[:label][:attributes] ||= {})[:value] = checkbox_item[:value]
|
182
|
+
checkbox_item[:label][:attributes][:for] = checkbox_item[:attributes][:id] if checkbox_item[:attributes][:id]
|
183
|
+
|
184
|
+
concat(form.check_box(attribute, checkbox_item[:attributes].merge({ class: 'govuk-checkboxes__input', multiple: true, include_hidden: false }), checkbox_item[:value]))
|
185
|
+
concat(govuk_label_with_form(form, attribute, checkbox_item[:label][:text], **checkbox_item[:label]))
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
# Wrapper method used by {govuk_checkbox_item} and {govuk_checkbox_item_with_form} to generate the checkboxes HTML
|
190
|
+
# including the label and hint, if there is one.
|
191
|
+
#
|
192
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
193
|
+
# @param checkbox_item [Hash] the options for the checkbox item
|
194
|
+
#
|
195
|
+
# @option checkbox_item [String] :classes additional CSS classes for the checkbox button HTML
|
196
|
+
# @option checkbox_item [Hash] :label the parameters that will be used to create the label for the checkbox button, see {govuk_label}
|
197
|
+
# @option checkbox_item [Hash] :hint (nil) the parameters that will be used to create the hint for the checkbox button, see {govuk_hint}.
|
198
|
+
# If no hint is given then no hint will be rendered
|
199
|
+
# @option checkbox_item [Hash] :attributes ({}) any additional attributes that will be added as part of the checkbox button HTML
|
200
|
+
#
|
201
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons, label and hint
|
202
|
+
# which is used in {govuk_checkbox_item} and {govuk_checkbox_item_with_form}
|
203
|
+
|
204
|
+
def _govuk_checkbox_item(attribute, checkbox_item)
|
205
|
+
checkbox_item[:attributes] ||= {}
|
206
|
+
checkbox_item[:label] ||= {}
|
207
|
+
checkbox_item[:label][:classes] = "govuk-checkboxes__label #{checkbox_item[:label][:classes]}".rstrip
|
208
|
+
|
209
|
+
set_item_options_for_hint('checkboxes', attribute, checkbox_item) if checkbox_item[:hint]
|
210
|
+
|
211
|
+
capture do
|
212
|
+
yield
|
213
|
+
concat(govuk_hint(checkbox_item[:hint][:text], **checkbox_item[:hint])) if checkbox_item[:hint]
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
@@ -0,0 +1,234 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
require_relative 'input'
|
5
|
+
|
6
|
+
module CrownMarketplaceUtils
|
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 error_message [String] the error message to be displayed
|
24
|
+
# @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
|
25
|
+
#
|
26
|
+
# @option govuk_date_input_options [Hash] :form_group see {govuk_fields}
|
27
|
+
# @option govuk_date_input_options [Hash] :fieldset see {govuk_fields}
|
28
|
+
# @option govuk_date_input_options [Hash] :hint see {govuk_field}
|
29
|
+
# @option govuk_date_input_options [Hash] :date_input_options ({}) the options that will be used when rendering the date input.
|
30
|
+
# See {govuk_date_input_fields} for more details.
|
31
|
+
#
|
32
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Date Input
|
33
|
+
# which can then be rendered on the page
|
34
|
+
|
35
|
+
def govuk_date_input(attribute, error_message = nil, **govuk_date_input_options)
|
36
|
+
set_govuk_date_input_fieldset_options(**govuk_date_input_options)
|
37
|
+
|
38
|
+
govuk_fields(:date_input, attribute, error_message, **govuk_date_input_options) do |govuk_field_options|
|
39
|
+
concat(govuk_date_input_fields(attribute, error_message, **govuk_field_options))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Generates the HTML for the GOV.UK date input component using an ActiveModel.
|
44
|
+
# Unlike {govuk_date_input_with_model}, the method will be able to automatically determine if the error message needs to be shown.
|
45
|
+
#
|
46
|
+
# @param model [ActiveModel] model that will be used to find an error message and the value of the date inputs
|
47
|
+
# @param attribute [String, Symbol] the attribute of the date input
|
48
|
+
# @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
|
49
|
+
#
|
50
|
+
# @option (see govuk_date_input)
|
51
|
+
#
|
52
|
+
# @return (see govuk_date_input)
|
53
|
+
|
54
|
+
def govuk_date_input_with_model(model, attribute, **govuk_date_input_options)
|
55
|
+
set_govuk_date_input_fieldset_options(**govuk_date_input_options)
|
56
|
+
|
57
|
+
govuk_fields_with_model(:date_input, model, attribute, **govuk_date_input_options) do |govuk_field_options, any_errors|
|
58
|
+
concat(govuk_date_input_fields_with_model(model, attribute, any_errors, **govuk_field_options))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Generates the HTML for the GOV.UK date input component using an ActionView::Helpers::FormBuilder.
|
63
|
+
# Unlike {govuk_date_input_with_model}, the method will be able to automatically determine if the error message needs to be shown.
|
64
|
+
#
|
65
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the date inputs
|
66
|
+
# @param attribute [String, Symbol] the attribute of the date input
|
67
|
+
# @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
|
68
|
+
#
|
69
|
+
# @option (see govuk_date_input)
|
70
|
+
#
|
71
|
+
# @return (see govuk_date_input)
|
72
|
+
|
73
|
+
def govuk_date_input_with_form(form, attribute, **govuk_date_input_options)
|
74
|
+
set_govuk_date_input_fieldset_options(**govuk_date_input_options)
|
75
|
+
|
76
|
+
govuk_fields_with_form(:date_input, form, attribute, **govuk_date_input_options) do |govuk_field_options, any_errors|
|
77
|
+
concat(govuk_date_input_fields_with_form(form, attribute, any_errors, **govuk_field_options))
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
# Generates the date inputs HTML for {govuk_date_input}
|
84
|
+
#
|
85
|
+
# @param (see _govuk_date_input_fields)
|
86
|
+
#
|
87
|
+
# @option (see _govuk_date_input_fields)
|
88
|
+
#
|
89
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in {govuk_date_input}
|
90
|
+
|
91
|
+
def govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options)
|
92
|
+
_govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options) do |date_item_attribute, date_item_input_options|
|
93
|
+
govuk_input(
|
94
|
+
date_item_attribute,
|
95
|
+
**date_item_input_options
|
96
|
+
)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Generates the date inputs HTML for {govuk_date_input_with_model}
|
101
|
+
#
|
102
|
+
# @param model [ActiveModel] model that will be used to find an error message and the value of the date inputs
|
103
|
+
# @param (see _govuk_date_input_fields)
|
104
|
+
#
|
105
|
+
# @option (see _govuk_date_input_fields)
|
106
|
+
#
|
107
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in {govuk_date_input_with_model}
|
108
|
+
|
109
|
+
def govuk_date_input_fields_with_model(model, attribute, any_errors, **govuk_date_input_options)
|
110
|
+
_govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options) do |date_item_attribute, date_item_input_options|
|
111
|
+
govuk_input_with_model(
|
112
|
+
model,
|
113
|
+
date_item_attribute,
|
114
|
+
**date_item_input_options
|
115
|
+
)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Generates the date inputs HTML for {govuk_date_input_with_form}
|
120
|
+
#
|
121
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the date inputs
|
122
|
+
# @param (see _govuk_date_input_fields)
|
123
|
+
#
|
124
|
+
# @option (see _govuk_date_input_fields)
|
125
|
+
#
|
126
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in {govuk_date_input_with_form}
|
127
|
+
|
128
|
+
def govuk_date_input_fields_with_form(form, attribute, any_errors, **govuk_date_input_options)
|
129
|
+
_govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options) do |date_item_attribute, date_item_input_options|
|
130
|
+
govuk_input_with_form(
|
131
|
+
form,
|
132
|
+
date_item_attribute,
|
133
|
+
**date_item_input_options
|
134
|
+
)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Sets the role for the fieldset to group
|
139
|
+
#
|
140
|
+
# @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
|
141
|
+
|
142
|
+
def set_govuk_date_input_fieldset_options(**govuk_date_input_options)
|
143
|
+
(govuk_date_input_options[:fieldset][:attributes] ||= {})[:role] = 'group'
|
144
|
+
end
|
145
|
+
|
146
|
+
# Wrapper method used by {govuk_date_input_fields}, {govuk_date_input_fields_with_model} and {govuk_date_input_fields_with_form}
|
147
|
+
# to generate the date inputs HTML
|
148
|
+
#
|
149
|
+
# @param attribute [String, Symbol] the attribute of the date input
|
150
|
+
# @param any_errors [Boolean] flag to indicate if the inputs need the error class
|
151
|
+
# @param govuk_date_input_options [Hash] options that will be used in customising the HTML
|
152
|
+
#
|
153
|
+
# @option govuk_date_input_options [String] :classes additional CSS classes for the date inputs HTML
|
154
|
+
# @option govuk_date_input_options[Array] :date_items an array of the date items that will be rendered.
|
155
|
+
# Each date item hash must contain the +:name+ to add as suffix to attribute.
|
156
|
+
# Anything else in the hash will be used as options in a {govuk_input}.
|
157
|
+
# @option govuk_date_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
158
|
+
#
|
159
|
+
# @yield the date input item HTML generated by the parent method
|
160
|
+
#
|
161
|
+
# @yieldparam date_item_attribute [String] the attribute for individual date input
|
162
|
+
# @yieldparam date_item_input_options [Hash] the HTML options used for the date input item
|
163
|
+
#
|
164
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in
|
165
|
+
# {govuk_date_input_fields}, {govuk_date_input_fields_with_model} and {govuk_date_input_fields_with_form}
|
166
|
+
|
167
|
+
def _govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options)
|
168
|
+
govuk_date_input_classes = ['govuk-date-input']
|
169
|
+
govuk_date_input_classes << govuk_date_input_options[:classes]
|
170
|
+
date_items = govuk_date_input_options[:date_items] || [
|
171
|
+
{
|
172
|
+
name: 'day',
|
173
|
+
input: {
|
174
|
+
classes: 'govuk-input--width-2'
|
175
|
+
}
|
176
|
+
},
|
177
|
+
{
|
178
|
+
name: 'month',
|
179
|
+
input: {
|
180
|
+
classes: 'govuk-input--width-2'
|
181
|
+
}
|
182
|
+
},
|
183
|
+
{
|
184
|
+
name: 'year',
|
185
|
+
input: {
|
186
|
+
classes: 'govuk-input--width-4'
|
187
|
+
}
|
188
|
+
}
|
189
|
+
]
|
190
|
+
|
191
|
+
govuk_date_input_options[:attributes] ||= {}
|
192
|
+
|
193
|
+
tag.div(class: govuk_date_input_classes, **govuk_date_input_options[:attributes]) do
|
194
|
+
capture do
|
195
|
+
enumerate_and_set_date_input_options(date_items, any_errors) do |date_item_input_options|
|
196
|
+
concat(tag.div(class: 'govuk-date-input__item') do
|
197
|
+
concat(yield("#{attribute}_#{date_item_input_options[:name]}", date_item_input_options))
|
198
|
+
end)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
205
|
+
|
206
|
+
# Enumerates and inititalises the attributes of each date input item and is used by {_govuk_date_input_fields}
|
207
|
+
#
|
208
|
+
# @param date_items [Array] an array of the date item hashes that will be inititalised
|
209
|
+
# @param any_errors [Boolean] flag to indicate if the inputs need the error class
|
210
|
+
#
|
211
|
+
# @yield the date input item HTML generated by teach date input item
|
212
|
+
#
|
213
|
+
# @yieldparam date_item_input_options [Hash] the HTML options used for the date input item
|
214
|
+
|
215
|
+
def enumerate_and_set_date_input_options(date_items, any_errors)
|
216
|
+
date_items.each do |date_item_input_options|
|
217
|
+
(date_item_input_options[:input] ||= {})[:attributes] ||= {}
|
218
|
+
|
219
|
+
date_item_input_options[:input][:classes] = "govuk-date-input__input #{date_item_input_options[:input][:classes]} #{'govuk-input--error' if any_errors}".rstrip
|
220
|
+
|
221
|
+
date_item_input_options[:input][:attributes][:inputmode] ||= 'numeric'
|
222
|
+
|
223
|
+
date_item_input_options[:label] ||= {}
|
224
|
+
date_item_input_options[:label][:text] ||= date_item_input_options[:name].capitalize
|
225
|
+
date_item_input_options[:label][:classes] = 'govuk-date-input__label'
|
226
|
+
|
227
|
+
yield(date_item_input_options)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
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 error_message [String] the error message to be displayed
|
22
|
+
# @param govuk_file_upload_options [Hash] options that will be used for the parts of the form group, label, hint and file upload
|
23
|
+
#
|
24
|
+
# @option govuk_file_upload_options [Hash] :form_group see {govuk_field}
|
25
|
+
# @option govuk_file_upload_options [Hash] :label see {govuk_field}
|
26
|
+
# @option govuk_file_upload_options [Hash] :hint see {govuk_field}
|
27
|
+
# @option govuk_file_upload_options [Hash] :file_upload ({}) the options that will be used when rendering the file upload.
|
28
|
+
# See {govuk_file_upload_field} for more details.
|
29
|
+
#
|
30
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK File Upload
|
31
|
+
# which can then be rendered on the page
|
32
|
+
|
33
|
+
def govuk_file_upload(attribute, error_message = nil, **govuk_file_upload_options)
|
34
|
+
govuk_field(:file_upload, attribute, error_message, **govuk_file_upload_options) do |govuk_field_options|
|
35
|
+
concat(govuk_file_upload_field(attribute, error_message, **govuk_field_options))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Generates the HTML for the GOV.UK file upload component using an ActiveModel.
|
40
|
+
# Unlike {govuk_file_upload}, the method will be able to automatically determine if the error message needs to be shown.
|
41
|
+
#
|
42
|
+
# @param model [ActiveModel] model that will be used to find an error message for the file upload
|
43
|
+
# @param attribute [String, Symbol] the attribute of the file upload
|
44
|
+
# @param govuk_file_upload_options [Hash] options that will be used for the parts of the form group, label, hint and file upload
|
45
|
+
#
|
46
|
+
# @option (see govuk_file_upload)
|
47
|
+
#
|
48
|
+
# @return (see govuk_file_upload)
|
49
|
+
|
50
|
+
def govuk_file_upload_with_model(model, attribute, **govuk_file_upload_options)
|
51
|
+
govuk_field_with_model(:file_upload, model, attribute, **govuk_file_upload_options) do |govuk_field_options, error_message|
|
52
|
+
concat(govuk_file_upload_field(attribute, error_message, **govuk_field_options))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Generates the HTML for the GOV.UK file upload component using an ActionView::Helpers::FormBuilder.
|
57
|
+
# Unlike {govuk_file_upload}, the method will be able to automatically determine if the error message needs to be shown.
|
58
|
+
#
|
59
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the file upload input
|
60
|
+
# @param attribute [String, Symbol] the attribute of the file upload
|
61
|
+
# @param govuk_file_upload_options [Hash] options that will be used for the parts of the form group, label, hint and file upload
|
62
|
+
#
|
63
|
+
# @option (see govuk_file_upload)
|
64
|
+
#
|
65
|
+
# @return (see govuk_file_upload)
|
66
|
+
|
67
|
+
def govuk_file_upload_with_form(form, attribute, **govuk_file_upload_options)
|
68
|
+
govuk_field_with_form(:file_upload, form, attribute, **govuk_file_upload_options) do |govuk_field_options, error_message|
|
69
|
+
concat(govuk_file_upload_field_with_form(form, attribute, error_message, **govuk_field_options))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
# Generates the file upload HTML for {govuk_file_upload} and {govuk_file_upload_with_model}
|
76
|
+
#
|
77
|
+
# @param attribute [String, Symbol] the attribute of the file upload
|
78
|
+
# @param error_message [String] the error message to be displayed
|
79
|
+
# @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
|
80
|
+
#
|
81
|
+
# @option (see set_file_upload_options)
|
82
|
+
#
|
83
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the file upload field which is used in {govuk_file_upload} and {govuk_file_upload_with_model}
|
84
|
+
|
85
|
+
def govuk_file_upload_field(attribute, error_message, **govuk_file_upload_options)
|
86
|
+
set_file_upload_options(error_message, govuk_file_upload_options)
|
87
|
+
|
88
|
+
file_field_tag(attribute, class: govuk_file_upload_options[:classes], **govuk_file_upload_options[:attributes])
|
89
|
+
end
|
90
|
+
|
91
|
+
# Generates the file upload HTML for {govuk_file_upload_with_form}
|
92
|
+
#
|
93
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the file upload input
|
94
|
+
# @param attribute [String, Symbol] the attribute of the file upload
|
95
|
+
# @param error_message [String] used to indicate if there is an error which will add an extra classes
|
96
|
+
# @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
|
97
|
+
#
|
98
|
+
# @option (see set_file_upload_options)
|
99
|
+
#
|
100
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the file upload field which is used in {govuk_file_upload_with_form}
|
101
|
+
|
102
|
+
def govuk_file_upload_field_with_form(form, attribute, error_message, **govuk_file_upload_options)
|
103
|
+
set_file_upload_options(error_message, govuk_file_upload_options)
|
104
|
+
|
105
|
+
form.file_field(attribute, class: govuk_file_upload_options[:classes], **govuk_file_upload_options[:attributes])
|
106
|
+
end
|
107
|
+
|
108
|
+
# Initialises the attributes for the file upload input
|
109
|
+
#
|
110
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
111
|
+
# @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
|
112
|
+
#
|
113
|
+
# @option govuk_file_upload_options [String] :classes additional CSS classes for the file iupload HTML
|
114
|
+
# @option govuk_file_upload_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
115
|
+
|
116
|
+
def set_file_upload_options(error_message, govuk_file_upload_options)
|
117
|
+
govuk_file_upload_options[:classes] = "govuk-file-upload #{govuk_file_upload_options[:classes]}".rstrip
|
118
|
+
govuk_file_upload_options[:classes] << ' govuk-file-upload--error' if error_message
|
119
|
+
|
120
|
+
govuk_file_upload_options[:attributes] ||= {}
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|