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,140 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module GovUkHelper
|
7
|
+
module Field
|
8
|
+
# = GOV.UK Textarea
|
9
|
+
#
|
10
|
+
# This helper is used for generating the textarea component from the
|
11
|
+
# {https://design-system.service.gov.uk/components/textarea GDS - Components - Textarea}
|
12
|
+
#
|
13
|
+
# This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
|
14
|
+
|
15
|
+
module Textarea
|
16
|
+
include Field
|
17
|
+
|
18
|
+
# Generates the HTML for the GOV.UK textarea component
|
19
|
+
#
|
20
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
21
|
+
# @param error_message [String] the error message to be displayed
|
22
|
+
# @param govuk_textarea_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
|
23
|
+
#
|
24
|
+
# @option govuk_textarea_options [Hash] :form_group see {govuk_field}
|
25
|
+
# @option govuk_textarea_options [Hash] :label see {govuk_field}
|
26
|
+
# @option govuk_textarea_options [Hash] :hint see {govuk_field}
|
27
|
+
# @option govuk_textarea_options [Hash] :textarea ({}) the options that will be used when rendering the textarea.
|
28
|
+
# See {govuk_textarea_field} for more details.
|
29
|
+
#
|
30
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK textarea
|
31
|
+
# which can then be rendered on the page
|
32
|
+
|
33
|
+
def govuk_textarea(attribute, error_message = nil, **govuk_textarea_options)
|
34
|
+
govuk_field(:textarea, attribute, error_message, **govuk_textarea_options) do |govuk_field_options|
|
35
|
+
concat(govuk_textarea_field(attribute, error_message, **govuk_field_options))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Generates the HTML for the GOV.UK textarea component using an ActiveModel.
|
40
|
+
# Unlike {#govuk_textarea}, 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 and content of the textarea
|
43
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
44
|
+
# @param govuk_textarea_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
|
45
|
+
#
|
46
|
+
# @option (see govuk_textarea)
|
47
|
+
#
|
48
|
+
# @return (see govuk_textarea)
|
49
|
+
|
50
|
+
def govuk_textarea_with_model(model, attribute, **govuk_textarea_options)
|
51
|
+
(govuk_textarea_options[:textarea] ||= {})[:content] = model.send(attribute)
|
52
|
+
|
53
|
+
govuk_field_with_model(:textarea, model, attribute, **govuk_textarea_options) do |govuk_field_options, error_message|
|
54
|
+
concat(govuk_textarea_field(attribute, error_message, **govuk_field_options))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Generates the HTML for the GOV.UK textarea component using an ActionView::Helpers::FormBuilder.
|
59
|
+
# Unlike {#govuk_textarea}, the method will be able to automatically determine if the error message needs to be shown.
|
60
|
+
#
|
61
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the textarea
|
62
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
63
|
+
# @param govuk_textarea_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
|
64
|
+
#
|
65
|
+
# @option (see govuk_textarea)
|
66
|
+
#
|
67
|
+
# @return (see govuk_textarea)
|
68
|
+
|
69
|
+
def govuk_textarea_with_form(form, attribute, **govuk_textarea_options)
|
70
|
+
govuk_field_with_form(:textarea, form, attribute, **govuk_textarea_options) do |govuk_field_options, error_message|
|
71
|
+
concat(govuk_textarea_field_with_form(form, attribute, error_message, **govuk_field_options))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
# Generates the textarea HTML for {govuk_textarea}
|
78
|
+
#
|
79
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
80
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
81
|
+
# @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
|
82
|
+
#
|
83
|
+
# @option (see set_govuk_textarea_field_options)
|
84
|
+
#
|
85
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the textarea field which is used in {govuk_textarea}
|
86
|
+
|
87
|
+
def govuk_textarea_field(attribute, error_message, **govuk_text_textarea_options)
|
88
|
+
set_govuk_textarea_field_options(error_message, govuk_text_textarea_options)
|
89
|
+
|
90
|
+
text_area_tag(
|
91
|
+
attribute,
|
92
|
+
govuk_text_textarea_options[:content],
|
93
|
+
class: govuk_text_textarea_options[:classes],
|
94
|
+
rows: govuk_text_textarea_options[:rows],
|
95
|
+
**govuk_text_textarea_options[:attributes]
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Generates the textarea HTML for {govuk_textarea_with_form}
|
100
|
+
#
|
101
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the textarea
|
102
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
103
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
104
|
+
# @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
|
105
|
+
#
|
106
|
+
# @option (see set_govuk_textarea_field_options)
|
107
|
+
#
|
108
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the textarea field which is used in {govuk_textarea_with_form}
|
109
|
+
|
110
|
+
def govuk_textarea_field_with_form(form, attribute, error_message, **govuk_text_textarea_options)
|
111
|
+
set_govuk_textarea_field_options(error_message, govuk_text_textarea_options)
|
112
|
+
|
113
|
+
form.text_area(
|
114
|
+
attribute,
|
115
|
+
class: govuk_text_textarea_options[:classes],
|
116
|
+
rows: govuk_text_textarea_options[:rows],
|
117
|
+
**govuk_text_textarea_options[:attributes]
|
118
|
+
)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Initialises the attributes for the textarea input
|
122
|
+
#
|
123
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
124
|
+
# @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
|
125
|
+
#
|
126
|
+
# @option govuk_text_textarea_options [String] :classes additional CSS classes for the textarea HTML
|
127
|
+
# @option govuk_text_textarea_options [String] :content (nil) the content of the textarea
|
128
|
+
# @option govuk_text_textarea_options [String] :rows (5) the number of rows for the text area
|
129
|
+
# @option govuk_text_textarea_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
130
|
+
|
131
|
+
def set_govuk_textarea_field_options(error_message, govuk_text_textarea_options)
|
132
|
+
govuk_text_textarea_options[:classes] = "govuk-textarea #{govuk_text_textarea_options[:classes]}".rstrip
|
133
|
+
govuk_text_textarea_options[:classes] << ' govuk-textarea--error' if error_message
|
134
|
+
govuk_text_textarea_options[:attributes] ||= {}
|
135
|
+
govuk_text_textarea_options[:rows] ||= 5
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,305 @@
|
|
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
|
+
# rubocop:disable Metrics/ModuleLength
|
11
|
+
|
12
|
+
# = GOV.UK Field
|
13
|
+
#
|
14
|
+
# This module contains methods to wrap the:
|
15
|
+
# - fieldset
|
16
|
+
# - form group
|
17
|
+
# - label
|
18
|
+
# - hint
|
19
|
+
# - error message
|
20
|
+
# around some kind of input.
|
21
|
+
#
|
22
|
+
# The wrapper functions in this module are used
|
23
|
+
# to create the fields using the structure of a GDS input field component.
|
24
|
+
|
25
|
+
module Field
|
26
|
+
include Fieldset
|
27
|
+
include FormGroup
|
28
|
+
include Hint
|
29
|
+
include Label
|
30
|
+
|
31
|
+
# Generates the HTML to wrap arround a GDS input field component.
|
32
|
+
#
|
33
|
+
# @param field [Symbol] the type of the field
|
34
|
+
# @param attribute [String, Symbol] the attribute of the field
|
35
|
+
# @param error_message [String] the error message to be displayed
|
36
|
+
# @param govuk_field_options [Hash] options that will be used for the parts of the form group, label, hint and field
|
37
|
+
#
|
38
|
+
# @option govuk_field_options [Hash] :form_group ({}) the options for govuk_form_group {govuk_form_group}
|
39
|
+
# @option govuk_field_options [Hash] :label the parameters that will be used to create the label for the field, see {govuk_label}
|
40
|
+
# @option govuk_field_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field, see {govuk_hint}.
|
41
|
+
# If no hint is given then no hint will be rendered
|
42
|
+
# @option govuk_field_options [Hash] :field_options ({}) the options that will be used when rendering the field.
|
43
|
+
# For more details look at the specific module that uses +Field+.
|
44
|
+
#
|
45
|
+
# @yield the field HTML
|
46
|
+
#
|
47
|
+
# @yieldparam govuk_field_options [Hash] the HTML options used when rendering the field
|
48
|
+
#
|
49
|
+
# @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the field
|
50
|
+
|
51
|
+
def govuk_field(field, attribute, error_message = nil, **govuk_field_options)
|
52
|
+
set_label_for_if_custom_id(field, govuk_field_options)
|
53
|
+
set_hint_id(attribute, govuk_field_options)
|
54
|
+
|
55
|
+
govuk_form_group(attribute, error_message, **(govuk_field_options[:form_group] || {})) do |display_error_message|
|
56
|
+
set_field_described_by(field, attribute, error_message, govuk_field_options)
|
57
|
+
|
58
|
+
capture do
|
59
|
+
concat(govuk_label(attribute, govuk_field_options[:label][:text], **govuk_field_options[:label]))
|
60
|
+
concat(govuk_hint(govuk_field_options[:hint][:text], **govuk_field_options[:hint])) if govuk_field_options[:hint]
|
61
|
+
concat(display_error_message)
|
62
|
+
yield(govuk_field_options[field])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Generates the HTML to wrap arround a GDS input field component using an ActiveModel.
|
68
|
+
# Unlike {govuk_field}, the method will be able to automatically determine if the error message needs to be shown.
|
69
|
+
#
|
70
|
+
# @param field [Symbol] the type of the field
|
71
|
+
# @param model [ActiveModel] model that will be used to find an error message
|
72
|
+
# @param attribute [String, Symbol] the attribute of the field
|
73
|
+
# @param govuk_field_options [Hash] options that will be used for the parts of the form group, label, hint and field
|
74
|
+
#
|
75
|
+
# @option (see govuk_field)
|
76
|
+
#
|
77
|
+
# @yield (see govuk_field)
|
78
|
+
#
|
79
|
+
# @yieldparam (see govuk_field)
|
80
|
+
# @yieldparam error_message [ActiveSupport::SafeBuffer] used by the field to indicate if there is an error
|
81
|
+
#
|
82
|
+
# @return (see govuk_field)
|
83
|
+
|
84
|
+
def govuk_field_with_model(field, model, attribute, **govuk_field_options)
|
85
|
+
set_label_for_if_custom_id(field, govuk_field_options)
|
86
|
+
set_hint_id(attribute, govuk_field_options)
|
87
|
+
|
88
|
+
govuk_form_group_with_model(model, attribute, **(govuk_field_options[:form_group] || {})) do |display_error_message|
|
89
|
+
set_field_described_by(field, attribute, display_error_message, govuk_field_options)
|
90
|
+
|
91
|
+
capture do
|
92
|
+
concat(govuk_label(attribute, govuk_field_options[:label][:text], **govuk_field_options[:label]))
|
93
|
+
concat(govuk_hint(govuk_field_options[:hint][:text], **govuk_field_options[:hint])) if govuk_field_options[:hint]
|
94
|
+
concat(display_error_message)
|
95
|
+
yield(govuk_field_options[field], display_error_message)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Generates the HTML to wrap arround a GDS input field component using an ActionView::Helpers::FormBuilder.
|
101
|
+
# Unlike {govuk_field}, the method will be able to automatically determine if the error message needs to be shown.
|
102
|
+
#
|
103
|
+
# @param field [Symbol] the type of the field
|
104
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the label
|
105
|
+
# @param attribute [String, Symbol] the attribute of the field
|
106
|
+
# @param govuk_field_options [Hash] options that will be used for the parts of the form group, label, hint and field
|
107
|
+
#
|
108
|
+
# @option (see govuk_field)
|
109
|
+
#
|
110
|
+
# @yield (see govuk_field)
|
111
|
+
#
|
112
|
+
# @yieldparam (see govuk_field)
|
113
|
+
# @yieldparam error_message [ActiveSupport::SafeBuffer] used by the field to indicate if there is an error
|
114
|
+
#
|
115
|
+
# @return (see govuk_field)
|
116
|
+
|
117
|
+
def govuk_field_with_form(field, form, attribute, **govuk_field_options)
|
118
|
+
set_label_for_if_custom_id(field, govuk_field_options)
|
119
|
+
set_hint_id(attribute, govuk_field_options)
|
120
|
+
|
121
|
+
govuk_form_group_with_model(form.object, attribute, **(govuk_field_options[:form_group] || {})) do |display_error_message|
|
122
|
+
set_field_described_by(field, attribute, display_error_message, govuk_field_options)
|
123
|
+
|
124
|
+
capture do
|
125
|
+
concat(govuk_label_with_form(form, attribute, govuk_field_options[:label][:text], **(govuk_field_options[:label] || {})))
|
126
|
+
concat(govuk_hint(govuk_field_options[:hint][:text], **govuk_field_options[:hint])) if govuk_field_options[:hint]
|
127
|
+
concat(display_error_message)
|
128
|
+
yield(govuk_field_options[field], display_error_message)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# Generates the HTML to wrap arround a GDS input fields component.
|
134
|
+
# These are inputs that require being wrapped in a fieldset, for example radio buttons.
|
135
|
+
#
|
136
|
+
# @param field [Symbol] the type of the fields
|
137
|
+
# @param attribute [String, Symbol] the attribute of the fields
|
138
|
+
# @param error_message [String] the error message to be displayed
|
139
|
+
# @param govuk_fields_options [Hash] options that will be used for the parts of the fieldset, form group, hint and fields
|
140
|
+
#
|
141
|
+
# @option govuk_fields_options [Hash] :form_group ({}) the options for govuk_form_group {govuk_form_group}
|
142
|
+
# @option govuk_fields_options [Hash] :fieldset ({}) the options for govuk_fieldset {govuk_fieldset}
|
143
|
+
# @option govuk_fields_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field, see {govuk_hint}.
|
144
|
+
# If no hint is given then no hint will be rendered
|
145
|
+
# @option govuk_fields_options [Hash] :field_options ({}) the options that will be used when rendering the fields.
|
146
|
+
# For more details look at a module that uses +Field+.
|
147
|
+
#
|
148
|
+
# @yield the fields HTML
|
149
|
+
#
|
150
|
+
# @yieldparam govuk_fields_options [Hash] the HTML options used when rendering the fields
|
151
|
+
#
|
152
|
+
# @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the fields
|
153
|
+
|
154
|
+
def govuk_fields(field, attribute, error_message = nil, **govuk_fields_options)
|
155
|
+
set_hint_id(attribute, govuk_fields_options)
|
156
|
+
|
157
|
+
govuk_form_group(attribute, error_message, **(govuk_fields_options[:form_group] || {})) do |display_error_message|
|
158
|
+
set_field_described_by(:fieldset, attribute, error_message, govuk_fields_options)
|
159
|
+
(govuk_fields_options[field] ||= {})[:attributes] ||= {}
|
160
|
+
|
161
|
+
govuk_fieldset(**govuk_fields_options[:fieldset]) do
|
162
|
+
concat(capture do
|
163
|
+
concat(govuk_hint(govuk_fields_options[:hint][:text], **govuk_fields_options[:hint])) if govuk_fields_options[:hint]
|
164
|
+
concat(display_error_message)
|
165
|
+
yield(govuk_fields_options[field])
|
166
|
+
end)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# Generates the HTML to wrap arround a GDS input fields component using an ActiveModel.
|
172
|
+
# These are inputs that require being wrapped in a fieldset, for example radio buttons.
|
173
|
+
#
|
174
|
+
# @param field [Symbol] the type of the fields
|
175
|
+
# @param model [ActiveModel] model that will be used to find an error message
|
176
|
+
# @param attribute [String, Symbol] the attribute of the fields
|
177
|
+
# @param govuk_fields_options [Hash] options that will be used for the parts of the fieldset, form group, hint and fields
|
178
|
+
#
|
179
|
+
# @option (see govuk_fields)
|
180
|
+
#
|
181
|
+
# @yield (see govuk_fields)
|
182
|
+
#
|
183
|
+
# @yieldparam (see govuk_fields)
|
184
|
+
# @yieldparam any_errors flag to indicate if there are errors present
|
185
|
+
#
|
186
|
+
# @return (see govuk_fields)
|
187
|
+
|
188
|
+
def govuk_fields_with_model(field, model, attribute, **govuk_fields_options)
|
189
|
+
set_hint_id(attribute, govuk_fields_options)
|
190
|
+
|
191
|
+
govuk_form_group_with_model(model, attribute, **(govuk_fields_options[:form_group] || {})) do |display_error_message|
|
192
|
+
set_field_described_by(:fieldset, attribute, display_error_message, govuk_fields_options)
|
193
|
+
(govuk_fields_options[field] ||= {})[:attributes] ||= {}
|
194
|
+
|
195
|
+
govuk_fieldset(**govuk_fields_options[:fieldset]) do
|
196
|
+
concat(capture do
|
197
|
+
concat(govuk_hint(govuk_fields_options[:hint][:text], **govuk_fields_options[:hint])) if govuk_fields_options[:hint]
|
198
|
+
concat(display_error_message)
|
199
|
+
yield(govuk_fields_options[field], display_error_message)
|
200
|
+
end)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
# Generates the HTML to wrap arround a GDS input fields component using an ActionView::Helpers::FormBuilder.
|
206
|
+
# These are inputs that require being wrapped in a fieldset, for example radio buttons.
|
207
|
+
#
|
208
|
+
# Unlike {govuk_fields}, the method will be able to automatically determine if the error message needs to be shown.
|
209
|
+
#
|
210
|
+
# @param field [Symbol] the type of the fields
|
211
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the label
|
212
|
+
# @param attribute [String, Symbol] the attribute of the fields
|
213
|
+
# @param govuk_fields_options [Hash] options that will be used for the parts of the fieldset, form group, hint and fields
|
214
|
+
#
|
215
|
+
# @option (see govuk_fields)
|
216
|
+
#
|
217
|
+
# @yield (see govuk_fields)
|
218
|
+
#
|
219
|
+
# @yieldparam (see govuk_fields_with_model)
|
220
|
+
#
|
221
|
+
# @return (see govuk_fields)
|
222
|
+
|
223
|
+
def govuk_fields_with_form(field, form, attribute, **govuk_fields_options)
|
224
|
+
set_hint_id(attribute, govuk_fields_options)
|
225
|
+
|
226
|
+
govuk_form_group_with_model(form.object, attribute, **(govuk_fields_options[:form_group] || {})) do |display_error_message|
|
227
|
+
set_field_described_by(:fieldset, attribute, display_error_message, govuk_fields_options)
|
228
|
+
(govuk_fields_options[field] ||= {})[:attributes] ||= {}
|
229
|
+
|
230
|
+
govuk_fieldset(**govuk_fields_options[:fieldset]) do
|
231
|
+
concat(capture do
|
232
|
+
concat(govuk_hint(govuk_fields_options[:hint][:text], **govuk_fields_options[:hint])) if govuk_fields_options[:hint]
|
233
|
+
concat(display_error_message)
|
234
|
+
yield(govuk_fields_options[field], display_error_message)
|
235
|
+
end)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
private
|
241
|
+
|
242
|
+
# Sets the label +for+ if a custom ID has been given for the field.
|
243
|
+
#
|
244
|
+
# @param govuk_field_options [Hash] see {govuk_field} for details
|
245
|
+
|
246
|
+
def set_label_for_if_custom_id(field, govuk_field_options)
|
247
|
+
field_id = govuk_field_options.dig(field, :attributes, :id)
|
248
|
+
|
249
|
+
govuk_field_options[:label] ||= {}
|
250
|
+
(govuk_field_options[:label][:attributes] ||= {}).merge!({ for: field_id }) if field_id
|
251
|
+
end
|
252
|
+
|
253
|
+
# Sets the hint ID if there is a hint, and the ID for the hint has not been sent
|
254
|
+
#
|
255
|
+
# @param attribute [String, Symbol] the attribute of the field
|
256
|
+
# @param govuk_field_options [Hash] see {govuk_field} for details
|
257
|
+
|
258
|
+
def set_hint_id(attribute, govuk_field_options)
|
259
|
+
return if !govuk_field_options[:hint] || govuk_field_options.dig(:hint, :attributes, :id)
|
260
|
+
|
261
|
+
govuk_field_options[:hint] ||= {}
|
262
|
+
(govuk_field_options[:hint][:attributes] ||= {}).merge!({ id: "#{attribute}-hint" })
|
263
|
+
end
|
264
|
+
|
265
|
+
# Adds the aira-describedby attribute for the field
|
266
|
+
# if there is a hint or an error message
|
267
|
+
#
|
268
|
+
# @param attribute [String, Symbol] the attribute of the input
|
269
|
+
# @param error_message [String] used to indicate if there is an error
|
270
|
+
# @param govuk_field_options [Hash] see {#govuk_field} for details
|
271
|
+
|
272
|
+
def set_field_described_by(field, attribute, error_message, govuk_field_options)
|
273
|
+
aria_described_by = []
|
274
|
+
aria_described_by << govuk_field_options.dig(field, :attributes, :aria, :describedby)
|
275
|
+
aria_described_by << govuk_field_options[:hint][:attributes][:id] if govuk_field_options[:hint]
|
276
|
+
aria_described_by << "#{attribute}-error" if error_message
|
277
|
+
aria_described_by.compact!
|
278
|
+
|
279
|
+
govuk_field_options[field] ||= {}
|
280
|
+
govuk_field_options[field][:attributes] ||= {}
|
281
|
+
|
282
|
+
return unless aria_described_by.any?
|
283
|
+
|
284
|
+
(govuk_field_options[field][:attributes][:aria] ||= {}).merge!({ describedby: aria_described_by.join(' ') })
|
285
|
+
end
|
286
|
+
|
287
|
+
# Sets the hint classes and adds the aira-describedby attribute for a fields item
|
288
|
+
#
|
289
|
+
# @param type [String] the type of the item
|
290
|
+
# @param attribute [String, Symbol] the attribute of the item
|
291
|
+
# @param item [Hash] the options for that item
|
292
|
+
|
293
|
+
def set_item_options_for_hint(type, attribute, item)
|
294
|
+
(item[:hint] ||= {})[:attributes] ||= {}
|
295
|
+
item[:hint][:classes] = "govuk-#{type}__hint #{item[:hint][:classes]}".rstrip
|
296
|
+
item[:hint][:attributes][:id] ||= "#{attribute}_#{item[:value]}-item-hint"
|
297
|
+
|
298
|
+
(item[:attributes][:aria] ||= {})[:describedby] = item[:hint][:attributes][:id]
|
299
|
+
end
|
300
|
+
end
|
301
|
+
# rubocop:enable Metrics/ModuleLength
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'action_view'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module GovUkHelper
|
7
|
+
# = GOV.UK Fieldset
|
8
|
+
#
|
9
|
+
# This helper is used for generating the fieldset component from the
|
10
|
+
# {https://design-system.service.gov.uk/components/fieldset GDS - Components - Fieldset}
|
11
|
+
|
12
|
+
module Fieldset
|
13
|
+
include ActionView::Context
|
14
|
+
include ActionView::Helpers::TagHelper
|
15
|
+
include ActionView::Helpers::TextHelper
|
16
|
+
|
17
|
+
# Generates the HTML for the GOV.UK Fieldset component
|
18
|
+
#
|
19
|
+
# @param govuk_fieldset_options [Hash] options that will be used in customising the HTML
|
20
|
+
#
|
21
|
+
# @option govuk_fieldset_options [String] :classes additional CSS classes for the fieldset HTML
|
22
|
+
# @option govuk_fieldset_options [Hash] :legend options for the legend which are used in {#govuk_legend}
|
23
|
+
# @option govuk_fieldset_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
24
|
+
#
|
25
|
+
# @yield HTML that will be contained within the 'govuk-fieldset' div and under the legend
|
26
|
+
#
|
27
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Fieldset
|
28
|
+
# which can then be rendered on the page
|
29
|
+
|
30
|
+
def govuk_fieldset(**govuk_fieldset_options)
|
31
|
+
govuk_fieldset_classes = ['govuk-fieldset']
|
32
|
+
govuk_fieldset_classes << govuk_fieldset_options[:classes]
|
33
|
+
govuk_fieldset_options[:attributes] ||= {}
|
34
|
+
|
35
|
+
tag.fieldset(class: govuk_fieldset_classes, **govuk_fieldset_options[:attributes]) do
|
36
|
+
capture do
|
37
|
+
concat(govuk_legend(govuk_fieldset_options[:legend])) if govuk_fieldset_options[:legend]
|
38
|
+
yield
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
# Generates the HTML for the Legend as part of {#govuk_fieldset}
|
46
|
+
#
|
47
|
+
# @param govuk_legend_options [Hash] options that will be used in the legend
|
48
|
+
#
|
49
|
+
# @option govuk_legend_options [String] :classes additional CSS classes for the legend HTML
|
50
|
+
# @option govuk_legend_options [String] :text the text for the legend
|
51
|
+
# @option govuk_legend_options [boolean] :is_page_heading (false) if the legend is also the heading it will rendered in a h1
|
52
|
+
# @option govuk_legend_options [Hash] :caption an optional hash with the +text+ and +classes+ that will be used
|
53
|
+
# to render a caption before the h1 if the legend is a page heading
|
54
|
+
#
|
55
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Fieldset
|
56
|
+
# which can then be rendered on the page
|
57
|
+
|
58
|
+
def govuk_legend(govuk_legend_options)
|
59
|
+
govuk_legend_classes = ['govuk-fieldset__legend']
|
60
|
+
govuk_legend_classes << govuk_legend_options[:classes]
|
61
|
+
|
62
|
+
tag.legend(class: govuk_legend_classes) do
|
63
|
+
if govuk_legend_options[:is_page_heading]
|
64
|
+
capture do
|
65
|
+
concat(tag.span(govuk_legend_options[:caption][:text], class: govuk_legend_options[:caption][:classes])) if govuk_legend_options[:caption]
|
66
|
+
concat(tag.h1(govuk_legend_options[:text], class: 'govuk-fieldset__heading'))
|
67
|
+
end
|
68
|
+
else
|
69
|
+
govuk_legend_options[:text]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'action_view'
|
4
3
|
require_relative 'error_message'
|
5
4
|
|
6
5
|
module CrownMarketplaceUtils
|
@@ -10,8 +9,6 @@ module CrownMarketplaceUtils
|
|
10
9
|
# This helper is used for generating the form group component from the Government Design Systems
|
11
10
|
|
12
11
|
module FormGroup
|
13
|
-
include ActionView::Context
|
14
|
-
include ActionView::Helpers::TagHelper
|
15
12
|
include ErrorMessage
|
16
13
|
|
17
14
|
# Generates the HTML for the GOV.UK Form Group component
|