crown_marketplace_utils 0.1.0.beta.6 → 0.1.0.beta.8
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/Gemfile.lock +1 -1
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/accordion.rb +91 -89
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/back_link.rb +24 -22
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/breadcrumbs.rb +56 -54
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/button.rb +109 -107
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/details.rb +33 -31
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/error_message.rb +53 -46
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/character_count.rb +144 -142
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/checkboxes.rb +176 -174
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/date_input.rb +126 -124
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/file_upload.rb +75 -73
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/input.rb +137 -135
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/radios.rb +176 -174
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/select.rb +111 -109
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/textarea.rb +86 -84
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field.rb +181 -179
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/fieldset.rb +56 -54
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/form_group.rb +37 -35
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/header.rb +142 -140
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/hint.rb +25 -23
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/label.rb +69 -67
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/notification_banner.rb +121 -119
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/pagination.rb +290 -288
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/step_by_step_navigation.rb +187 -185
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/tag.rb +29 -27
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper/warning_text.rb +39 -37
- data/lib/crown_marketplace_utils/helpers/gov_uk_helper.rb +31 -29
- data/lib/crown_marketplace_utils/version.rb +1 -1
- metadata +2 -2
@@ -2,162 +2,164 @@
|
|
2
2
|
|
3
3
|
require_relative 'textarea'
|
4
4
|
|
5
|
-
module CrownMarketplaceUtils
|
6
|
-
module
|
7
|
-
module
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module Helpers
|
7
|
+
module GovUkHelper
|
8
|
+
module Field
|
9
|
+
# = GOV.UK Character count
|
10
|
+
#
|
11
|
+
# This helper is used for generating the character count component from the
|
12
|
+
# {https://design-system.service.gov.uk/components/character-count GDS - Components - Character count}
|
13
|
+
#
|
14
|
+
# This is a wrapper around a Textarea module and so makes use of the methods in {Textarea}
|
15
|
+
|
16
|
+
module CharacterCount
|
17
|
+
include Textarea
|
18
|
+
|
19
|
+
# Generates the HTML for the GOV.UK character count component.
|
20
|
+
# It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
|
21
|
+
#
|
22
|
+
# @param attribute [String, Symbol] the attribute of the character count text area
|
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 [String] :error_message (nil) the error message to be displayed
|
26
|
+
# @option govuk_character_count_options [ActiveModel] :model (nil) optional model that can be used to find an error message
|
27
|
+
# @option govuk_character_count_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
|
28
|
+
# the textarea tag and find the error message
|
29
|
+
# @option govuk_character_count_options [Hash] :form_group see {govuk_field}
|
30
|
+
# @option govuk_character_count_options [Hash] :label see {govuk_field}
|
31
|
+
# @option govuk_character_count_options [Hash] :hint see {govuk_field}
|
32
|
+
# @option govuk_character_count_options [Hash] :textarea see {govuk_textarea}
|
33
|
+
# @option govuk_character_count_options [Hash] :character_count ({}) the options that will be used when rendering the textarea.
|
34
|
+
# See {_govuk_character_count} for more details.
|
35
|
+
#
|
36
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
|
37
|
+
# which can then be rendered on the page
|
38
|
+
|
39
|
+
def govuk_character_count(attribute, **govuk_character_count_options)
|
40
|
+
character_count_attribute = govuk_character_count_options[:form] ? "#{form.object_name}_#{attribute}" : attribute
|
41
|
+
|
42
|
+
_govuk_character_count(character_count_attribute, **govuk_character_count_options) do |govuk_textarea_options|
|
43
|
+
govuk_textarea(
|
44
|
+
attribute,
|
45
|
+
error_message: govuk_character_count_options[:error_message],
|
46
|
+
model: govuk_character_count_options[:model],
|
47
|
+
form: govuk_character_count_options[:form],
|
48
|
+
**govuk_textarea_options
|
49
|
+
)
|
50
|
+
end
|
49
51
|
end
|
50
|
-
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
53
|
+
private
|
54
|
+
|
55
|
+
# Wrapper method used by {govuk_character_count} to generate the character count HTML
|
56
|
+
#
|
57
|
+
# @param attribute [String, Symbol] the attribute of the character count
|
58
|
+
# @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
|
59
|
+
# This includes everything described in {govuk_character_count}
|
60
|
+
# with the addition of the +:character_count+ which are described below
|
61
|
+
#
|
62
|
+
# @option govuk_character_count_options [String] :maxlength (required) - if +maxwords+ is set, this is not required.
|
63
|
+
# The maximum number of characters.
|
64
|
+
# If +maxwords+ is provided, the +maxlength+ option will be ignored.
|
65
|
+
# @option govuk_character_count_options [String] :maxwords (required) - if maxlength is set, this is not required.
|
66
|
+
# The maximum number of words.
|
67
|
+
# If +maxwords+ is provided, the +maxlength+ option will be ignored.
|
68
|
+
# @option govuk_character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed.
|
69
|
+
# If this attribute is set, the count message will be hidden by default.
|
70
|
+
# @option govuk_textarea_options [Hash] :fallback_hint ({}) additional parameters that will be used to create the hint containing the character count text.
|
71
|
+
# This includes all the options in {govuk_hint} plus +:count_message+.
|
72
|
+
# This will replace the default text for the count message.
|
73
|
+
# If you want the count number to appear, put %<count>s in the string and it will be replaced with the number
|
74
|
+
#
|
75
|
+
# @yield the textarea HTML generated by {govuk_character_count}
|
76
|
+
#
|
77
|
+
# @yieldparam govuk_textarea_options [Hash] the options used in the textarea called by {govuk_character_count}
|
78
|
+
#
|
79
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the chracter count which wrpas arround {govuk_character_count}
|
80
|
+
|
81
|
+
def _govuk_character_count(attribute, **govuk_character_count_options)
|
82
|
+
deep_init_hash(govuk_character_count_options, :textarea, :attributes, :aria)
|
83
|
+
govuk_character_count_options[:textarea][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea, :attributes, :aria, :describedby), "#{attribute}-info"].compact.join(' ')
|
84
|
+
|
85
|
+
govuk_character_count_options[:textarea][:classes] = "#{govuk_character_count_options[:textarea][:classes]} govuk-js-character-count".lstrip
|
86
|
+
|
87
|
+
tag.div(class: 'govuk-character-count', **get_character_count_attributes(**govuk_character_count_options)) do
|
88
|
+
capture do
|
89
|
+
concat(yield(govuk_character_count_options))
|
90
|
+
concat(character_count_hint(attribute, **govuk_character_count_options))
|
91
|
+
end
|
90
92
|
end
|
91
93
|
end
|
92
|
-
end
|
93
94
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
95
|
+
# Generates the fallback hint HTML for {_govuk_character_count}
|
96
|
+
#
|
97
|
+
# @param (see _govuk_character_count)
|
98
|
+
#
|
99
|
+
# @option (see _govuk_character_count)
|
100
|
+
#
|
101
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the fullback hint used in {_govuk_character_count}
|
101
102
|
|
102
|
-
|
103
|
-
|
104
|
-
|
103
|
+
def character_count_hint(attribute, **govuk_character_count_options)
|
104
|
+
fallback_hint_length = govuk_character_count_options[:character_count][:maxwords] || govuk_character_count_options[:character_count][:maxlength]
|
105
|
+
fallback_hint_default = "You can enter up to %<count>s #{govuk_character_count_options[:character_count][:maxwords] ? 'words' : 'characters'}"
|
105
106
|
|
106
|
-
|
107
|
+
deep_init_hash(govuk_character_count_options, :character_count, :fallback_hint, :attributes)
|
107
108
|
|
108
|
-
|
109
|
+
fallback_hint_text = format(govuk_character_count_options[:character_count][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
|
109
110
|
|
110
|
-
|
111
|
-
|
111
|
+
govuk_character_count_options[:character_count][:fallback_hint][:classes] = "#{govuk_character_count_options.dig(:character_count, :fallback_hint, :classes)} govuk-character-count__message".lstrip
|
112
|
+
govuk_character_count_options[:character_count][:fallback_hint][:attributes].merge!(id: "#{attribute}-info")
|
112
113
|
|
113
|
-
|
114
|
-
end
|
115
|
-
|
116
|
-
# Generates a hash with the character count attributes used in {_govuk_character_count}
|
117
|
-
#
|
118
|
-
# @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
|
119
|
-
# This includes everything described in {govuk_character_count}
|
120
|
-
# with the addition of the +:character_count+ which are described below
|
121
|
-
#
|
122
|
-
# @option (see _govuk_character_count)
|
123
|
-
#
|
124
|
-
# @return [Hash] contains the HTMl attributes used in {_govuk_character_count}
|
125
|
-
|
126
|
-
def get_character_count_attributes(**govuk_character_count_options)
|
127
|
-
govuk_character_count_attributes = { data: { module: 'govuk-character-count' } }
|
128
|
-
|
129
|
-
%i[maxlength threshold maxwords].each do |data_attribute|
|
130
|
-
govuk_character_count_attributes[:data][data_attribute] = govuk_character_count_options[:character_count][data_attribute].to_s if govuk_character_count_options[:character_count][data_attribute]
|
114
|
+
govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count][:fallback_hint])
|
131
115
|
end
|
132
116
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
# @example When the hash has already been initialised
|
150
|
-
# hash = { a: { b: { d: 'hello' } } }
|
151
|
-
# keys = [:a, :b, :c]
|
152
|
-
#
|
153
|
-
# deep_init_hash(hash, *keys)
|
154
|
-
#
|
155
|
-
# hash = { a: { b: { d: 'hello', c: { } } } }
|
117
|
+
# Generates a hash with the character count attributes used in {_govuk_character_count}
|
118
|
+
#
|
119
|
+
# @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
|
120
|
+
# This includes everything described in {govuk_character_count}
|
121
|
+
# with the addition of the +:character_count+ which are described below
|
122
|
+
#
|
123
|
+
# @option (see _govuk_character_count)
|
124
|
+
#
|
125
|
+
# @return [Hash] contains the HTMl attributes used in {_govuk_character_count}
|
126
|
+
|
127
|
+
def get_character_count_attributes(**govuk_character_count_options)
|
128
|
+
govuk_character_count_attributes = { data: { module: 'govuk-character-count' } }
|
129
|
+
|
130
|
+
%i[maxlength threshold maxwords].each do |data_attribute|
|
131
|
+
govuk_character_count_attributes[:data][data_attribute] = govuk_character_count_options[:character_count][data_attribute].to_s if govuk_character_count_options[:character_count][data_attribute]
|
132
|
+
end
|
156
133
|
|
157
|
-
|
158
|
-
|
134
|
+
govuk_character_count_attributes
|
135
|
+
end
|
159
136
|
|
160
|
-
|
137
|
+
# Method to initialise hashes within hashes if it is not already initialised
|
138
|
+
#
|
139
|
+
# @param hash [Hash] the hash that is going to be initialised
|
140
|
+
# @param keys [Array] the keys that are going to be initialised in the hash
|
141
|
+
#
|
142
|
+
# @example When the hash is completely empty
|
143
|
+
# hash = { }
|
144
|
+
# keys = [:a, :b, :c]
|
145
|
+
#
|
146
|
+
# deep_init_hash(hash, *keys)
|
147
|
+
#
|
148
|
+
# hash = { a: { b: { c: { } } } }
|
149
|
+
#
|
150
|
+
# @example When the hash has already been initialised
|
151
|
+
# hash = { a: { b: { d: 'hello' } } }
|
152
|
+
# keys = [:a, :b, :c]
|
153
|
+
#
|
154
|
+
# deep_init_hash(hash, *keys)
|
155
|
+
#
|
156
|
+
# hash = { a: { b: { d: 'hello', c: { } } } }
|
157
|
+
|
158
|
+
def deep_init_hash(hash, *keys)
|
159
|
+
current_hash = hash
|
160
|
+
|
161
|
+
keys.each { |key| current_hash = (current_hash[key] ||= {}) }
|
162
|
+
end
|
161
163
|
end
|
162
164
|
end
|
163
165
|
end
|