crown_marketplace_utils 0.1.0.beta.3 → 0.1.0.beta.4
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/README.md +2 -1
- data/lib/crown_marketplace_utils/gov_uk_helper/button.rb +2 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/character_count.rb +35 -38
- data/lib/crown_marketplace_utils/gov_uk_helper/field/checkboxes.rb +36 -26
- data/lib/crown_marketplace_utils/gov_uk_helper/field/date_input.rb +234 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/file_upload.rb +125 -0
- data/lib/crown_marketplace_utils/gov_uk_helper/field/input.rb +53 -19
- data/lib/crown_marketplace_utils/gov_uk_helper/field/radios.rb +37 -23
- data/lib/crown_marketplace_utils/gov_uk_helper/field/select.rb +52 -48
- data/lib/crown_marketplace_utils/gov_uk_helper/field/textarea.rb +48 -35
- data/lib/crown_marketplace_utils/gov_uk_helper/field.rb +138 -96
- data/lib/crown_marketplace_utils/gov_uk_helper/fieldset.rb +1 -1
- data/lib/crown_marketplace_utils/gov_uk_helper/header.rb +2 -2
- data/lib/crown_marketplace_utils/gov_uk_helper/label.rb +1 -1
- data/lib/crown_marketplace_utils/gov_uk_helper/pagination.rb +1 -1
- data/lib/crown_marketplace_utils/gov_uk_helper.rb +4 -0
- data/lib/crown_marketplace_utils/version.rb +1 -1
- metadata +4 -2
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module GovUkHelper
|
7
|
+
module Field
|
8
|
+
# = GOV.UK File Upload
|
9
|
+
#
|
10
|
+
# This helper is used for generating the file upload component from the
|
11
|
+
# {https://design-system.service.gov.uk/components/file-upload GDS - Components - File Upload}
|
12
|
+
#
|
13
|
+
# This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
|
14
|
+
|
15
|
+
module FileUpload
|
16
|
+
include Field
|
17
|
+
|
18
|
+
# Generates the HTML for the GOV.UK file upload component
|
19
|
+
#
|
20
|
+
# @param attribute [String, Symbol] the attribute of the file upload
|
21
|
+
# @param error_message [String] the error message to be displayed
|
22
|
+
# @param govuk_file_upload_options [Hash] options that will be used for the parts of the form group, label, hint and file upload
|
23
|
+
#
|
24
|
+
# @option govuk_file_upload_options [Hash] :form_group see {govuk_field}
|
25
|
+
# @option govuk_file_upload_options [Hash] :label see {govuk_field}
|
26
|
+
# @option govuk_file_upload_options [Hash] :hint see {govuk_field}
|
27
|
+
# @option govuk_file_upload_options [Hash] :file_upload ({}) the options that will be used when rendering the file upload.
|
28
|
+
# See {govuk_file_upload_field} for more details.
|
29
|
+
#
|
30
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK File Upload
|
31
|
+
# which can then be rendered on the page
|
32
|
+
|
33
|
+
def govuk_file_upload(attribute, error_message = nil, **govuk_file_upload_options)
|
34
|
+
govuk_field(:file_upload, attribute, error_message, **govuk_file_upload_options) do |govuk_field_options|
|
35
|
+
concat(govuk_file_upload_field(attribute, error_message, **govuk_field_options))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Generates the HTML for the GOV.UK file upload component using an ActiveModel.
|
40
|
+
# Unlike {govuk_file_upload}, 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 for the file upload
|
43
|
+
# @param attribute [String, Symbol] the attribute of the file upload
|
44
|
+
# @param govuk_file_upload_options [Hash] options that will be used for the parts of the form group, label, hint and file upload
|
45
|
+
#
|
46
|
+
# @option (see govuk_file_upload)
|
47
|
+
#
|
48
|
+
# @return (see govuk_file_upload)
|
49
|
+
|
50
|
+
def govuk_file_upload_with_model(model, attribute, **govuk_file_upload_options)
|
51
|
+
govuk_field_with_model(:file_upload, model, attribute, **govuk_file_upload_options) do |govuk_field_options, error_message|
|
52
|
+
concat(govuk_file_upload_field(attribute, error_message, **govuk_field_options))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Generates the HTML for the GOV.UK file upload component using an ActionView::Helpers::FormBuilder.
|
57
|
+
# Unlike {govuk_file_upload}, the method will be able to automatically determine if the error message needs to be shown.
|
58
|
+
#
|
59
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the file upload input
|
60
|
+
# @param attribute [String, Symbol] the attribute of the file upload
|
61
|
+
# @param govuk_file_upload_options [Hash] options that will be used for the parts of the form group, label, hint and file upload
|
62
|
+
#
|
63
|
+
# @option (see govuk_file_upload)
|
64
|
+
#
|
65
|
+
# @return (see govuk_file_upload)
|
66
|
+
|
67
|
+
def govuk_file_upload_with_form(form, attribute, **govuk_file_upload_options)
|
68
|
+
govuk_field_with_form(:file_upload, form, attribute, **govuk_file_upload_options) do |govuk_field_options, error_message|
|
69
|
+
concat(govuk_file_upload_field_with_form(form, attribute, error_message, **govuk_field_options))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
# Generates the file upload HTML for {govuk_file_upload} and {govuk_file_upload_with_model}
|
76
|
+
#
|
77
|
+
# @param attribute [String, Symbol] the attribute of the file upload
|
78
|
+
# @param error_message [String] the error message to be displayed
|
79
|
+
# @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
|
80
|
+
#
|
81
|
+
# @option (see set_file_upload_options)
|
82
|
+
#
|
83
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the file upload field which is used in {govuk_file_upload} and {govuk_file_upload_with_model}
|
84
|
+
|
85
|
+
def govuk_file_upload_field(attribute, error_message, **govuk_file_upload_options)
|
86
|
+
set_file_upload_options(error_message, govuk_file_upload_options)
|
87
|
+
|
88
|
+
file_field_tag(attribute, class: govuk_file_upload_options[:classes], **govuk_file_upload_options[:attributes])
|
89
|
+
end
|
90
|
+
|
91
|
+
# Generates the file upload HTML for {govuk_file_upload_with_form}
|
92
|
+
#
|
93
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the file upload input
|
94
|
+
# @param attribute [String, Symbol] the attribute of the file upload
|
95
|
+
# @param error_message [String] used to indicate if there is an error which will add an extra classes
|
96
|
+
# @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
|
97
|
+
#
|
98
|
+
# @option (see set_file_upload_options)
|
99
|
+
#
|
100
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the file upload field which is used in {govuk_file_upload_with_form}
|
101
|
+
|
102
|
+
def govuk_file_upload_field_with_form(form, attribute, error_message, **govuk_file_upload_options)
|
103
|
+
set_file_upload_options(error_message, govuk_file_upload_options)
|
104
|
+
|
105
|
+
form.file_field(attribute, class: govuk_file_upload_options[:classes], **govuk_file_upload_options[:attributes])
|
106
|
+
end
|
107
|
+
|
108
|
+
# Initialises the attributes for the file upload input
|
109
|
+
#
|
110
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
111
|
+
# @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
|
112
|
+
#
|
113
|
+
# @option govuk_file_upload_options [String] :classes additional CSS classes for the file iupload HTML
|
114
|
+
# @option govuk_file_upload_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
115
|
+
|
116
|
+
def set_file_upload_options(error_message, govuk_file_upload_options)
|
117
|
+
govuk_file_upload_options[:classes] = "govuk-file-upload #{govuk_file_upload_options[:classes]}".rstrip
|
118
|
+
govuk_file_upload_options[:classes] << ' govuk-file-upload--error' if error_message
|
119
|
+
|
120
|
+
govuk_file_upload_options[:attributes] ||= {}
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -21,11 +21,11 @@ module CrownMarketplaceUtils
|
|
21
21
|
# @param error_message [String] the error message to be displayed
|
22
22
|
# @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
|
23
23
|
#
|
24
|
-
# @option govuk_input_options [Hash] :
|
24
|
+
# @option govuk_input_options [Hash] :form_group see {govuk_field}
|
25
25
|
# @option govuk_input_options [Hash] :label see {govuk_field}
|
26
26
|
# @option govuk_input_options [Hash] :hint see {govuk_field}
|
27
|
-
# @option govuk_input_options [Hash] :
|
28
|
-
#
|
27
|
+
# @option govuk_input_options [Hash] :input ({}) the options that will be used when rendering the input.
|
28
|
+
# See {govuk_input_field} for more details.
|
29
29
|
#
|
30
30
|
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Input
|
31
31
|
# which can then be rendered on the page
|
@@ -36,18 +36,33 @@ module CrownMarketplaceUtils
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
+
# Generates the HTML for the GOV.UK input component using an ActiveModel.
|
40
|
+
# Unlike {govuk_input}, 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 the value of 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 (see govuk_input)
|
47
|
+
#
|
48
|
+
# @return (see govuk_input)
|
49
|
+
|
50
|
+
def govuk_input_with_model(model, attribute, **govuk_input_options)
|
51
|
+
govuk_field_with_model(:input, model, attribute, **govuk_input_options) do |govuk_field_options, error_message|
|
52
|
+
govuk_field_options[:value] = model.send(attribute)
|
53
|
+
|
54
|
+
concat(govuk_input_field(attribute, error_message, **govuk_field_options))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
39
58
|
# Generates the HTML for the GOV.UK input component using an ActionView::Helpers::FormBuilder.
|
40
59
|
# Unlike {govuk_input}, the method will be able to automatically determine if the error message needs to be shown.
|
41
60
|
#
|
42
|
-
# @param form [ActionView::Helpers::FormBuilder]
|
61
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the input
|
43
62
|
# @param attribute [String, Symbol] the attribute of the input
|
44
63
|
# @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
|
45
64
|
#
|
46
|
-
# @option
|
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.
|
65
|
+
# @option (see govuk_input)
|
51
66
|
#
|
52
67
|
# @return (see govuk_input)
|
53
68
|
|
@@ -59,7 +74,7 @@ module CrownMarketplaceUtils
|
|
59
74
|
|
60
75
|
private
|
61
76
|
|
62
|
-
# Generates the input HTML for {govuk_input}
|
77
|
+
# Generates the input HTML for {govuk_input} and {govuk_input_with_model}
|
63
78
|
#
|
64
79
|
# @param attribute [String, Symbol] the attribute of the input
|
65
80
|
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
@@ -71,15 +86,17 @@ module CrownMarketplaceUtils
|
|
71
86
|
|
72
87
|
def govuk_input_field(attribute, error_message, **govuk_text_input_options)
|
73
88
|
_govuk_input_field(error_message, **govuk_text_input_options) do |govuk_text_input_classes, govuk_text_input_attributes|
|
74
|
-
|
89
|
+
field_type = :"#{get_field_type(govuk_text_input_options[:field_type])}_tag"
|
90
|
+
|
91
|
+
send(field_type, attribute, govuk_text_input_options[:value], class: govuk_text_input_classes, **govuk_text_input_attributes)
|
75
92
|
end
|
76
93
|
end
|
77
94
|
|
78
95
|
# Generates the input HTML for {govuk_input_with_form}
|
79
96
|
#
|
80
|
-
# @param form [ActionView::Helpers::FormBuilder]
|
97
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the input
|
81
98
|
# @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
|
99
|
+
# @param error_message [String] used to indicate if there is an error which will add an extra classes
|
83
100
|
# @param govuk_text_input_options [Hash] options that will be used in customising the HTML
|
84
101
|
#
|
85
102
|
# @option (see _govuk_input_field)
|
@@ -88,7 +105,9 @@ module CrownMarketplaceUtils
|
|
88
105
|
|
89
106
|
def govuk_input_field_with_form(form, attribute, error_message, **govuk_text_input_options)
|
90
107
|
_govuk_input_field(error_message, **govuk_text_input_options) do |govuk_text_input_classes, govuk_text_input_attributes|
|
91
|
-
|
108
|
+
field_type = get_field_type(govuk_text_input_options[:field_type])
|
109
|
+
|
110
|
+
form.send(field_type, attribute, class: govuk_text_input_classes, **govuk_text_input_attributes)
|
92
111
|
end
|
93
112
|
end
|
94
113
|
|
@@ -99,6 +118,7 @@ module CrownMarketplaceUtils
|
|
99
118
|
#
|
100
119
|
# @option govuk_text_input_options [String] :classes additional CSS classes for the input HTML
|
101
120
|
# @option govuk_text_input_options [String] :value (nil) the value of the input
|
121
|
+
# @option govuk_text_input_options [Symbol] :field_type the type of the input field, see {get_field_type} for options
|
102
122
|
# @option govuk_text_input_options [Hash] :prefix (nil) optional prefix for the input field. See {govuk_fix} for more details.
|
103
123
|
# @option govuk_text_input_options [Hash] :suffix (nil) optional suffix for the input field. See {govuk_fix} for more details.
|
104
124
|
# @option govuk_text_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
@@ -111,9 +131,9 @@ module CrownMarketplaceUtils
|
|
111
131
|
# @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_field} or {govuk_input_field_with_form}
|
112
132
|
|
113
133
|
def _govuk_input_field(error_message, **govuk_text_input_options)
|
114
|
-
govuk_text_input_classes =
|
115
|
-
govuk_text_input_classes
|
116
|
-
|
134
|
+
govuk_text_input_classes = "govuk-input #{govuk_text_input_options[:classes]}".rstrip
|
135
|
+
govuk_text_input_classes << ' govuk-input--error' if error_message
|
136
|
+
|
117
137
|
govuk_text_input_options[:attributes] ||= {}
|
118
138
|
|
119
139
|
input_html = yield(govuk_text_input_classes, govuk_text_input_options[:attributes])
|
@@ -152,9 +172,23 @@ module CrownMarketplaceUtils
|
|
152
172
|
|
153
173
|
tag.div(govuk_fix_options[:text], class: govuk_fix_classes, **govuk_fix_options[:attributes])
|
154
174
|
end
|
175
|
+
|
176
|
+
# Returns the field type used to create the rails input tag
|
177
|
+
#
|
178
|
+
# @param field_type [Symbol] the type of the field, defaults to text
|
179
|
+
# Allowed values are:
|
180
|
+
# - +:email+
|
181
|
+
# - +:password+
|
182
|
+
|
183
|
+
def get_field_type(field_type)
|
184
|
+
case field_type
|
185
|
+
when :email, :password
|
186
|
+
:"#{field_type}_field"
|
187
|
+
else
|
188
|
+
:text_field
|
189
|
+
end
|
190
|
+
end
|
155
191
|
end
|
156
192
|
end
|
157
193
|
end
|
158
194
|
end
|
159
|
-
|
160
|
-
ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
|
@@ -22,11 +22,11 @@ module CrownMarketplaceUtils
|
|
22
22
|
# @param error_message [String] the error message to be displayed
|
23
23
|
# @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
|
24
24
|
#
|
25
|
-
# @option govuk_radios_options [Hash] :
|
26
|
-
# @option govuk_radios_options [Hash] :
|
25
|
+
# @option govuk_radios_options [Hash] :form_group see {govuk_fields}
|
26
|
+
# @option govuk_radios_options [Hash] :fieldset see {govuk_fields}
|
27
27
|
# @option govuk_radios_options [Hash] :hint see {govuk_field}
|
28
|
-
# @option govuk_radios_options [Hash] :
|
29
|
-
#
|
28
|
+
# @option govuk_radios_options [Hash] :radios ({}) the options that will be used when rendering the radio buttons.
|
29
|
+
# See {govuk_radios_fields} for more details.
|
30
30
|
#
|
31
31
|
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Radios
|
32
32
|
# which can then be rendered on the page
|
@@ -37,6 +37,27 @@ module CrownMarketplaceUtils
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
# Generates the HTML for the GOV.UK Radios component using an ActiveModel.
|
41
|
+
# Unlike {govuk_radios}, the method will be able to automatically determine if the error message needs to be shown.
|
42
|
+
#
|
43
|
+
# @param model [ActiveModel] model that will be used to find an error message and the selected radio button
|
44
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
45
|
+
# @param items [Array] array of radio items, see {_govuk_radios_fields}
|
46
|
+
# @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
|
47
|
+
#
|
48
|
+
# @option (see govuk_radios)
|
49
|
+
#
|
50
|
+
# @return (see govuk_radios)
|
51
|
+
|
52
|
+
def govuk_radios_with_model(model, attribute, items, **govuk_radios_options)
|
53
|
+
value = model.send(attribute)
|
54
|
+
items.each { |item| item[:checked] = item[:value] == value }
|
55
|
+
|
56
|
+
govuk_fields_with_model(:radios, model, attribute, **govuk_radios_options) do |govuk_field_options|
|
57
|
+
concat(govuk_radios_fields(attribute, items, **govuk_field_options))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
40
61
|
# Generates the HTML for the GOV.UK Radios component using an ActionView::Helpers::FormBuilder.
|
41
62
|
# Unlike {govuk_radios}, the method will be able to automatically determine if the error message needs to be shown.
|
42
63
|
#
|
@@ -45,11 +66,7 @@ module CrownMarketplaceUtils
|
|
45
66
|
# @param items [Array] array of radio items, see {_govuk_radios_fields}
|
46
67
|
# @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
|
47
68
|
#
|
48
|
-
# @option
|
49
|
-
# @option govuk_radios_options [Hash] :fieldset_options see {govuk_fields_with_form}
|
50
|
-
# @option govuk_radios_options [Hash] :hint see {govuk_fields_with_form}
|
51
|
-
# @option govuk_radios_options [Hash] :radios_options ({}) the options that will be used when rendering the radio buttons.
|
52
|
-
# See {govuk_radios_fields_with_form} for more details.
|
69
|
+
# @option (see govuk_radios)
|
53
70
|
#
|
54
71
|
# @return (see govuk_radios)
|
55
72
|
|
@@ -79,7 +96,7 @@ module CrownMarketplaceUtils
|
|
79
96
|
|
80
97
|
# Generates the radios HTML for {govuk_radios_with_form}
|
81
98
|
#
|
82
|
-
# @param form [ActionView::Helpers::FormBuilder]
|
99
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
|
83
100
|
# @param attribute [String, Symbol] the attribute of the raido buttons
|
84
101
|
# @param items [Array] array of radio items, see {_govuk_radios_fields}
|
85
102
|
# @param govuk_radios_options [Hash] options that will be used in customising the HTML
|
@@ -146,7 +163,7 @@ module CrownMarketplaceUtils
|
|
146
163
|
label_attribute = radio_item[:attributes][:id] || "#{attribute}[#{radio_item[:value]}]"
|
147
164
|
|
148
165
|
concat(radio_button_tag(attribute, radio_item[:value], radio_item[:checked], class: 'govuk-radios__input', **radio_item[:attributes]))
|
149
|
-
concat(govuk_label(label_attribute, radio_item[:label][:text], **radio_item[:label]
|
166
|
+
concat(govuk_label(label_attribute, radio_item[:label][:text], **radio_item[:label]))
|
150
167
|
end
|
151
168
|
end
|
152
169
|
|
@@ -161,11 +178,11 @@ module CrownMarketplaceUtils
|
|
161
178
|
|
162
179
|
def govuk_radio_item_with_form(form, attribute, radio_item)
|
163
180
|
_govuk_radio_item(attribute, radio_item) do
|
164
|
-
(radio_item[:label][:
|
165
|
-
radio_item[:label][:
|
181
|
+
(radio_item[:label][:attributes] ||= {})[:value] = radio_item[:value]
|
182
|
+
radio_item[:label][:attributes][:for] = radio_item[:attributes][:id] if radio_item[:attributes][:id]
|
166
183
|
|
167
184
|
concat(form.radio_button(attribute, radio_item[:value], class: 'govuk-radios__input', **radio_item[:attributes]))
|
168
|
-
concat(govuk_label_with_form(form, attribute, radio_item[:label][:text], **radio_item[:label]
|
185
|
+
concat(govuk_label_with_form(form, attribute, radio_item[:label][:text], **radio_item[:label]))
|
169
186
|
end
|
170
187
|
end
|
171
188
|
|
@@ -176,12 +193,9 @@ module CrownMarketplaceUtils
|
|
176
193
|
# @param radio_item [Hash] the options for the radio item
|
177
194
|
#
|
178
195
|
# @option radio_item [String] :classes additional CSS classes for the radio button HTML
|
179
|
-
# @option radio_item [Hash] :label the parameters that will be used to create the label for the radio button
|
180
|
-
#
|
181
|
-
#
|
182
|
-
# @option radio_item [Hash] :hint (nil) the parameters that will be used to create the hint for the field
|
183
|
-
# - +:text+ (required) - the hint text
|
184
|
-
# - +:options+ - default: { } - the options for govuk_hint {govuk_hint}
|
196
|
+
# @option radio_item [Hash] :label the parameters that will be used to create the label for the radio button, see {govuk_label}
|
197
|
+
# @option radio_item [Hash] :hint (nil) the parameters that will be used to create the hint for the radio button, see {govuk_hint}.
|
198
|
+
# If no hint is given then no hint will be rendered
|
185
199
|
# @option radio_item [Hash] :attributes ({}) any additional attributes that will be added as part of the radio button HTML
|
186
200
|
#
|
187
201
|
# @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons, label and hint
|
@@ -189,14 +203,14 @@ module CrownMarketplaceUtils
|
|
189
203
|
|
190
204
|
def _govuk_radio_item(attribute, radio_item)
|
191
205
|
radio_item[:attributes] ||= {}
|
192
|
-
radio_item[:label]
|
193
|
-
radio_item[:label][:
|
206
|
+
radio_item[:label] ||= {}
|
207
|
+
radio_item[:label][:classes] = "govuk-radios__label #{radio_item[:label][:classes]}".rstrip
|
194
208
|
|
195
209
|
set_item_options_for_hint('radios', attribute, radio_item) if radio_item[:hint]
|
196
210
|
|
197
211
|
capture do
|
198
212
|
yield
|
199
|
-
concat(govuk_hint(radio_item[:hint][:text], **radio_item[:hint]
|
213
|
+
concat(govuk_hint(radio_item[:hint][:text], **radio_item[:hint])) if radio_item[:hint]
|
200
214
|
end
|
201
215
|
end
|
202
216
|
end
|
@@ -24,11 +24,11 @@ module CrownMarketplaceUtils
|
|
24
24
|
# @param error_message [String] the error message to be displayed
|
25
25
|
# @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
|
26
26
|
#
|
27
|
-
# @option govuk_select_options [Hash] :
|
27
|
+
# @option govuk_select_options [Hash] :form_group see {govuk_field}
|
28
28
|
# @option govuk_select_options [Hash] :label see {govuk_field}
|
29
29
|
# @option govuk_select_options [Hash] :hint see {govuk_field}
|
30
|
-
# @option govuk_select_options [Hash] :
|
31
|
-
#
|
30
|
+
# @option govuk_select_options [Hash] :select ({}) the options that will be used when rendering the select field.
|
31
|
+
# See {govuk_select_field} for more details.
|
32
32
|
#
|
33
33
|
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Select
|
34
34
|
# which can then be rendered on the page
|
@@ -39,19 +39,35 @@ module CrownMarketplaceUtils
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
# Generates the HTML for the GOV.UK Select component using an ActiveModel.
|
43
|
+
# Unlike {govuk_select}, the method will be able to automatically determine if the error message needs to be shown.
|
44
|
+
#
|
45
|
+
# @param model [ActiveModel] model that will be used to find an error message and the selected options
|
46
|
+
# @param attribute [String, Symbol] the attribute of the select field
|
47
|
+
# @param items [Array] array of option items for the select, see {govuk_map_select_items}
|
48
|
+
# @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
|
49
|
+
#
|
50
|
+
# @option see govuk_select)
|
51
|
+
#
|
52
|
+
# @return (see govuk_select)
|
53
|
+
|
54
|
+
def govuk_select_with_model(model, attribute, items, **govuk_select_options)
|
55
|
+
(govuk_select_options[:select] ||= {})[:selected] = model.send(attribute)
|
56
|
+
|
57
|
+
govuk_field_with_model(:select, model, attribute, **govuk_select_options) do |govuk_field_options, error_message|
|
58
|
+
concat(govuk_select_field(attribute, items, error_message, **govuk_field_options))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
42
62
|
# Generates the HTML for the GOV.UK Select component using an ActionView::Helpers::FormBuilder.
|
43
63
|
# Unlike {govuk_select}, the method will be able to automatically determine if the error message needs to be shown.
|
44
64
|
#
|
45
|
-
# @param form [ActionView::Helpers::FormBuilder]
|
65
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the select field
|
46
66
|
# @param attribute [String, Symbol] the attribute of the select field
|
47
67
|
# @param items [Array] array of option items for the select, see {govuk_map_select_items}
|
48
68
|
# @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
|
49
69
|
#
|
50
|
-
# @option
|
51
|
-
# @option govuk_select_options [Hash] :label see {govuk_field_with_form}
|
52
|
-
# @option govuk_select_options [Hash] :hint see {govuk_field_with_form}
|
53
|
-
# @option govuk_select_options [Hash] :select_options ({}) the options that will be used when rendering the select field.
|
54
|
-
# See {govuk_select_field_with_form} for more details.
|
70
|
+
# @option see govuk_select)
|
55
71
|
#
|
56
72
|
# @return (see govuk_select)
|
57
73
|
|
@@ -70,49 +86,49 @@ module CrownMarketplaceUtils
|
|
70
86
|
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
71
87
|
# @param govuk_select_options [Hash] options that will be used in customising the HTML
|
72
88
|
#
|
73
|
-
# @option (see
|
89
|
+
# @option (see set_govuk_select_field_options)
|
74
90
|
#
|
75
91
|
# @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select}
|
76
92
|
|
77
93
|
def govuk_select_field(attribute, items, error_message, **govuk_select_options)
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
94
|
+
set_govuk_select_field_options(error_message, govuk_select_options)
|
95
|
+
|
96
|
+
select_tag(
|
97
|
+
attribute,
|
98
|
+
options_for_select(
|
99
|
+
govuk_map_select_items(items),
|
100
|
+
govuk_select_options[:selected]
|
101
|
+
),
|
102
|
+
class: govuk_select_options[:classes],
|
103
|
+
**govuk_select_options[:attributes]
|
104
|
+
)
|
89
105
|
end
|
90
106
|
|
91
107
|
# Generates the select HTML for {govuk_select_with_form}
|
92
108
|
#
|
93
|
-
# @param form [ActionView::Helpers::FormBuilder]
|
109
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the select field
|
94
110
|
# @param attribute [String, Symbol] the attribute of the select field
|
95
111
|
# @param items [Array] array of option items for the select, see {govuk_map_select_items}
|
96
112
|
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
97
113
|
# @param govuk_select_options [Hash] options that will be used in customising the HTML
|
98
114
|
#
|
99
|
-
# @option (see
|
115
|
+
# @option (see set_govuk_select_field_options)
|
100
116
|
#
|
101
117
|
# @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select_with_form}
|
102
118
|
|
103
119
|
def govuk_select_field_with_form(form, attribute, items, error_message, **govuk_select_options)
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
120
|
+
set_govuk_select_field_options(error_message, govuk_select_options)
|
121
|
+
|
122
|
+
form.select(
|
123
|
+
attribute,
|
124
|
+
govuk_map_select_items(items),
|
125
|
+
govuk_select_options[:select] || {},
|
126
|
+
class: govuk_select_options[:classes],
|
127
|
+
**govuk_select_options[:attributes]
|
128
|
+
)
|
113
129
|
end
|
114
130
|
|
115
|
-
#
|
131
|
+
# Initialises the attributes for the select input
|
116
132
|
#
|
117
133
|
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
118
134
|
# @param govuk_select_options [Hash] options that will be used in customising the HTML
|
@@ -120,21 +136,11 @@ module CrownMarketplaceUtils
|
|
120
136
|
# @option govuk_select_options [String] :classes additional CSS classes for the select HTML
|
121
137
|
# @option govuk_select_options [String] :selected (nil) the selected option
|
122
138
|
# @option govuk_select_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
123
|
-
#
|
124
|
-
# @yield the select HTML generated by {govuk_select_field} or {govuk_select_field_with_form}
|
125
|
-
#
|
126
|
-
# @yieldparam govuk_select_classes [Array] the classes for the select HTML
|
127
|
-
# @yieldparam govuk_select_attributes [Hash] additional attributes that will added as part of the HTML
|
128
|
-
#
|
129
|
-
# @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select_field} or {govuk_select_field_with_form}
|
130
139
|
|
131
|
-
def
|
132
|
-
|
133
|
-
|
134
|
-
govuk_select_classes << govuk_select_options[:classes]
|
140
|
+
def set_govuk_select_field_options(error_message, govuk_select_options)
|
141
|
+
govuk_select_options[:classes] = "govuk-select #{govuk_select_options[:classes]}".rstrip
|
142
|
+
govuk_select_options[:classes] << ' govuk-select--error' if error_message
|
135
143
|
govuk_select_options[:attributes] ||= {}
|
136
|
-
|
137
|
-
yield(govuk_select_classes, govuk_select_options[:attributes])
|
138
144
|
end
|
139
145
|
|
140
146
|
# Maps the items into an array that can be used to generate the options for
|
@@ -162,5 +168,3 @@ module CrownMarketplaceUtils
|
|
162
168
|
end
|
163
169
|
end
|
164
170
|
end
|
165
|
-
|
166
|
-
ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
|