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.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/Gemfile.lock +5 -5
- data/README.md +3 -4
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/accordion.rb +116 -0
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/back_link.rb +34 -0
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/breadcrumbs.rb +2 -2
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/button.rb +13 -19
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/details.rb +1 -1
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/error_message.rb +1 -1
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/character_count.rb +40 -68
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/checkboxes.rb +65 -71
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/date_input.rb +153 -0
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/file_upload.rb +94 -0
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/input.rb +156 -0
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/radios.rb +65 -67
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/select.rb +134 -0
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/textarea.rb +105 -0
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field.rb +215 -0
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/fieldset.rb +2 -2
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/form_group.rb +10 -21
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/header.rb +3 -3
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/hint.rb +1 -1
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/label.rb +88 -0
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/notification_banner.rb +1 -1
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/pagination.rb +338 -0
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/step_by_step_navigation.rb +1 -1
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/tag.rb +1 -1
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/warning_text.rb +52 -0
- data/lib/crown_marketplace_utils/{gov_uk_helper.rb → helpers/gov_uk_helper.rb} +11 -1
- data/lib/crown_marketplace_utils/version.rb +1 -1
- data/lib/crown_marketplace_utils.rb +1 -1
- metadata +28 -23
- data/lib/crown_marketplace_utils/gov_uk_helper/field/input.rb +0 -160
- data/lib/crown_marketplace_utils/gov_uk_helper/field/select.rb +0 -166
- data/lib/crown_marketplace_utils/gov_uk_helper/field/textarea.rb +0 -127
- data/lib/crown_marketplace_utils/gov_uk_helper/field.rb +0 -263
- data/lib/crown_marketplace_utils/gov_uk_helper/label.rb +0 -97
- data/lib/crown_marketplace_utils/gov_uk_helper/pagination.rb +0 -214
@@ -1,263 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'fieldset'
|
4
|
-
require_relative 'form_group'
|
5
|
-
require_relative 'hint'
|
6
|
-
require_relative 'label'
|
7
|
-
|
8
|
-
module CrownMarketplaceUtils
|
9
|
-
module GovUkHelper
|
10
|
-
# = GOV.UK Field
|
11
|
-
#
|
12
|
-
# This module contains methods to wrap the:
|
13
|
-
# - fieldset
|
14
|
-
# - form group
|
15
|
-
# - label
|
16
|
-
# - hint
|
17
|
-
# - error message
|
18
|
-
# around some kind of input.
|
19
|
-
#
|
20
|
-
# The wrapper functions in this module are used
|
21
|
-
# to create the fields using the structure of a GDS input field component.
|
22
|
-
|
23
|
-
module Field
|
24
|
-
include Fieldset
|
25
|
-
include FormGroup
|
26
|
-
include Hint
|
27
|
-
include Label
|
28
|
-
|
29
|
-
# Generates the HTML to wrap arround a GDS input field component.
|
30
|
-
#
|
31
|
-
# @param type [Symbol] the type of the field
|
32
|
-
# @param attribute [String, Symbol] the attribute of the field
|
33
|
-
# @param error_message [String] the error message to be displayed
|
34
|
-
# @param govuk_field_options [Hash] options that will be used for the parts of the form group, label, hint and field
|
35
|
-
#
|
36
|
-
# @option govuk_field_options [Hash] :form_group_options ({}) the options for govuk_form_group {govuk_form_group}
|
37
|
-
# @option govuk_field_options [Hash] :label the parameters that will be used to create the label for the field:
|
38
|
-
# - +:text+ (required) - the label text
|
39
|
-
# - +:options+ - default: { } - the options for govuk_label {govuk_label}
|
40
|
-
# @option govuk_field_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field
|
41
|
-
# - +:text+ (required) - the hint text
|
42
|
-
# - +:options+ - default: { } - the options for govuk_hint {govuk_hint}
|
43
|
-
# If no hint is given then no hint will be rendered
|
44
|
-
# @option govuk_field_options [Hash] :field_options ({}) the options that will be used when rendering the field.
|
45
|
-
# For more details look at a module that uses +Field+.
|
46
|
-
#
|
47
|
-
# @yield the field HTML
|
48
|
-
#
|
49
|
-
# @yieldparam govuk_field_options [Hash] the HTML options used when rendering the field
|
50
|
-
#
|
51
|
-
# @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the field
|
52
|
-
|
53
|
-
def govuk_field(type, attribute, error_message = nil, **govuk_field_options)
|
54
|
-
field_key = :"#{type}_options"
|
55
|
-
|
56
|
-
set_label_for_if_custom_id(field_key, govuk_field_options)
|
57
|
-
set_hint_id(attribute, govuk_field_options)
|
58
|
-
|
59
|
-
govuk_form_group(attribute, error_message, **(govuk_field_options[:form_group_options] || {})) do |display_error_message|
|
60
|
-
set_field_described_by(field_key, attribute, error_message, govuk_field_options)
|
61
|
-
|
62
|
-
capture do
|
63
|
-
concat(govuk_label(attribute, govuk_field_options[:label][:text], **govuk_field_options[:label][:options]))
|
64
|
-
concat(govuk_hint(govuk_field_options[:hint][:text], **govuk_field_options[:hint][:options])) if govuk_field_options[:hint]
|
65
|
-
concat(display_error_message)
|
66
|
-
yield(govuk_field_options[field_key])
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
# Generates the HTML to wrap arround a GDS input field component using an ActionView::Helpers::FormBuilder.
|
72
|
-
# Unlike {govuk_field}, the method will be able to automatically determine if the error message needs to be shown.
|
73
|
-
#
|
74
|
-
# @param type [Symbol] the type of the field
|
75
|
-
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the label
|
76
|
-
# @param attribute [String, Symbol] the attribute of the field
|
77
|
-
# @param govuk_field_options [Hash] options that will be used for the parts of the form group, label, hint and field
|
78
|
-
#
|
79
|
-
# @option govuk_field_options [Hash] :form_group_options ({}) the options for govuk_form_group {govuk_form_group}
|
80
|
-
# @option govuk_field_options [Hash] :label the parameters that will be used to create the label for the field:
|
81
|
-
# - +:text+ (required) - the label text
|
82
|
-
# - +:options+ - default: { } - the options for govuk_label {govuk_label}
|
83
|
-
# @option govuk_field_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field
|
84
|
-
# - +:text+ (required) - the hint text
|
85
|
-
# - +:options+ - default: { } - the options for govuk_hint {govuk_hint}
|
86
|
-
# If no hint is given then no hint will be rendered
|
87
|
-
# @option govuk_field_options [Hash] :field_options ({}) the options that will be used when rendering the field.
|
88
|
-
# For more details look at a module that uses +Field+.
|
89
|
-
#
|
90
|
-
# @yield the field HTML
|
91
|
-
#
|
92
|
-
# @yieldparam govuk_field_options [Hash] the HTML options used when rendering the field
|
93
|
-
# @yieldparam error_message [ActiveSupport::SafeBuffer] used by the field to indicate if there is an error
|
94
|
-
#
|
95
|
-
# @return (see govuk_field)
|
96
|
-
|
97
|
-
def govuk_field_with_form(type, form, attribute, **govuk_field_options)
|
98
|
-
field_key = :"#{type}_options"
|
99
|
-
|
100
|
-
set_label_for_if_custom_id(field_key, govuk_field_options)
|
101
|
-
set_hint_id(attribute, govuk_field_options)
|
102
|
-
|
103
|
-
govuk_form_group_with_model(form.object, attribute, **(govuk_field_options[:form_group_options] || {})) do |display_error_message|
|
104
|
-
set_field_described_by(field_key, attribute, display_error_message, govuk_field_options)
|
105
|
-
|
106
|
-
capture do
|
107
|
-
concat(govuk_label_with_form(form, attribute, govuk_field_options[:label][:text], **(govuk_field_options[:label][:options] || {})))
|
108
|
-
concat(govuk_hint(govuk_field_options[:hint][:text], **govuk_field_options[:hint][:options])) if govuk_field_options[:hint]
|
109
|
-
concat(display_error_message)
|
110
|
-
yield(govuk_field_options[field_key], display_error_message)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
# Generates the HTML to wrap arround a GDS input fields component.
|
116
|
-
# These are inputs that require being wrapped in a fieldset, for example radio buttons.
|
117
|
-
#
|
118
|
-
# @param type [Symbol] the type of the fields
|
119
|
-
# @param attribute [String, Symbol] the attribute of the fields
|
120
|
-
# @param error_message [String] the error message to be displayed
|
121
|
-
# @param govuk_fields_options [Hash] options that will be used for the parts of the fieldset, form group, hint and fields
|
122
|
-
#
|
123
|
-
# @option govuk_fields_options [Hash] :form_group_options ({}) the options for govuk_form_group {govuk_form_group}
|
124
|
-
# @option govuk_fields_options [Hash] :fieldset_options ({}) the options for govuk_fieldset {govuk_fieldset}
|
125
|
-
# @option govuk_fields_options [Hash] :hint (nil) the parameters that will be used to create the hint for the fields
|
126
|
-
# - +:text+ (required) - the hint text
|
127
|
-
# - +:options+ - default: { } - the options for govuk_hint {govuk_hint}
|
128
|
-
# If no hint is given then no hint will be rendered
|
129
|
-
# @option govuk_fields_options [Hash] :field_options ({}) the options that will be used when rendering the fields.
|
130
|
-
# For more details look at a module that uses +Field+.
|
131
|
-
#
|
132
|
-
# @yield the fields HTML
|
133
|
-
#
|
134
|
-
# @yieldparam govuk_fields_options [Hash] the HTML options used when rendering the fields
|
135
|
-
#
|
136
|
-
# @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the fields
|
137
|
-
|
138
|
-
def govuk_fields(type, attribute, error_message = nil, **govuk_fields_options)
|
139
|
-
field_key = :"#{type}_options"
|
140
|
-
|
141
|
-
set_hint_id(attribute, govuk_fields_options)
|
142
|
-
|
143
|
-
govuk_form_group(attribute, error_message, **(govuk_fields_options[:form_group_options] || {})) do |display_error_message|
|
144
|
-
set_field_described_by(:fieldset_options, attribute, error_message, govuk_fields_options)
|
145
|
-
(govuk_fields_options[field_key] ||= {})[:attributes] ||= {}
|
146
|
-
|
147
|
-
govuk_fieldset(**govuk_fields_options[:fieldset_options]) do
|
148
|
-
capture do
|
149
|
-
concat(govuk_hint(govuk_fields_options[:hint][:text], **govuk_fields_options[:hint][:options])) if govuk_fields_options[:hint]
|
150
|
-
concat(display_error_message)
|
151
|
-
yield(govuk_fields_options[field_key])
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
# Generates the HTML to wrap arround a GDS input fields component using an ActionView::Helpers::FormBuilder.
|
158
|
-
# These are inputs that require being wrapped in a fieldset, for example radio buttons.
|
159
|
-
#
|
160
|
-
# Unlike {govuk_fields}, the method will be able to automatically determine if the error message needs to be shown.
|
161
|
-
#
|
162
|
-
# @param type [Symbol] the type of the fields
|
163
|
-
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the label
|
164
|
-
# @param attribute [String, Symbol] the attribute of the fields
|
165
|
-
# @param govuk_fields_options [Hash] options that will be used for the parts of the fieldset, form group, hint and fields
|
166
|
-
#
|
167
|
-
# @option govuk_fields_options [Hash] :form_group_options ({}) the options for govuk_form_group {govuk_form_group}
|
168
|
-
# @option govuk_fields_options [Hash] :fieldset_options ({}) the options for govuk_fieldset {govuk_fieldset}
|
169
|
-
# @option govuk_fields_options [Hash] :hint (nil) the parameters that will be used to create the hint for the fields
|
170
|
-
# - +:text+ (required) - the hint text
|
171
|
-
# - +:options+ - default: { } - the options for govuk_hint {govuk_hint}
|
172
|
-
# If no hint is given then no hint will be rendered
|
173
|
-
# @option govuk_fields_options [Hash] :field_options ({}) the options that will be used when rendering the fields.
|
174
|
-
# For more details look at a module that uses +Field+.
|
175
|
-
#
|
176
|
-
# @yield the fields HTML
|
177
|
-
#
|
178
|
-
# @yieldparam govuk_fields_options [Hash] the HTML options used when rendering the fields
|
179
|
-
#
|
180
|
-
# @return (see govuk_fields)
|
181
|
-
|
182
|
-
def govuk_fields_with_form(type, form, attribute, **govuk_fields_options)
|
183
|
-
field_key = :"#{type}_options"
|
184
|
-
|
185
|
-
set_hint_id(attribute, govuk_fields_options)
|
186
|
-
|
187
|
-
govuk_form_group_with_model(form.object, attribute, **(govuk_fields_options[:form_group_options] || {})) do |display_error_message|
|
188
|
-
set_field_described_by(:fieldset_options, attribute, display_error_message, govuk_fields_options)
|
189
|
-
(govuk_fields_options[field_key] ||= {})[:attributes] ||= {}
|
190
|
-
|
191
|
-
govuk_fieldset(**govuk_fields_options[:fieldset_options]) do
|
192
|
-
capture do
|
193
|
-
concat(govuk_hint(govuk_fields_options[:hint][:text], **govuk_fields_options[:hint][:options])) if govuk_fields_options[:hint]
|
194
|
-
concat(display_error_message)
|
195
|
-
yield(govuk_fields_options[field_key])
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
private
|
202
|
-
|
203
|
-
# Sets the label +for+ if a custom ID has been given for the field.
|
204
|
-
#
|
205
|
-
# @param govuk_field_options [Hash] see {govuk_field} for details
|
206
|
-
|
207
|
-
def set_label_for_if_custom_id(field_key, govuk_field_options)
|
208
|
-
field_id = govuk_field_options.dig(field_key, :attributes, :id)
|
209
|
-
|
210
|
-
govuk_field_options[:label][:options] ||= {}
|
211
|
-
(govuk_field_options[:label][:options][:attributes] ||= {}).merge!({ for: field_id }) if field_id
|
212
|
-
end
|
213
|
-
|
214
|
-
# Sets the hint ID if there is a hint, and the ID for the hint has not been sent
|
215
|
-
#
|
216
|
-
# @param attribute [String, Symbol] the attribute of the field
|
217
|
-
# @param govuk_field_options [Hash] see {govuk_field} for details
|
218
|
-
|
219
|
-
def set_hint_id(attribute, govuk_field_options)
|
220
|
-
return if !govuk_field_options[:hint] || govuk_field_options.dig(:hint, :options, :attributes, :id)
|
221
|
-
|
222
|
-
govuk_field_options[:hint][:options] ||= {}
|
223
|
-
(govuk_field_options[:hint][:options][:attributes] ||= {}).merge!({ id: "#{attribute}-hint" })
|
224
|
-
end
|
225
|
-
|
226
|
-
# Adds the aira-describedby attribute for the field
|
227
|
-
# if there is a hint or an error message
|
228
|
-
#
|
229
|
-
# @param attribute [String, Symbol] the attribute of the input
|
230
|
-
# @param error_message [String] used to indicate if there is an error
|
231
|
-
# @param govuk_field_options [Hash] see {#govuk_field} for details
|
232
|
-
|
233
|
-
def set_field_described_by(field_key, attribute, error_message, govuk_field_options)
|
234
|
-
aria_described_by = []
|
235
|
-
aria_described_by << govuk_field_options.dig(field_key, :attributes, :aria, :describedby)
|
236
|
-
aria_described_by << govuk_field_options[:hint][:options][:attributes][:id] if govuk_field_options[:hint]
|
237
|
-
aria_described_by << "#{attribute}-error" if error_message
|
238
|
-
aria_described_by.compact!
|
239
|
-
|
240
|
-
govuk_field_options[field_key] ||= {}
|
241
|
-
govuk_field_options[field_key][:attributes] ||= {}
|
242
|
-
|
243
|
-
return unless aria_described_by.any?
|
244
|
-
|
245
|
-
(govuk_field_options[field_key][:attributes][:aria] ||= {}).merge!({ describedby: aria_described_by.join(' ') })
|
246
|
-
end
|
247
|
-
|
248
|
-
# Sets the hint classes and adds the aira-describedby attribute for a fields item
|
249
|
-
#
|
250
|
-
# @param type [String] the type of the item
|
251
|
-
# @param attribute [String, Symbol] the attribute of the item
|
252
|
-
# @param item [Hash] the options for that item
|
253
|
-
|
254
|
-
def set_item_options_for_hint(type, attribute, item)
|
255
|
-
(item[:hint][:options] ||= {})[:attributes] ||= {}
|
256
|
-
item[:hint][:options][:classes] = "govuk-#{type}__hint #{item[:hint][:options][:classes]}".rstrip
|
257
|
-
item[:hint][:options][:attributes][:id] ||= "#{attribute}_#{item[:value]}-item-hint"
|
258
|
-
|
259
|
-
(item[:attributes][:aria] ||= {})[:describedby] = item[:hint][:options][:attributes][:id]
|
260
|
-
end
|
261
|
-
end
|
262
|
-
end
|
263
|
-
end
|
@@ -1,97 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'action_view'
|
4
|
-
|
5
|
-
module CrownMarketplaceUtils
|
6
|
-
module GovUkHelper
|
7
|
-
# = GOV.UK label
|
8
|
-
#
|
9
|
-
# This helper is used for generating the label component from the Government Design Systems
|
10
|
-
|
11
|
-
module Label
|
12
|
-
include ActionView::Context
|
13
|
-
include ActionView::Helpers::TagHelper
|
14
|
-
include ActionView::Helpers::TextHelper
|
15
|
-
include ActionView::Helpers::FormTagHelper
|
16
|
-
|
17
|
-
# Generates the HTML for the GOV.UK label component
|
18
|
-
#
|
19
|
-
# @param attribute [String, Symbol] the attribute of the input that requires a label
|
20
|
-
# @param label_text [String] the label text, it is ignored if a block is given
|
21
|
-
# @param govuk_label_options [Hash] options that will be used in customising the HTML
|
22
|
-
#
|
23
|
-
# @option govuk_label_options [String] :classes additional CSS classes for the label HTML
|
24
|
-
# @option govuk_label_options [Boolean] :is_page_heading (false) if the legend is also the heading it will rendered in a h1
|
25
|
-
# @option govuk_label_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
26
|
-
#
|
27
|
-
# @yield HTML that will be contained within the 'govuk-label' label
|
28
|
-
#
|
29
|
-
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Label
|
30
|
-
# which can then be rendered on the page
|
31
|
-
|
32
|
-
def govuk_label(attribute, label_text = nil, **govuk_label_options)
|
33
|
-
_govuk_label(**govuk_label_options) do |govuk_label_classes, govuk_label_attributes|
|
34
|
-
label_tag(attribute, class: govuk_label_classes, **govuk_label_attributes) do
|
35
|
-
if block_given?
|
36
|
-
yield
|
37
|
-
else
|
38
|
-
concat(label_text)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
# Generates the HTML for the GOV.UK label component using an ActionView::Helpers::FormBuilder
|
45
|
-
#
|
46
|
-
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the label
|
47
|
-
# @param attribute [String, Symbol] the attribute of the input that requires a label
|
48
|
-
# @param label_text [String] the label text, it is ignored if a block is given
|
49
|
-
# @param govuk_label_options [Hash] options that will be used in customising the HTML
|
50
|
-
#
|
51
|
-
# @option (see govuk_label)
|
52
|
-
#
|
53
|
-
# @yield (see govuk_label)
|
54
|
-
#
|
55
|
-
# @return (see govuk_label)
|
56
|
-
|
57
|
-
def govuk_label_with_form(form, attribute, label_text = nil, **govuk_label_options, &block)
|
58
|
-
_govuk_label(**govuk_label_options) do |govuk_label_classes, govuk_label_attributes|
|
59
|
-
if block_given?
|
60
|
-
form.label(attribute, class: govuk_label_classes, **govuk_label_attributes, &block)
|
61
|
-
else
|
62
|
-
form.label(attribute, label_text, class: govuk_label_classes, **govuk_label_attributes)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
private
|
68
|
-
|
69
|
-
# Wrapper method used by {#govuk_label} and {#govuk_label_with_form} to generate the Label HTML
|
70
|
-
#
|
71
|
-
# @param govuk_label_options [Hash] options that will be used in customising the HTML
|
72
|
-
#
|
73
|
-
# @option (see govuk_label)
|
74
|
-
#
|
75
|
-
# @yield the label HTML generated by {#govuk_label} or {#govuk_label_with_form}
|
76
|
-
#
|
77
|
-
# @yieldparam govuk_label_classes [Array] the classes for the label HTML
|
78
|
-
# @yieldparam govuk_label_attributes [Hash] additional attributes that will added as part of the HTML
|
79
|
-
#
|
80
|
-
# @return (see govuk_label)
|
81
|
-
|
82
|
-
def _govuk_label(**govuk_label_options)
|
83
|
-
govuk_label_classes = ['govuk-label']
|
84
|
-
govuk_label_classes << govuk_label_options[:classes]
|
85
|
-
govuk_label_options[:attributes] ||= {}
|
86
|
-
|
87
|
-
label_html = yield(govuk_label_classes, govuk_label_options[:attributes])
|
88
|
-
|
89
|
-
if govuk_label_options[:is_page_heading]
|
90
|
-
tag.h1(label_html, class: 'govuk-label-wrapper')
|
91
|
-
else
|
92
|
-
label_html
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
@@ -1,214 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'action_view'
|
4
|
-
|
5
|
-
module CrownMarketplaceUtils
|
6
|
-
module GovUkHelper
|
7
|
-
# = GOV.UK Pagination
|
8
|
-
#
|
9
|
-
# This helper is used for generating the pagination component from the
|
10
|
-
# {https://design-system.service.gov.uk/components/pagination GDS - Components - Pagination}
|
11
|
-
|
12
|
-
module Pagination
|
13
|
-
include ActionView::Context
|
14
|
-
include ActionView::Helpers::TagHelper
|
15
|
-
include ActionView::Helpers::TextHelper
|
16
|
-
include ActionView::Helpers::UrlHelper
|
17
|
-
|
18
|
-
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
19
|
-
|
20
|
-
# Generates the HTML for the GOV.UK Pagination component
|
21
|
-
#
|
22
|
-
# @param govuk_pagination_options [Hash] options that will be used in customising the HTML
|
23
|
-
#
|
24
|
-
# @option govuk_pagination_options [String] :classes additional CSS classes for the pagination HTML
|
25
|
-
# @option govuk_pagination_options [Array] :items an array of items for the pagination (see: {govuk_pagination_items})
|
26
|
-
# @option govuk_pagination_options [Hash] :previous the previous link (see: {govuk_pagination_previous})
|
27
|
-
# @option govuk_pagination_options [Hash] :next the next link (see: {govuk_pagination_next})
|
28
|
-
# @option govuk_pagination_options [Hash] :attributes ({role: 'navigation', aria: { label: 'results' }})
|
29
|
-
# any additional attributes that will added as part of the HTML
|
30
|
-
#
|
31
|
-
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Pagination
|
32
|
-
# which can then be rendered on the page
|
33
|
-
|
34
|
-
def govuk_pagination(**govuk_pagination_options)
|
35
|
-
govuk_pagination_classes = ['govuk-pagination']
|
36
|
-
govuk_pagination_classes << govuk_pagination_options[:classes]
|
37
|
-
|
38
|
-
block_is_level = govuk_pagination_options[:items].blank? && (govuk_pagination_options[:next].present? || govuk_pagination_options[:previous].present?)
|
39
|
-
|
40
|
-
govuk_pagination_classes << 'govuk-pagination--block' if block_is_level
|
41
|
-
|
42
|
-
(govuk_pagination_options[:attributes] ||= {}).merge({ role: 'navigation' })
|
43
|
-
(govuk_pagination_options[:attributes][:aria] ||= {})[:label] ||= 'results'
|
44
|
-
|
45
|
-
tag.nav(class: govuk_pagination_classes, **govuk_pagination_options[:attributes]) do
|
46
|
-
capture do
|
47
|
-
concat(govuk_pagination_previous(block_is_level, **govuk_pagination_options[:previous])) if govuk_pagination_options[:previous]
|
48
|
-
concat(govuk_pagination_items(govuk_pagination_options[:items])) if govuk_pagination_options[:items]
|
49
|
-
concat(govuk_pagination_next(block_is_level, **govuk_pagination_options[:next])) if govuk_pagination_options[:next]
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
# Generates the previous link for {govuk_pagination}
|
59
|
-
#
|
60
|
-
# @param block_is_level [Boolean] when there are no items, this will be true and will add extra classe
|
61
|
-
# to the link to make the next and previous pagination links level
|
62
|
-
# @param govuk_pagination_previous_options [Hash] options that will be used in customising the HTML
|
63
|
-
#
|
64
|
-
# @option govuk_pagination_previous_options [String] :href the URL for the link
|
65
|
-
# @option govuk_pagination_previous_options [String] :text ('Previous') the text for the link
|
66
|
-
# @option govuk_pagination_previous_options [String] :lable_text additional text for the link when the pagination block is level
|
67
|
-
# @option govuk_pagination_previous_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
68
|
-
#
|
69
|
-
# @return [ActiveSupport::SafeBuffer] the HTML for the previous link which is used in {govuk_pagination}
|
70
|
-
|
71
|
-
def govuk_pagination_previous(block_is_level, **govuk_pagination_previous_options)
|
72
|
-
govuk_pagination_previous_classes = ['govuk-pagination__link-title']
|
73
|
-
govuk_pagination_previous_classes << 'govuk-pagination__link-title--decorated' if block_is_level && !govuk_pagination_previous_options[:lable_text]
|
74
|
-
|
75
|
-
tag.div(class: 'govuk-pagination__prev') do
|
76
|
-
link_to(govuk_pagination_previous_options[:href], class: 'govuk-link govuk-pagination__link', rel: 'prev', **(govuk_pagination_previous_options[:attributes] || {})) do
|
77
|
-
capture do
|
78
|
-
concat(govuk_pagination_icon(:prev))
|
79
|
-
concat(tag.span(govuk_pagination_previous_options[:text] || 'Previous', class: govuk_pagination_previous_classes))
|
80
|
-
concat(govuk_pagination_icon_label_text(block_is_level, govuk_pagination_previous_options[:lable_text]))
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
# Generates the next link for {govuk_pagination}
|
87
|
-
#
|
88
|
-
# @param block_is_level [Boolean] when there are no items, this will be true and will add extra classe
|
89
|
-
# to the link to make the next and previous pagination links level
|
90
|
-
# @param govuk_pagination_next_options [Hash] options that will be used in customising the HTML
|
91
|
-
#
|
92
|
-
# @option govuk_pagination_next_options [String] :href the URL for the link
|
93
|
-
# @option govuk_pagination_next_options [String] :text ('Next') the text for the link
|
94
|
-
# @option govuk_pagination_next_options [String] :lable_text additional text for the link when the pagination block is level
|
95
|
-
# @option govuk_pagination_next_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
96
|
-
#
|
97
|
-
# @return [ActiveSupport::SafeBuffer] the HTML for the next link which is used in {govuk_pagination}
|
98
|
-
|
99
|
-
def govuk_pagination_next(block_is_level, **govuk_pagination_next_options)
|
100
|
-
govuk_pagination_next_classes = ['govuk-pagination__link-title']
|
101
|
-
govuk_pagination_next_classes << 'govuk-pagination__link-title--decorated' if block_is_level && !govuk_pagination_next_options[:lable_text]
|
102
|
-
|
103
|
-
tag.div(class: 'govuk-pagination__next') do
|
104
|
-
link_to(govuk_pagination_next_options[:href], class: 'govuk-link govuk-pagination__link', rel: 'next', **(govuk_pagination_next_options[:attributes] || {})) do
|
105
|
-
capture do
|
106
|
-
concat(govuk_pagination_icon(:next)) if block_is_level
|
107
|
-
concat(tag.span(govuk_pagination_next_options[:text] || 'Next', class: govuk_pagination_next_classes))
|
108
|
-
concat(govuk_pagination_icon_label_text(block_is_level, govuk_pagination_next_options[:lable_text]))
|
109
|
-
concat(govuk_pagination_icon(:next)) unless block_is_level
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
# Generates the item links for {govuk_pagination}
|
116
|
-
#
|
117
|
-
# @param govuk_pagination_items [Array(Hash)] an array of item hashes for the pagination.
|
118
|
-
# There are two types of item:
|
119
|
-
# - {govuk_pagination_item_ellipsis ellipsis item}
|
120
|
-
# - {govuk_pagination_item_number number item}
|
121
|
-
#
|
122
|
-
# @return [ActiveSupport::SafeBuffer] the HTML for the lits of items which is used in {govuk_pagination}
|
123
|
-
|
124
|
-
def govuk_pagination_items(govuk_pagination_items)
|
125
|
-
tag.ul(class: 'govuk-pagination__list') do
|
126
|
-
capture do
|
127
|
-
govuk_pagination_items.each do |govuk_pagination_item|
|
128
|
-
case govuk_pagination_item[:type]
|
129
|
-
when :ellipsis
|
130
|
-
concat(govuk_pagination_item_ellipsis)
|
131
|
-
when :number
|
132
|
-
concat(govuk_pagination_item_number(govuk_pagination_item))
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
# Generates the icon for:
|
140
|
-
# - {govuk_pagination_previous}
|
141
|
-
# - {govuk_pagination_next}
|
142
|
-
#
|
143
|
-
# @param type [Symbol] the type of the pagination icon (+:prev+ or +:next+)
|
144
|
-
#
|
145
|
-
# @return [ActiveSupport::SafeBuffer]
|
146
|
-
|
147
|
-
def govuk_pagination_icon(type)
|
148
|
-
tag.svg(class: "govuk-pagination__icon govuk-pagination__icon--#{type}", xmlns: 'http://www.w3.org/2000/svg', height: '13', width: '15', aria: { hidden: 'true' }, focusable: 'false', viewBox: '0 0 15 13') do
|
149
|
-
tag.path(d: PAGINATION_ICON_PATHS[type])
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
# Generates the label text for:
|
154
|
-
# - {govuk_pagination_previous}
|
155
|
-
# - {govuk_pagination_next}
|
156
|
-
#
|
157
|
-
# @param block_is_level [Boolean] when there are no items, this will be true
|
158
|
-
# @param label_text [String] the additional text for the link
|
159
|
-
#
|
160
|
-
# @return [ActiveSupport::SafeBuffer]
|
161
|
-
|
162
|
-
def govuk_pagination_icon_label_text(block_is_level, label_text)
|
163
|
-
return unless block_is_level && label_text
|
164
|
-
|
165
|
-
capture do
|
166
|
-
concat(tag.span(':', class: 'govuk-visually-hidden'))
|
167
|
-
concat(tag.span(label_text, class: 'govuk-pagination__link-label'))
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
# Generates the ellipsis HTML for {govuk_pagination_items}
|
172
|
-
#
|
173
|
-
# @return [ActiveSupport::SafeBuffer] the HTML for an ellipsis item which is used in {govuk_pagination_items}
|
174
|
-
|
175
|
-
def govuk_pagination_item_ellipsis
|
176
|
-
tag.li('⋯', class: 'govuk-pagination__item govuk-pagination__item--ellipses')
|
177
|
-
end
|
178
|
-
|
179
|
-
# Generates the number item HTML for {govuk_pagination_items}
|
180
|
-
#
|
181
|
-
# @param govuk_pagination_item [Hash] options that will be used in customising the HTML
|
182
|
-
#
|
183
|
-
# @option govuk_pagination_item [String] :href the URL for the link
|
184
|
-
# @option govuk_pagination_item [String] :number the number for the link
|
185
|
-
# @option govuk_pagination_item [String] :current is this item the current page
|
186
|
-
# @option govuk_pagination_item [Hash] :attributes ({aria: { label: 'Page <NUMBER>' } })
|
187
|
-
# any additional attributes that will added as part of the HTML
|
188
|
-
#
|
189
|
-
# @return [ActiveSupport::SafeBuffer] the HTML for an number item which is used in {govuk_pagination_items}
|
190
|
-
|
191
|
-
def govuk_pagination_item_number(govuk_pagination_item)
|
192
|
-
govuk_pagination_item_classes = ['govuk-pagination__item']
|
193
|
-
(govuk_pagination_item[:attributes] ||= {})[:aria] ||= {}
|
194
|
-
govuk_pagination_item[:attributes][:aria][:label] ||= "Page #{govuk_pagination_item[:number]}"
|
195
|
-
|
196
|
-
if govuk_pagination_item[:current]
|
197
|
-
govuk_pagination_item_classes << 'govuk-pagination__item--current'
|
198
|
-
govuk_pagination_item[:attributes][:aria][:current] = 'page'
|
199
|
-
end
|
200
|
-
|
201
|
-
tag.li(class: govuk_pagination_item_classes) do
|
202
|
-
link_to(govuk_pagination_item[:href], govuk_pagination_item[:number], class: 'govuk-link govuk-pagination__link', **govuk_pagination_item[:attributes])
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
# The paths for the pagination next and previous icons
|
207
|
-
|
208
|
-
PAGINATION_ICON_PATHS = {
|
209
|
-
prev: 'm6.5938-0.0078125-6.7266 6.7266 6.7441 6.4062 1.377-1.449-4.1856-3.9768h12.896v-2h-12.984l4.2931-4.293-1.414-1.414z',
|
210
|
-
next: 'm8.107-0.0078125-1.4136 1.414 4.2926 4.293h-12.986v2h12.896l-4.1855 3.9766 1.377 1.4492 6.7441-6.4062-6.7246-6.7266z'
|
211
|
-
}.freeze
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|