crown_marketplace_utils 0.1.0.beta.4 → 0.1.0.beta.6
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 +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
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils::Helpers
|
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 govuk_textarea_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
|
22
|
+
#
|
23
|
+
# @option govuk_textarea_options [String] :error_message (nil) the error message to be displayed
|
24
|
+
# @option govuk_textarea_options [ActiveModel] :model (nil) optional model that can be used to find an error message
|
25
|
+
# @option govuk_textarea_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
|
26
|
+
# the textarea tag and find the error message
|
27
|
+
# @option govuk_textarea_options [Hash] :form_group see {govuk_field}
|
28
|
+
# @option govuk_textarea_options [Hash] :label see {govuk_field}
|
29
|
+
# @option govuk_textarea_options [Hash] :hint see {govuk_field}
|
30
|
+
# @option govuk_textarea_options [Hash] :textarea ({}) the options that will be used when rendering the textarea.
|
31
|
+
# See {set_govuk_textarea_field_options} for more details.
|
32
|
+
#
|
33
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK textarea
|
34
|
+
# which can then be rendered on the page
|
35
|
+
|
36
|
+
def govuk_textarea(attribute, **govuk_textarea_options)
|
37
|
+
govuk_field(:textarea, attribute, **govuk_textarea_options) do |govuk_field_options, error_message|
|
38
|
+
set_govuk_textarea_field_options(error_message, govuk_field_options)
|
39
|
+
(govuk_textarea_options[:textarea] ||= {})[:content] = govuk_textarea_options[:model].send(attribute) if govuk_textarea_options[:model]
|
40
|
+
|
41
|
+
concat(
|
42
|
+
if govuk_textarea_options[:form]
|
43
|
+
govuk_textarea_form(govuk_textarea_options[:form], attribute, **govuk_field_options)
|
44
|
+
else
|
45
|
+
govuk_textarea_tag(attribute, **govuk_field_options)
|
46
|
+
end
|
47
|
+
)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
# Generates the textarea HTML for {govuk_textarea}
|
54
|
+
#
|
55
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
56
|
+
# @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
|
57
|
+
#
|
58
|
+
# @option (see set_govuk_textarea_field_options)
|
59
|
+
#
|
60
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the textarea field which is used in {govuk_textarea}
|
61
|
+
|
62
|
+
def govuk_textarea_tag(attribute, **govuk_text_textarea_options)
|
63
|
+
text_area_tag(
|
64
|
+
attribute,
|
65
|
+
govuk_text_textarea_options[:content],
|
66
|
+
**govuk_text_textarea_options[:attributes]
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Generates the textarea HTML for {govuk_textarea} when there is a ActionView::Helpers::FormBuilder
|
71
|
+
#
|
72
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the textarea
|
73
|
+
# @param (see govuk_textarea_tag)
|
74
|
+
#
|
75
|
+
# @option (see set_govuk_textarea_field_options)
|
76
|
+
#
|
77
|
+
# @return (see govuk_textarea_tag)
|
78
|
+
|
79
|
+
def govuk_textarea_form(form, attribute, **govuk_text_textarea_options)
|
80
|
+
form.text_area(
|
81
|
+
attribute,
|
82
|
+
**govuk_text_textarea_options[:attributes]
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Initialises the attributes for the textarea input
|
87
|
+
#
|
88
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
89
|
+
# @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
|
90
|
+
#
|
91
|
+
# @option govuk_text_textarea_options [String] :classes additional CSS classes for the textarea HTML
|
92
|
+
# @option govuk_text_textarea_options [String] :content (nil) the content of the textarea
|
93
|
+
# @option govuk_text_textarea_options [String] :rows (5) the number of rows for the text area
|
94
|
+
# @option govuk_text_textarea_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
95
|
+
|
96
|
+
def set_govuk_textarea_field_options(error_message, govuk_text_textarea_options)
|
97
|
+
govuk_text_textarea_options[:attributes] ||= {}
|
98
|
+
govuk_text_textarea_options[:attributes][:class] = "govuk-textarea #{govuk_text_textarea_options[:classes]}".rstrip
|
99
|
+
govuk_text_textarea_options[:attributes][:class] << ' govuk-textarea--error' if error_message
|
100
|
+
govuk_text_textarea_options[:attributes][:rows] ||= govuk_text_textarea_options[:rows] || 5
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,215 @@
|
|
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::Helpers
|
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 field [Symbol] the type of the field
|
32
|
+
# @param attribute [String, Symbol] the attribute of the field
|
33
|
+
# @param govuk_field_options [Hash] options that will be used for the parts of the form group, label, hint and field
|
34
|
+
#
|
35
|
+
# @option govuk_field_options [String] :error_message (nil) the error message to be displayed
|
36
|
+
# @option govuk_field_options [ActiveModel] :model (nil) optional model that can be used to find an error message
|
37
|
+
# @option govuk_field_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
|
38
|
+
# the field and find the error message
|
39
|
+
# @option govuk_field_options [Hash] :form_group ({}) the options for govuk_form_group {govuk_form_group}
|
40
|
+
# @option govuk_field_options [Hash] :label the parameters that will be used to create the label for the field, see {govuk_label}
|
41
|
+
# @option govuk_field_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field, see {govuk_hint}.
|
42
|
+
# If no hint is given then no hint will be rendered
|
43
|
+
# @option govuk_field_options [Hash] :field_options ({}) the options that will be used when rendering the field.
|
44
|
+
# For more details look at the specific module that uses +Field+.
|
45
|
+
#
|
46
|
+
# @yield the field HTML
|
47
|
+
#
|
48
|
+
# @yieldparam govuk_field_options [Hash] the HTML options used when rendering the field
|
49
|
+
# @yieldparam error_message [String] flag to indicate if there are any erros
|
50
|
+
#
|
51
|
+
# @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the field
|
52
|
+
|
53
|
+
def govuk_field(field, attribute, **govuk_field_options)
|
54
|
+
set_label_for_if_custom_id(field, govuk_field_options)
|
55
|
+
set_hint_id(attribute, govuk_field_options)
|
56
|
+
error_message = find_field_error_message(attribute, govuk_field_options)
|
57
|
+
|
58
|
+
govuk_form_group(attribute, error_message: error_message, **(govuk_field_options[:form_group] || {})) do |display_error_message|
|
59
|
+
set_field_described_by(field, attribute, error_message, govuk_field_options)
|
60
|
+
|
61
|
+
capture do
|
62
|
+
concat(govuk_label(attribute, govuk_field_options[:label][:text], form: govuk_field_options[:form], **govuk_field_options[:label]))
|
63
|
+
concat(govuk_hint(govuk_field_options[:hint][:text], **govuk_field_options[:hint])) if govuk_field_options[:hint]
|
64
|
+
concat(display_error_message)
|
65
|
+
yield(govuk_field_options[field], error_message)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Generates the HTML to wrap arround a GDS input fields component.
|
71
|
+
# These are inputs that require being wrapped in a fieldset, for example radio buttons.
|
72
|
+
#
|
73
|
+
# @param field [Symbol] the type of the fields
|
74
|
+
# @param attribute [String, Symbol] the attribute of the fields
|
75
|
+
# @param govuk_fields_options [Hash] options that will be used for the parts of the fieldset, form group, hint and fields
|
76
|
+
#
|
77
|
+
# @option govuk_fields_options [String] :error_message (nil) the error message to be displayed
|
78
|
+
# @option govuk_fields_options [ActiveModel] :model (nil) optional model that can be used to find an error message
|
79
|
+
# @option govuk_fields_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
|
80
|
+
# the field and find the error message
|
81
|
+
# @option govuk_fields_options [Hash] :form_group ({}) the options for govuk_form_group {govuk_form_group}
|
82
|
+
# @option govuk_fields_options [Hash] :fieldset ({}) the options for govuk_fieldset {govuk_fieldset}
|
83
|
+
# @option govuk_fields_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field, see {govuk_hint}.
|
84
|
+
# If no hint is given then no hint will be rendered
|
85
|
+
# @option govuk_fields_options [Hash] :field_options ({}) the options that will be used when rendering the fields.
|
86
|
+
# For more details look at a module that uses +Field+.
|
87
|
+
#
|
88
|
+
# @yield the fields HTML
|
89
|
+
#
|
90
|
+
# @yieldparam govuk_fields_options [Hash] the HTML options used when rendering the fields
|
91
|
+
# @yieldparam error_message [String] flag to indicate if there are any erros
|
92
|
+
#
|
93
|
+
# @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the fields
|
94
|
+
|
95
|
+
def govuk_fields(field, attribute, **govuk_fields_options)
|
96
|
+
set_hint_id(attribute, govuk_fields_options)
|
97
|
+
error_message = find_field_error_message(attribute, govuk_fields_options)
|
98
|
+
|
99
|
+
govuk_form_group(attribute, error_message: error_message, **(govuk_fields_options[:form_group] || {})) do |display_error_message|
|
100
|
+
set_field_described_by(:fieldset, attribute, error_message, govuk_fields_options)
|
101
|
+
(govuk_fields_options[field] ||= {})[:attributes] ||= {}
|
102
|
+
|
103
|
+
govuk_fieldset(**govuk_fields_options[:fieldset]) do
|
104
|
+
concat(capture do
|
105
|
+
concat(govuk_hint(govuk_fields_options[:hint][:text], **govuk_fields_options[:hint])) if govuk_fields_options[:hint]
|
106
|
+
concat(display_error_message)
|
107
|
+
yield(govuk_fields_options[field], error_message)
|
108
|
+
end)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
# If present, will find the error message for the field
|
116
|
+
#
|
117
|
+
# @param attribute [String, Symbol] the attribute of the fields
|
118
|
+
#
|
119
|
+
# @option govuk_field_options [String] :error_message (nil) the error message to be displayed
|
120
|
+
# @option govuk_field_options [ActiveModel] :model (nil) optional model that can be used to find an error message
|
121
|
+
# @option govuk_field_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
|
122
|
+
# the field and find the error message
|
123
|
+
#
|
124
|
+
# @return [String] the error message
|
125
|
+
|
126
|
+
def find_field_error_message(attribute, govuk_field_options)
|
127
|
+
if govuk_field_options[:model]
|
128
|
+
govuk_field_options[:model].errors[attribute].first
|
129
|
+
elsif govuk_field_options[:form]
|
130
|
+
govuk_field_options[:form].object.errors[attribute].first
|
131
|
+
else
|
132
|
+
govuk_field_options[:error_message]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Sets the label +for+ if a custom ID has been given for the field.
|
137
|
+
#
|
138
|
+
# @param govuk_field_options [Hash] see {govuk_field} for details
|
139
|
+
|
140
|
+
def set_label_for_if_custom_id(field, govuk_field_options)
|
141
|
+
field_id = govuk_field_options.dig(field, :attributes, :id)
|
142
|
+
|
143
|
+
govuk_field_options[:label] ||= {}
|
144
|
+
(govuk_field_options[:label][:attributes] ||= {}).merge!({ for: field_id }) if field_id
|
145
|
+
end
|
146
|
+
|
147
|
+
# Sets the hint ID if there is a hint, and the ID for the hint has not been sent
|
148
|
+
#
|
149
|
+
# @param attribute [String, Symbol] the attribute of the field
|
150
|
+
# @param govuk_field_options [Hash] see {govuk_field} for details
|
151
|
+
|
152
|
+
def set_hint_id(attribute, govuk_field_options)
|
153
|
+
return if !govuk_field_options[:hint] || govuk_field_options.dig(:hint, :attributes, :id)
|
154
|
+
|
155
|
+
govuk_field_options[:hint] ||= {}
|
156
|
+
(govuk_field_options[:hint][:attributes] ||= {}).merge!({ id: "#{attribute}-hint" })
|
157
|
+
end
|
158
|
+
|
159
|
+
# Adds the aira-describedby attribute for the field
|
160
|
+
# if there is a hint or an error message
|
161
|
+
#
|
162
|
+
# @param attribute [String, Symbol] the attribute of the input
|
163
|
+
# @param error_message [String] used to indicate if there is an error
|
164
|
+
# @param govuk_field_options [Hash] see {#govuk_field} for details
|
165
|
+
|
166
|
+
def set_field_described_by(field, attribute, error_message, govuk_field_options)
|
167
|
+
aria_described_by = []
|
168
|
+
aria_described_by << govuk_field_options.dig(field, :attributes, :aria, :describedby)
|
169
|
+
aria_described_by << govuk_field_options[:hint][:attributes][:id] if govuk_field_options[:hint]
|
170
|
+
aria_described_by << "#{attribute}-error" if error_message
|
171
|
+
aria_described_by.compact!
|
172
|
+
|
173
|
+
govuk_field_options[field] ||= {}
|
174
|
+
govuk_field_options[field][:attributes] ||= {}
|
175
|
+
|
176
|
+
return unless aria_described_by.any?
|
177
|
+
|
178
|
+
(govuk_field_options[field][:attributes][:aria] ||= {}).merge!({ describedby: aria_described_by.join(' ') })
|
179
|
+
end
|
180
|
+
|
181
|
+
# Sets the hint attributes and adds the aira-describedby attribute for a fields item
|
182
|
+
#
|
183
|
+
# @param type [String] the type of the item
|
184
|
+
# @param attribute [String, Symbol] the attribute of the item
|
185
|
+
# @param item [Hash] the options for that item
|
186
|
+
|
187
|
+
def set_item_options_for_hint(type, attribute, item)
|
188
|
+
return unless item[:hint]
|
189
|
+
|
190
|
+
(item[:hint] ||= {})[:attributes] ||= {}
|
191
|
+
item[:hint][:classes] = "govuk-#{type}__hint #{item[:hint][:classes]}".rstrip
|
192
|
+
item[:hint][:attributes][:id] ||= "#{attribute}_#{item[:value]}-item-hint"
|
193
|
+
|
194
|
+
(item[:attributes][:aria] ||= {})[:describedby] = item[:hint][:attributes][:id]
|
195
|
+
end
|
196
|
+
|
197
|
+
# Sets the conditional attributes and adds the data-aira-controls attribute for a fields item
|
198
|
+
#
|
199
|
+
# @param type [String] the type of the item
|
200
|
+
# @param attribute [String, Symbol] the attribute of the item
|
201
|
+
# @param item [Hash] the options for that item
|
202
|
+
|
203
|
+
def set_conditional_item_options(type, attribute, item)
|
204
|
+
return unless item[:conditional]
|
205
|
+
|
206
|
+
item[:conditional][:attributes] ||= {}
|
207
|
+
item[:conditional][:attributes][:class] = "govuk-#{type}__conditional #{"govuk-#{type}__conditional--hidden" unless item[:checked]}".rstrip
|
208
|
+
item[:conditional][:attributes][:id] ||= sanitize_to_id("#{attribute}_#{item[:value]}_conditional")
|
209
|
+
(item[:attributes][:data] ||= {})[:'aria-controls'] = item[:conditional][:attributes][:id]
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require_relative 'error_message'
|
4
4
|
|
5
|
-
module CrownMarketplaceUtils
|
5
|
+
module CrownMarketplaceUtils::Helpers
|
6
6
|
module GovUkHelper
|
7
7
|
# = GOV.UK FormGroup
|
8
8
|
#
|
@@ -14,10 +14,11 @@ module CrownMarketplaceUtils
|
|
14
14
|
# Generates the HTML for the GOV.UK Form Group component
|
15
15
|
#
|
16
16
|
# @param attribute [String, Symbol] the attribute that the form group is for
|
17
|
-
# @param error_message [String] the error message to be displayed
|
18
17
|
# @param govuk_form_group_options [Hash] options that will be used in customising the HTML
|
19
18
|
#
|
20
19
|
# @option govuk_form_group_options [String] :classes additional CSS classes for the form group HTML
|
20
|
+
# @option govuk_form_group_options [String] :error_message (nil) the error message to be displayed
|
21
|
+
# @option govuk_form_group_options [ActiveModel] :model (nil) model that will be used to find an error message
|
21
22
|
# @option govuk_form_group_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
22
23
|
#
|
23
24
|
# @yield HTML that will be contained within the 'govuk-form-group' div
|
@@ -27,7 +28,13 @@ module CrownMarketplaceUtils
|
|
27
28
|
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Form Group
|
28
29
|
# which can then be rendered on the page
|
29
30
|
|
30
|
-
def govuk_form_group(attribute,
|
31
|
+
def govuk_form_group(attribute, **govuk_form_group_options)
|
32
|
+
error_message = if govuk_form_group_options[:model]
|
33
|
+
model.errors[attribute].first
|
34
|
+
else
|
35
|
+
govuk_form_group_options[:error_message]
|
36
|
+
end
|
37
|
+
|
31
38
|
govuk_form_group_classes = ['govuk-form-group']
|
32
39
|
govuk_form_group_classes += ['govuk-form-group--error'] if error_message
|
33
40
|
govuk_form_group_classes << govuk_form_group_options[:classes]
|
@@ -37,24 +44,6 @@ module CrownMarketplaceUtils
|
|
37
44
|
yield((govuk_error_message(error_message, attribute) if error_message))
|
38
45
|
end
|
39
46
|
end
|
40
|
-
|
41
|
-
# Generates the HTML for the GOV.UK Form Group component using an ActiveModel
|
42
|
-
#
|
43
|
-
# @param model [ActiveModel] model that will be used to find an error message
|
44
|
-
# @param attribute [String, Symbol] the attribute that the form group is for
|
45
|
-
# @param govuk_form_group_options [Hash] options that will be used in customising the HTML
|
46
|
-
#
|
47
|
-
# @option (see #govuk_form_group)
|
48
|
-
#
|
49
|
-
# @yield (see #govuk_form_group)
|
50
|
-
#
|
51
|
-
# @yieldparam (see #govuk_form_group)
|
52
|
-
#
|
53
|
-
# @return (see #govuk_form_group)
|
54
|
-
|
55
|
-
def govuk_form_group_with_model(model, attribute, **govuk_form_group_options, &block)
|
56
|
-
govuk_form_group(attribute, model.errors[attribute].first, **govuk_form_group_options, &block)
|
57
|
-
end
|
58
47
|
end
|
59
48
|
end
|
60
49
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'action_view'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils::Helpers
|
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 [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create the label
|
26
|
+
# @option govuk_label_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
27
|
+
#
|
28
|
+
# @yield HTML that will be contained within the 'govuk-label' label. Ignored if label text is given
|
29
|
+
#
|
30
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Label
|
31
|
+
# which can then be rendered on the page
|
32
|
+
|
33
|
+
def govuk_label(attribute, label_text = nil, **govuk_label_options, &block)
|
34
|
+
govuk_label_options[:attributes] ||= {}
|
35
|
+
govuk_label_options[:attributes][:class] = "govuk-label #{govuk_label_options[:classes]}".rstrip
|
36
|
+
|
37
|
+
label_html = if govuk_label_options[:form]
|
38
|
+
govuk_label_form(attribute, label_text, **govuk_label_options, &block)
|
39
|
+
else
|
40
|
+
govuk_label_tag(attribute, label_text, **govuk_label_options, &block)
|
41
|
+
end
|
42
|
+
|
43
|
+
if govuk_label_options[:is_page_heading]
|
44
|
+
tag.h1(label_html, class: 'govuk-label-wrapper')
|
45
|
+
else
|
46
|
+
label_html
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# Generates the HTML for the GOV.UK label component
|
53
|
+
# using the inbuilt rails method +label+
|
54
|
+
#
|
55
|
+
# @param (see govuk_label)
|
56
|
+
#
|
57
|
+
# @option (see govuk_label)
|
58
|
+
#
|
59
|
+
# @yield (see govuk_label)
|
60
|
+
#
|
61
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Label
|
62
|
+
# which can then be rendered on the page
|
63
|
+
|
64
|
+
def govuk_label_tag(attribute, label_text, **govuk_label_options)
|
65
|
+
label_tag(attribute, **govuk_label_options[:attributes]) do
|
66
|
+
label_text || yield
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Generates the HTML for the GOV.UK label component
|
71
|
+
# using the inbuilt rails ActionView::Helpers::FormBuilder
|
72
|
+
#
|
73
|
+
# @param (see govuk_label)
|
74
|
+
#
|
75
|
+
# @option (see govuk_label)
|
76
|
+
#
|
77
|
+
# @yield (see govuk_label)
|
78
|
+
#
|
79
|
+
# @return (see govuk_label_tag)
|
80
|
+
|
81
|
+
def govuk_label_form(attribute, label_text, **govuk_label_options)
|
82
|
+
govuk_label_options[:form].label(attribute, **govuk_label_options[:attributes]) do
|
83
|
+
label_text || yield
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|