crown_marketplace_utils 0.1.0.beta.1 → 0.1.0.beta.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +0 -1
- data/.rubocop.yml +14 -0
- data/Gemfile +0 -4
- data/Gemfile.lock +45 -29
- data/README.md +44 -1
- data/crown_marketplace_utils.gemspec +11 -8
- data/lib/crown_marketplace_utils/gov_uk_helper/breadcrumbs.rb +76 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/button.rb +130 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/details.rb +14 -8
- data/lib/crown_marketplace_utils/gov_uk_helper/error_message.rb +18 -11
- data/lib/crown_marketplace_utils/gov_uk_helper/field/character_count.rb +193 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/checkboxes.rb +209 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/input.rb +160 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/radios.rb +205 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/select.rb +166 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/textarea.rb +127 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field.rb +263 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/fieldset.rb +75 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/form_group.rb +60 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/header.rb +172 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/hint.rb +12 -6
- 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 +37 -2
- data/lib/crown_marketplace_utils/version.rb +1 -1
- data/lib/crown_marketplace_utils.rb +6 -1
- metadata +75 -31
- data/lib/crown_marketplace_utils/engine.rb +0 -11
@@ -0,0 +1,193 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'textarea'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module GovUkHelper
|
7
|
+
module Field
|
8
|
+
# = GOV.UK Character count
|
9
|
+
#
|
10
|
+
# This helper is used for generating the character count component from the
|
11
|
+
# {https://design-system.service.gov.uk/components/character-count GDS - Components - Character count}
|
12
|
+
#
|
13
|
+
# This is a wrapper around a Textarea module and so makes use of the methods in {Textarea}
|
14
|
+
|
15
|
+
module CharacterCount
|
16
|
+
include Textarea
|
17
|
+
|
18
|
+
# Generates the HTML for the GOV.UK character count component.
|
19
|
+
# It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
|
20
|
+
#
|
21
|
+
# @param attribute [String, Symbol] the attribute of the character count text area
|
22
|
+
# @param error_message [String] the error message to be displayed
|
23
|
+
# @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint, textarea and the character count
|
24
|
+
#
|
25
|
+
# @option govuk_character_count_options [Hash] :form_group_options see {govuk_field}
|
26
|
+
# @option govuk_character_count_options [Hash] :label see {govuk_field}
|
27
|
+
# @option govuk_character_count_options [Hash] :hint see {govuk_field}
|
28
|
+
# @option govuk_character_count_options [Hash] :textarea_options see {govuk_textarea}
|
29
|
+
# @option govuk_character_count_options [Hash] :character_count_options ({}) the options that will be used when rendering the textarea.
|
30
|
+
# See {_govuk_character_count} for more details.
|
31
|
+
#
|
32
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
|
33
|
+
# which can then be rendered on the page
|
34
|
+
|
35
|
+
def govuk_character_count(attribute, error_message = nil, **govuk_character_count_options)
|
36
|
+
_govuk_character_count(attribute, **govuk_character_count_options) do |govuk_text_textarea_options|
|
37
|
+
govuk_textarea(attribute, error_message, **govuk_text_textarea_options)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Generates the HTML for the GOV.UK character count component.
|
42
|
+
# It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
|
43
|
+
#
|
44
|
+
# @param attribute [String, Symbol] the attribute of the character count
|
45
|
+
# @param error_message [String] the error message to be displayed
|
46
|
+
# @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
|
47
|
+
#
|
48
|
+
# @option govuk_character_count_options [Hash] :form_group_options see {govuk_field}
|
49
|
+
# @option govuk_character_count_options [Hash] :label see {govuk_field}
|
50
|
+
# @option govuk_character_count_options [Hash] :hint see {govuk_field}
|
51
|
+
# @option govuk_character_count_options [Hash] :textarea_options see {govuk_textarea}
|
52
|
+
# @option govuk_character_count_options [Hash] :character_count_options ({}) the options that will be used when rendering the textarea.
|
53
|
+
# See {_govuk_character_count} for more details.
|
54
|
+
#
|
55
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
|
56
|
+
# which can then be rendered on the page
|
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] :form the form builder used to create the character count textarea
|
62
|
+
# @param attribute [String, Symbol] the attribute of the character count text area
|
63
|
+
# @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint, textarea and the character count
|
64
|
+
#
|
65
|
+
# @option govuk_character_count_options [Hash] :form_group_options see {govuk_field_with_form}
|
66
|
+
# @option govuk_character_count_options [Hash] :label see {govuk_field_with_form}
|
67
|
+
# @option govuk_character_count_options [Hash] :hint see {govuk_field_with_form}
|
68
|
+
# @option govuk_character_count_options [Hash] :textarea_options see {govuk_textarea_with_form}
|
69
|
+
# @option govuk_character_count_options [Hash] :character_count_options ({}) the options that will be used when rendering the textarea.
|
70
|
+
# See {_govuk_character_count} for more details.
|
71
|
+
#
|
72
|
+
# @return (see govuk_character_count)
|
73
|
+
|
74
|
+
def govuk_character_count_with_form(form, attribute, **govuk_character_count_options)
|
75
|
+
_govuk_character_count(attribute, **govuk_character_count_options) do |govuk_text_textarea_options|
|
76
|
+
govuk_textarea_with_form(form, attribute, **govuk_text_textarea_options)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
# Wrapper method used by {govuk_character_count} and {govuk_character_count_with_form} to generate the character count HTML
|
83
|
+
#
|
84
|
+
# @param attribute [String, Symbol] the attribute of the character count
|
85
|
+
# @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
|
86
|
+
# This includes everything described in {govuk_character_count} and {govuk_character_count_with_form}
|
87
|
+
# with the addition of the +:character_count_options+ which are described below
|
88
|
+
#
|
89
|
+
# @option govuk_character_count_options [String] :maxlength (required) - if +maxwords+ is set, this is not required.
|
90
|
+
# The maximum number of characters.
|
91
|
+
# If +maxwords+ is provided, the +maxlength+ option will be ignored.
|
92
|
+
# @option govuk_character_count_options [String] :maxwords (required) - if maxlength is set, this is not required.
|
93
|
+
# The maximum number of words.
|
94
|
+
# If +maxwords+ is provided, the +maxlength+ option will be ignored.
|
95
|
+
# @option govuk_character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed.
|
96
|
+
# If this attribute is set, the count message will be hidden by default.
|
97
|
+
# @option govuk_text_textarea_options [Hash] :fallback_hint ({}) additional parameters that will be used to create the hint containing the character count text.
|
98
|
+
# This includes all the options in {govuk_hint} plus +:count_message+.
|
99
|
+
# This will replace the default text for the count message.
|
100
|
+
# If you want the count number to appear, put %<count>s in the string and it will be replaced with the number
|
101
|
+
#
|
102
|
+
# @yield the textarea HTML generated by {govuk_character_count} or {govuk_character_count_with_form}
|
103
|
+
#
|
104
|
+
# @yieldparam govuk_text_textarea_options [Hash] the options used in the textarea called by {govuk_character_count} or {govuk_character_count_with_form}
|
105
|
+
#
|
106
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the chracter count which wrpas arround {govuk_character_count} or {govuk_character_count_with_form}
|
107
|
+
|
108
|
+
def _govuk_character_count(attribute, **govuk_character_count_options)
|
109
|
+
deep_init_hash(govuk_character_count_options, :textarea_options, :attributes, :aria)
|
110
|
+
govuk_character_count_options[:textarea_options][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea_options, :attributes, :aria, :describedby), "#{attribute}-hint-info"].compact.join(' ')
|
111
|
+
|
112
|
+
govuk_character_count_options[:textarea_options][:classes] = [govuk_character_count_options[:textarea_options][:classes], 'govuk-js-character-count'].compact
|
113
|
+
|
114
|
+
tag.div(class: 'govuk-character-count', **get_character_count_attributes(**govuk_character_count_options)) do
|
115
|
+
capture do
|
116
|
+
concat(yield(govuk_character_count_options))
|
117
|
+
concat(character_count_hint(attribute, **govuk_character_count_options))
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Generates the fallback hint HTML for {_govuk_character_count}
|
123
|
+
#
|
124
|
+
# @param (see _govuk_character_count)
|
125
|
+
#
|
126
|
+
# @option (see _govuk_character_count)
|
127
|
+
#
|
128
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the fullback hint used in {_govuk_character_count}
|
129
|
+
|
130
|
+
def character_count_hint(attribute, **govuk_character_count_options)
|
131
|
+
fallback_hint_length = govuk_character_count_options[:character_count_options][:maxwords] || govuk_character_count_options[:character_count_options][:maxlength]
|
132
|
+
fallback_hint_default = "You can enter up to %<count>s #{govuk_character_count_options[:character_count_options][:maxwords] ? 'words' : 'characters'}"
|
133
|
+
|
134
|
+
deep_init_hash(govuk_character_count_options, :character_count_options, :fallback_hint, :attributes)
|
135
|
+
|
136
|
+
fallback_hint_text = format(govuk_character_count_options[:character_count_options][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
|
137
|
+
|
138
|
+
govuk_character_count_options[:character_count_options][:fallback_hint][:classes] = [govuk_character_count_options.dig(:character_count_options, :fallback_hint, :classes), 'govuk-character-count__message']
|
139
|
+
govuk_character_count_options[:character_count_options][:fallback_hint][:attributes].merge!(id: "#{attribute}-hint-info")
|
140
|
+
|
141
|
+
govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count_options][:fallback_hint])
|
142
|
+
end
|
143
|
+
|
144
|
+
# Generates a hash with the character count attributes used in {_govuk_character_count}
|
145
|
+
#
|
146
|
+
# @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
|
147
|
+
# This includes everything described in {govuk_character_count} and {govuk_character_count_with_form}
|
148
|
+
# with the addition of the +:character_count_options+ which are described below
|
149
|
+
#
|
150
|
+
# @option (see _govuk_character_count)
|
151
|
+
#
|
152
|
+
# @return [Hash] contains the HTMl attributes used in {_govuk_character_count}
|
153
|
+
|
154
|
+
def get_character_count_attributes(**govuk_character_count_options)
|
155
|
+
govuk_character_count_attributes = { data: { module: 'govuk-character-count' } }
|
156
|
+
|
157
|
+
%i[maxlength threshold maxwords].each do |data_attribute|
|
158
|
+
govuk_character_count_attributes[:data][data_attribute] = govuk_character_count_options[:character_count_options][data_attribute].to_s if govuk_character_count_options[:character_count_options][data_attribute]
|
159
|
+
end
|
160
|
+
|
161
|
+
govuk_character_count_attributes
|
162
|
+
end
|
163
|
+
|
164
|
+
# Method to initialise hashes within hashes if it is not already initialised
|
165
|
+
#
|
166
|
+
# @param hash [Hash] the hash that is going to be initialised
|
167
|
+
# @param keys [Array] the keys that are going to be initialised in the hash
|
168
|
+
#
|
169
|
+
# @example When the hash is completely empty
|
170
|
+
# hash = { }
|
171
|
+
# keys = [:a, :b, :c]
|
172
|
+
#
|
173
|
+
# deep_init_hash(hash, *keys)
|
174
|
+
#
|
175
|
+
# hash = { a: { b: { c: { } } } }
|
176
|
+
#
|
177
|
+
# @example When the hash has already been initialised
|
178
|
+
# hash = { a: { b: { d: 'hello' } } }
|
179
|
+
# keys = [:a, :b, :c]
|
180
|
+
#
|
181
|
+
# deep_init_hash(hash, *keys)
|
182
|
+
#
|
183
|
+
# hash = { a: { b: { d: 'hello', c: { } } } }
|
184
|
+
|
185
|
+
def deep_init_hash(hash, *keys)
|
186
|
+
current_hash = hash
|
187
|
+
|
188
|
+
keys.each { |key| current_hash = (current_hash[key] ||= {}) }
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
@@ -0,0 +1,209 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module GovUkHelper
|
7
|
+
module Field
|
8
|
+
# = GOV.UK Checkboxes
|
9
|
+
#
|
10
|
+
# This helper is used for generating the checkboxes component from the
|
11
|
+
# {https://design-system.service.gov.uk/components/checkboxes GDS - Components - Checkboxes}
|
12
|
+
#
|
13
|
+
# This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
|
14
|
+
|
15
|
+
module Checkboxes
|
16
|
+
include Field
|
17
|
+
|
18
|
+
# Generates the HTML for the GOV.UK Checkboxes component
|
19
|
+
#
|
20
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
21
|
+
# @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
|
22
|
+
# @param error_message [String] the error message to be displayed
|
23
|
+
# @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
|
24
|
+
#
|
25
|
+
# @option govuk_checkboxes_options [Hash] :form_group_options see {govuk_fields}
|
26
|
+
# @option govuk_checkboxes_options [Hash] :fieldset_options see {govuk_fields}
|
27
|
+
# @option govuk_checkboxes_options [Hash] :hint see {govuk_field}
|
28
|
+
# @option govuk_checkboxes_options [Hash] :checkboxes_options ({}) the options that will be used when rendering the checkbox buttons.
|
29
|
+
# See {govuk_checkboxes_fields} for more details.
|
30
|
+
#
|
31
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Checkboxes
|
32
|
+
# which can then be rendered on the page
|
33
|
+
|
34
|
+
def govuk_checkboxes(attribute, items, error_message = nil, **govuk_checkboxes_options)
|
35
|
+
govuk_fields(:checkboxes, attribute, error_message, **govuk_checkboxes_options) do |govuk_field_options|
|
36
|
+
concat(govuk_checkboxes_fields(attribute, items, **govuk_field_options))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Generates the HTML for the GOV.UK Checkboxes component using an ActionView::Helpers::FormBuilder.
|
41
|
+
# Unlike {govuk_checkboxes}, the method will be able to automatically determine if the error message needs to be shown.
|
42
|
+
#
|
43
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
|
44
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
45
|
+
# @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
|
46
|
+
# @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
|
47
|
+
#
|
48
|
+
# @option govuk_checkboxes_options [Hash] :form_group_options see {govuk_fields_with_form}
|
49
|
+
# @option govuk_checkboxes_options [Hash] :fieldset_options see {govuk_fields_with_form}
|
50
|
+
# @option govuk_checkboxes_options [Hash] :hint see {govuk_fields_with_form}
|
51
|
+
# @option govuk_checkboxes_options [Hash] :checkboxes_options ({}) the options that will be used when rendering the checkbox buttons.
|
52
|
+
# See {govuk_checkboxes_fields_with_form} for more details.
|
53
|
+
#
|
54
|
+
# @return (see govuk_checkboxes)
|
55
|
+
|
56
|
+
def govuk_checkboxes_with_form(form, attribute, items, **govuk_checkboxes_options)
|
57
|
+
govuk_fields_with_form(:checkboxes, form, attribute, **govuk_checkboxes_options) do |govuk_field_options|
|
58
|
+
concat(govuk_checkboxes_fields_with_form(form, attribute, items, **govuk_field_options))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
# Generates the checkboxes HTML for {govuk_checkboxes}
|
65
|
+
#
|
66
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
67
|
+
# @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
|
68
|
+
# @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
|
69
|
+
#
|
70
|
+
# @option (see _govuk_checkboxes_fields)
|
71
|
+
#
|
72
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes}
|
73
|
+
|
74
|
+
def govuk_checkboxes_fields(attribute, items, **govuk_checkboxes_options)
|
75
|
+
_govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
|
76
|
+
govuk_checkbox_item(attribute, checkbox_item)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Generates the checkboxes HTML for {govuk_checkboxes_with_form}
|
81
|
+
#
|
82
|
+
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the checkbox buttons
|
83
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
84
|
+
# @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
|
85
|
+
# @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
|
86
|
+
#
|
87
|
+
# @option (see _govuk_checkboxes_fields)
|
88
|
+
#
|
89
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_with_form}
|
90
|
+
|
91
|
+
def govuk_checkboxes_fields_with_form(form, attribute, items, **govuk_checkboxes_options)
|
92
|
+
_govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
|
93
|
+
govuk_checkbox_item_with_form(form, attribute, checkbox_item)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Wrapper method used by {govuk_checkboxes_fields} and {govuk_checkboxes_fields_with_form} to generate the checkboxes HTML
|
98
|
+
#
|
99
|
+
# @param items [Array] array of checkbox items.
|
100
|
+
# Each item is a hash which can be:
|
101
|
+
# - +:divider+ text to separate checkbox items
|
102
|
+
# - checkbox item, see {_govuk_checkbox_item}
|
103
|
+
# @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
|
104
|
+
#
|
105
|
+
# @option govuk_checkboxes_options [String] :classes additional CSS classes for the checkboxes HTML
|
106
|
+
# @option govuk_checkboxes_options [Hash] :attributes ({ module: 'govuk-checkboxes' }) any additional attributes that will added as part of the HTML
|
107
|
+
#
|
108
|
+
# @yield the checkbox item HTML generated by {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
|
109
|
+
#
|
110
|
+
# @yieldparam checkbox_item [Hash] the current checkbox item to be rendered
|
111
|
+
#
|
112
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
|
113
|
+
|
114
|
+
def _govuk_checkboxes_fields(items, **govuk_checkboxes_options)
|
115
|
+
govuk_checkboxes_classes = ['govuk-checkboxes']
|
116
|
+
govuk_checkboxes_classes << govuk_checkboxes_options[:classes]
|
117
|
+
((govuk_checkboxes_options[:attributes] ||= {})[:data] ||= {}).merge!({ module: 'govuk-checkboxes' })
|
118
|
+
|
119
|
+
tag.div(class: govuk_checkboxes_classes, **govuk_checkboxes_options[:attributes]) do
|
120
|
+
capture do
|
121
|
+
items.each do |checkbox_item|
|
122
|
+
concat(
|
123
|
+
if checkbox_item[:divider]
|
124
|
+
tag.div(checkbox_item[:divider], class: 'govuk-checkboxes__divider')
|
125
|
+
else
|
126
|
+
tag.div(class: 'govuk-checkboxes__item') do
|
127
|
+
yield(checkbox_item)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Generates the HTML for a checkbox button for {govuk_checkboxes_fields_with_form}
|
137
|
+
#
|
138
|
+
# @param (see _govuk_checkbox_item)
|
139
|
+
#
|
140
|
+
# @option (see _govuk_checkbox_item)
|
141
|
+
#
|
142
|
+
# @return (see _govuk_checkbox_item)
|
143
|
+
|
144
|
+
def govuk_checkbox_item(attribute, checkbox_item)
|
145
|
+
_govuk_checkbox_item(attribute, checkbox_item) do
|
146
|
+
checkbox_item[:attributes][:id] ||= "#{sanitize_to_id(attribute)}_#{sanitize_to_id(checkbox_item[:value])}"
|
147
|
+
|
148
|
+
concat(check_box_tag("#{attribute}[]", checkbox_item[:value], checkbox_item[:checked], class: 'govuk-checkboxes__input', **checkbox_item[:attributes]))
|
149
|
+
concat(govuk_label(checkbox_item[:attributes][:id], checkbox_item[:label][:text], **checkbox_item[:label][:options]))
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# rubocop:disable Metrics/AbcSize
|
154
|
+
|
155
|
+
# Generates the HTML for a checkbox button for {govuk_checkboxes_fields}
|
156
|
+
#
|
157
|
+
# @param (see _govuk_checkbox_item)
|
158
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
|
159
|
+
#
|
160
|
+
# @option (see _govuk_checkbox_item)
|
161
|
+
#
|
162
|
+
# @return (see _govuk_checkbox_item)
|
163
|
+
|
164
|
+
def govuk_checkbox_item_with_form(form, attribute, checkbox_item)
|
165
|
+
_govuk_checkbox_item(attribute, checkbox_item) do
|
166
|
+
(checkbox_item[:label][:options][:attributes] ||= {})[:value] = checkbox_item[:value]
|
167
|
+
checkbox_item[:label][:options][:attributes][:for] = checkbox_item[:attributes][:id] if checkbox_item[:attributes][:id]
|
168
|
+
|
169
|
+
concat(form.check_box(attribute, checkbox_item[:attributes].merge({ class: 'govuk-checkboxes__input', multiple: true, include_hidden: false }), checkbox_item[:value]))
|
170
|
+
concat(govuk_label_with_form(form, attribute, checkbox_item[:label][:text], **checkbox_item[:label][:options]))
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# Wrapper method used by {govuk_checkbox_item} and {govuk_checkbox_item_with_form} to generate the checkboxes HTML
|
175
|
+
# including the label and hint, if there is one.
|
176
|
+
#
|
177
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
178
|
+
# @param checkbox_item [Hash] the options for the checkbox item
|
179
|
+
#
|
180
|
+
# @option checkbox_item [String] :classes additional CSS classes for the checkbox button HTML
|
181
|
+
# @option checkbox_item [Hash] :label the parameters that will be used to create the label for the checkbox button:
|
182
|
+
# - +:text+ (required) - the label text
|
183
|
+
# - +:options+ - default: { } - the options for govuk_label {govuk_label}
|
184
|
+
# @option checkbox_item [Hash] :hint (nil) the parameters that will be used to create the hint for the field
|
185
|
+
# - +:text+ (required) - the hint text
|
186
|
+
# - +:options+ - default: { } - the options for govuk_hint {govuk_hint}
|
187
|
+
# @option checkbox_item [Hash] :attributes ({}) any additional attributes that will be added as part of the checkbox button HTML
|
188
|
+
#
|
189
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons, label and hint
|
190
|
+
# which is used in {govuk_checkbox_item} and {govuk_checkbox_item_with_form}
|
191
|
+
|
192
|
+
# rubocop:enable Metrics/AbcSize
|
193
|
+
|
194
|
+
def _govuk_checkbox_item(attribute, checkbox_item)
|
195
|
+
checkbox_item[:attributes] ||= {}
|
196
|
+
checkbox_item[:label][:options] ||= {}
|
197
|
+
checkbox_item[:label][:options][:classes] = "govuk-checkboxes__label #{checkbox_item[:label][:options][:classes]}".rstrip
|
198
|
+
|
199
|
+
set_item_options_for_hint('checkboxes', attribute, checkbox_item) if checkbox_item[:hint]
|
200
|
+
|
201
|
+
capture do
|
202
|
+
yield
|
203
|
+
concat(govuk_hint(checkbox_item[:hint][:text], **checkbox_item[:hint][:options])) if checkbox_item[:hint]
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module GovUkHelper
|
7
|
+
module Field
|
8
|
+
# = GOV.UK Input
|
9
|
+
#
|
10
|
+
# This helper is used for generating the input component from the
|
11
|
+
# {https://design-system.service.gov.uk/components/text-input GDS - Components - Text Input}
|
12
|
+
#
|
13
|
+
# This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
|
14
|
+
|
15
|
+
module Input
|
16
|
+
include Field
|
17
|
+
|
18
|
+
# Generates the HTML for the GOV.UK input component
|
19
|
+
#
|
20
|
+
# @param attribute [String, Symbol] the attribute of the input
|
21
|
+
# @param error_message [String] the error message to be displayed
|
22
|
+
# @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
|
23
|
+
#
|
24
|
+
# @option govuk_input_options [Hash] :form_group_options see {govuk_field}
|
25
|
+
# @option govuk_input_options [Hash] :label see {govuk_field}
|
26
|
+
# @option govuk_input_options [Hash] :hint see {govuk_field}
|
27
|
+
# @option govuk_input_options [Hash] :input_options ({}) the options that will be used when rendering the input.
|
28
|
+
# See {govuk_input_field} for more details.
|
29
|
+
#
|
30
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Input
|
31
|
+
# which can then be rendered on the page
|
32
|
+
|
33
|
+
def govuk_input(attribute, error_message = nil, **govuk_input_options)
|
34
|
+
govuk_field(:input, attribute, error_message, **govuk_input_options) do |govuk_field_options|
|
35
|
+
concat(govuk_input_field(attribute, error_message, **govuk_field_options))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Generates the HTML for the GOV.UK input component using an ActionView::Helpers::FormBuilder.
|
40
|
+
# Unlike {govuk_input}, the method will be able to automatically determine if the error message needs to be shown.
|
41
|
+
#
|
42
|
+
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the input
|
43
|
+
# @param attribute [String, Symbol] the attribute of the input
|
44
|
+
# @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
|
45
|
+
#
|
46
|
+
# @option govuk_input_options [Hash] :form_group_options see {govuk_field_with_form}
|
47
|
+
# @option govuk_input_options [Hash] :label see {govuk_field_with_form}
|
48
|
+
# @option govuk_input_options [Hash] :hint see {govuk_field_with_form}
|
49
|
+
# @option govuk_input_options [Hash] :input_options ({}) the options that will be used when rendering the input.
|
50
|
+
# See {govuk_input_field_with_form} for more details.
|
51
|
+
#
|
52
|
+
# @return (see govuk_input)
|
53
|
+
|
54
|
+
def govuk_input_with_form(form, attribute, **govuk_input_options)
|
55
|
+
govuk_field_with_form(:input, form, attribute, **govuk_input_options) do |govuk_field_options, error_message|
|
56
|
+
concat(govuk_input_field_with_form(form, attribute, error_message, **govuk_field_options))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# Generates the input HTML for {govuk_input}
|
63
|
+
#
|
64
|
+
# @param attribute [String, Symbol] the attribute of the input
|
65
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
66
|
+
# @param govuk_text_input_options [Hash] options that will be used in customising the HTML
|
67
|
+
#
|
68
|
+
# @option (see _govuk_input_field)
|
69
|
+
#
|
70
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input}
|
71
|
+
|
72
|
+
def govuk_input_field(attribute, error_message, **govuk_text_input_options)
|
73
|
+
_govuk_input_field(error_message, **govuk_text_input_options) do |govuk_text_input_classes, govuk_text_input_attributes|
|
74
|
+
text_field_tag(attribute, govuk_text_input_options[:value], class: govuk_text_input_classes, **govuk_text_input_attributes)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Generates the input HTML for {govuk_input_with_form}
|
79
|
+
#
|
80
|
+
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the input
|
81
|
+
# @param attribute [String, Symbol] the attribute of the input
|
82
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
83
|
+
# @param govuk_text_input_options [Hash] options that will be used in customising the HTML
|
84
|
+
#
|
85
|
+
# @option (see _govuk_input_field)
|
86
|
+
#
|
87
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_with_form}
|
88
|
+
|
89
|
+
def govuk_input_field_with_form(form, attribute, error_message, **govuk_text_input_options)
|
90
|
+
_govuk_input_field(error_message, **govuk_text_input_options) do |govuk_text_input_classes, govuk_text_input_attributes|
|
91
|
+
form.text_field(attribute, class: govuk_text_input_classes, **govuk_text_input_attributes)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Wrapper method used by {govuk_input_field} and {govuk_input_field_with_form} to generate the Input HTML
|
96
|
+
#
|
97
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
98
|
+
# @param govuk_text_input_options [Hash] options that will be used in customising the HTML
|
99
|
+
#
|
100
|
+
# @option govuk_text_input_options [String] :classes additional CSS classes for the input HTML
|
101
|
+
# @option govuk_text_input_options [String] :value (nil) the value of the input
|
102
|
+
# @option govuk_text_input_options [Hash] :prefix (nil) optional prefix for the input field. See {govuk_fix} for more details.
|
103
|
+
# @option govuk_text_input_options [Hash] :suffix (nil) optional suffix for the input field. See {govuk_fix} for more details.
|
104
|
+
# @option govuk_text_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
105
|
+
#
|
106
|
+
# @yield the input HTML generated by {govuk_input_field} or {govuk_input_field_with_form}
|
107
|
+
#
|
108
|
+
# @yieldparam govuk_text_input_classes [Array] the classes for the input HTML
|
109
|
+
# @yieldparam govuk_text_input_attributes [Hash] additional attributes that will added as part of the HTML
|
110
|
+
#
|
111
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_field} or {govuk_input_field_with_form}
|
112
|
+
|
113
|
+
def _govuk_input_field(error_message, **govuk_text_input_options)
|
114
|
+
govuk_text_input_classes = ['govuk-input']
|
115
|
+
govuk_text_input_classes += ['govuk-input--error'] if error_message
|
116
|
+
govuk_text_input_classes << govuk_text_input_options[:classes]
|
117
|
+
govuk_text_input_options[:attributes] ||= {}
|
118
|
+
|
119
|
+
input_html = yield(govuk_text_input_classes, govuk_text_input_options[:attributes])
|
120
|
+
|
121
|
+
prefix = govuk_text_input_options[:prefix]
|
122
|
+
suffix = govuk_text_input_options[:suffix]
|
123
|
+
|
124
|
+
if prefix || suffix
|
125
|
+
tag.div(class: 'govuk-input__wrapper') do
|
126
|
+
capture do
|
127
|
+
concat(govuk_fix('pre', prefix)) if prefix
|
128
|
+
concat(input_html)
|
129
|
+
concat(govuk_fix('suf', suffix)) if suffix
|
130
|
+
end
|
131
|
+
end
|
132
|
+
else
|
133
|
+
input_html
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Generates the prefix and suffix HTML for {_govuk_input_field}
|
138
|
+
#
|
139
|
+
# @param fix [String] either +"pre"+ or +"suf"+
|
140
|
+
# @param govuk_fix_options [Hash] options that will be used in customising the HTML
|
141
|
+
#
|
142
|
+
# @option govuk_fix_options [String] :classes additional CSS classes for the input HTML
|
143
|
+
# @option govuk_text_input_options [Hash] :attributes ({ aria: { hidden: true } }) any additional attributes that will added as part of the HTML
|
144
|
+
#
|
145
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_field} or {govuk_input_field_with_form}
|
146
|
+
|
147
|
+
def govuk_fix(fix, govuk_fix_options)
|
148
|
+
govuk_fix_classes = ["govuk-input__#{fix}fix"]
|
149
|
+
govuk_fix_classes << govuk_fix_options[:classes]
|
150
|
+
govuk_fix_options[:attributes] ||= {}
|
151
|
+
(govuk_fix_options[:attributes][:aria] ||= {}).merge!({ hidden: true })
|
152
|
+
|
153
|
+
tag.div(govuk_fix_options[:text], class: govuk_fix_classes, **govuk_fix_options[:attributes])
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
|