crown_marketplace_utils 0.1.0.beta.4 → 0.1.0.beta.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/Gemfile.lock +11 -11
- data/README.md +1 -3
- 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 +5 -5
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/button.rb +11 -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 +25 -50
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/checkboxes.rb +52 -68
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/date_input.rb +46 -127
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/file_upload.rb +94 -0
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/input.rb +34 -72
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/radios.rb +52 -68
- data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/select.rb +28 -64
- 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 +1 -1
- 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 +1 -1
- 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} +7 -1
- data/lib/crown_marketplace_utils/version.rb +1 -1
- data/lib/crown_marketplace_utils.rb +1 -1
- metadata +28 -25
- data/lib/crown_marketplace_utils/gov_uk_helper/field/file_upload.rb +0 -125
- data/lib/crown_marketplace_utils/gov_uk_helper/field/textarea.rb +0 -140
- data/lib/crown_marketplace_utils/gov_uk_helper/field.rb +0 -305
- 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,140 +0,0 @@
|
|
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
|
@@ -1,305 +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
|
-
# 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 }
|
@@ -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] 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
|