crown_marketplace_utils 0.1.0.beta.1 → 0.1.0.beta.3
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/.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,205 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module GovUkHelper
|
7
|
+
module Field
|
8
|
+
# = GOV.UK Radios
|
9
|
+
#
|
10
|
+
# This helper is used for generating the radios component from the
|
11
|
+
# {https://design-system.service.gov.uk/components/radios GDS - Components - Radios}
|
12
|
+
#
|
13
|
+
# This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
|
14
|
+
|
15
|
+
module Radios
|
16
|
+
include Field
|
17
|
+
|
18
|
+
# Generates the HTML for the GOV.UK Radios component
|
19
|
+
#
|
20
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
21
|
+
# @param items [Array] array of radio items, see {_govuk_radios_fields}
|
22
|
+
# @param error_message [String] the error message to be displayed
|
23
|
+
# @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
|
24
|
+
#
|
25
|
+
# @option govuk_radios_options [Hash] :form_group_options see {govuk_fields}
|
26
|
+
# @option govuk_radios_options [Hash] :fieldset_options see {govuk_fields}
|
27
|
+
# @option govuk_radios_options [Hash] :hint see {govuk_field}
|
28
|
+
# @option govuk_radios_options [Hash] :radios_options ({}) the options that will be used when rendering the radio buttons.
|
29
|
+
# See {govuk_radios_fields} for more details.
|
30
|
+
#
|
31
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Radios
|
32
|
+
# which can then be rendered on the page
|
33
|
+
|
34
|
+
def govuk_radios(attribute, items, error_message = nil, **govuk_radios_options)
|
35
|
+
govuk_fields(:radios, attribute, error_message, **govuk_radios_options) do |govuk_field_options|
|
36
|
+
concat(govuk_radios_fields(attribute, items, **govuk_field_options))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Generates the HTML for the GOV.UK Radios component using an ActionView::Helpers::FormBuilder.
|
41
|
+
# Unlike {govuk_radios}, 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 radio buttons
|
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 govuk_radios_options [Hash] :form_group_options see {govuk_fields_with_form}
|
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.
|
53
|
+
#
|
54
|
+
# @return (see govuk_radios)
|
55
|
+
|
56
|
+
def govuk_radios_with_form(form, attribute, items, **govuk_radios_options)
|
57
|
+
govuk_fields_with_form(:radios, form, attribute, **govuk_radios_options) do |govuk_field_options|
|
58
|
+
concat(govuk_radios_fields_with_form(form, attribute, items, **govuk_field_options))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
# Generates the radios HTML for {govuk_radios}
|
65
|
+
#
|
66
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
67
|
+
# @param items [Array] array of radio items, see {_govuk_radios_fields}
|
68
|
+
# @param govuk_radios_options [Hash] options that will be used in customising the HTML
|
69
|
+
#
|
70
|
+
# @option (see _govuk_radios_fields)
|
71
|
+
#
|
72
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios}
|
73
|
+
|
74
|
+
def govuk_radios_fields(attribute, items, **govuk_radios_options)
|
75
|
+
_govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
|
76
|
+
govuk_radio_item(attribute, radio_item)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Generates the radios HTML for {govuk_radios_with_form}
|
81
|
+
#
|
82
|
+
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the radio buttons
|
83
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
84
|
+
# @param items [Array] array of radio items, see {_govuk_radios_fields}
|
85
|
+
# @param govuk_radios_options [Hash] options that will be used in customising the HTML
|
86
|
+
#
|
87
|
+
# @option (see _govuk_radios_fields)
|
88
|
+
#
|
89
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios_with_form}
|
90
|
+
|
91
|
+
def govuk_radios_fields_with_form(form, attribute, items, **govuk_radios_options)
|
92
|
+
_govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
|
93
|
+
govuk_radio_item_with_form(form, attribute, radio_item)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Wrapper method used by {govuk_radios_fields} and {govuk_radios_fields_with_form} to generate the radios HTML
|
98
|
+
#
|
99
|
+
# @param items [Array] array of radio items.
|
100
|
+
# Each item is a hash which can be:
|
101
|
+
# - +:divider+ text to separate radio items
|
102
|
+
# - radio item, see {_govuk_radio_item}
|
103
|
+
# @param govuk_radios_options [Hash] options that will be used in customising the HTML
|
104
|
+
#
|
105
|
+
# @option govuk_radios_options [String] :classes additional CSS classes for the radios HTML
|
106
|
+
# @option govuk_radios_options [Hash] :attributes ({ module: 'govuk-radios' }) any additional attributes that will added as part of the HTML
|
107
|
+
#
|
108
|
+
# @yield the radio item HTML generated by {govuk_radios_fields} or {govuk_radios_fields_with_form}
|
109
|
+
#
|
110
|
+
# @yieldparam radio_item [Hash] the current radio item to be rendered
|
111
|
+
#
|
112
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios_fields} or {govuk_radios_fields_with_form}
|
113
|
+
|
114
|
+
def _govuk_radios_fields(items, **govuk_radios_options)
|
115
|
+
govuk_radios_classes = ['govuk-radios']
|
116
|
+
govuk_radios_classes << govuk_radios_options[:classes]
|
117
|
+
((govuk_radios_options[:attributes] ||= {})[:data] ||= {}).merge!({ module: 'govuk-radios' })
|
118
|
+
|
119
|
+
tag.div(class: govuk_radios_classes, **govuk_radios_options[:attributes]) do
|
120
|
+
capture do
|
121
|
+
items.each do |radio_item|
|
122
|
+
concat(
|
123
|
+
if radio_item[:divider]
|
124
|
+
tag.div(radio_item[:divider], class: 'govuk-radios__divider')
|
125
|
+
else
|
126
|
+
tag.div(class: 'govuk-radios__item') do
|
127
|
+
yield(radio_item)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Generates the HTML for a radio button for {govuk_radios_fields_with_form}
|
137
|
+
#
|
138
|
+
# @param (see _govuk_radio_item)
|
139
|
+
#
|
140
|
+
# @option (see _govuk_radio_item)
|
141
|
+
#
|
142
|
+
# @return (see _govuk_radio_item)
|
143
|
+
|
144
|
+
def govuk_radio_item(attribute, radio_item)
|
145
|
+
_govuk_radio_item(attribute, radio_item) do
|
146
|
+
label_attribute = radio_item[:attributes][:id] || "#{attribute}[#{radio_item[:value]}]"
|
147
|
+
|
148
|
+
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][:options]))
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# Generates the HTML for a radio button for {govuk_radios_fields}
|
154
|
+
#
|
155
|
+
# @param (see _govuk_radio_item)
|
156
|
+
# @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
|
157
|
+
#
|
158
|
+
# @option (see _govuk_radio_item)
|
159
|
+
#
|
160
|
+
# @return (see _govuk_radio_item)
|
161
|
+
|
162
|
+
def govuk_radio_item_with_form(form, attribute, radio_item)
|
163
|
+
_govuk_radio_item(attribute, radio_item) do
|
164
|
+
(radio_item[:label][:options][:attributes] ||= {})[:value] = radio_item[:value]
|
165
|
+
radio_item[:label][:options][:attributes][:for] = radio_item[:attributes][:id] if radio_item[:attributes][:id]
|
166
|
+
|
167
|
+
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][:options]))
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# Wrapper method used by {govuk_radio_item} and {govuk_radio_item_with_form} to generate the radios HTML
|
173
|
+
# including the label and hint, if there is one.
|
174
|
+
#
|
175
|
+
# @param attribute [String, Symbol] the attribute of the raido buttons
|
176
|
+
# @param radio_item [Hash] the options for the radio item
|
177
|
+
#
|
178
|
+
# @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
|
+
# - +:text+ (required) - the label text
|
181
|
+
# - +:options+ - default: { } - the options for govuk_label {govuk_label}
|
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}
|
185
|
+
# @option radio_item [Hash] :attributes ({}) any additional attributes that will be added as part of the radio button HTML
|
186
|
+
#
|
187
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons, label and hint
|
188
|
+
# which is used in {govuk_radio_item} and {govuk_radio_item_with_form}
|
189
|
+
|
190
|
+
def _govuk_radio_item(attribute, radio_item)
|
191
|
+
radio_item[:attributes] ||= {}
|
192
|
+
radio_item[:label][:options] ||= {}
|
193
|
+
radio_item[:label][:options][:classes] = "govuk-radios__label #{radio_item[:label][:options][:classes]}".rstrip
|
194
|
+
|
195
|
+
set_item_options_for_hint('radios', attribute, radio_item) if radio_item[:hint]
|
196
|
+
|
197
|
+
capture do
|
198
|
+
yield
|
199
|
+
concat(govuk_hint(radio_item[:hint][:text], **radio_item[:hint][:options])) if radio_item[:hint]
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'action_view'
|
4
|
+
require_relative '../field'
|
5
|
+
|
6
|
+
module CrownMarketplaceUtils
|
7
|
+
module GovUkHelper
|
8
|
+
module Field
|
9
|
+
# = GOV.UK Select
|
10
|
+
#
|
11
|
+
# This helper is used for generating the select component from the
|
12
|
+
# {https://design-system.service.gov.uk/components/select GDS - Components - Select}
|
13
|
+
#
|
14
|
+
# This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
|
15
|
+
|
16
|
+
module Select
|
17
|
+
include ActionView::Helpers::FormOptionsHelper
|
18
|
+
include Field
|
19
|
+
|
20
|
+
# Generates the HTML for the GOV.UK Select component
|
21
|
+
#
|
22
|
+
# @param attribute [String, Symbol] the attribute of the select field
|
23
|
+
# @param items [Array] array of option items for the select, see {govuk_map_select_items}
|
24
|
+
# @param error_message [String] the error message to be displayed
|
25
|
+
# @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
|
26
|
+
#
|
27
|
+
# @option govuk_select_options [Hash] :form_group_options see {govuk_field}
|
28
|
+
# @option govuk_select_options [Hash] :label see {govuk_field}
|
29
|
+
# @option govuk_select_options [Hash] :hint see {govuk_field}
|
30
|
+
# @option govuk_select_options [Hash] :select_options ({}) the options that will be used when rendering the select field.
|
31
|
+
# See {govuk_select_field} for more details.
|
32
|
+
#
|
33
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Select
|
34
|
+
# which can then be rendered on the page
|
35
|
+
|
36
|
+
def govuk_select(attribute, items, error_message = nil, **govuk_select_options)
|
37
|
+
govuk_field(:select, attribute, error_message, **govuk_select_options) do |govuk_field_options|
|
38
|
+
concat(govuk_select_field(attribute, items, error_message, **govuk_field_options))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Generates the HTML for the GOV.UK Select component using an ActionView::Helpers::FormBuilder.
|
43
|
+
# Unlike {govuk_select}, the method will be able to automatically determine if the error message needs to be shown.
|
44
|
+
#
|
45
|
+
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the select field
|
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 govuk_select_options [Hash] :form_group_options see {govuk_field_with_form}
|
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.
|
55
|
+
#
|
56
|
+
# @return (see govuk_select)
|
57
|
+
|
58
|
+
def govuk_select_with_form(form, attribute, items, **govuk_select_options)
|
59
|
+
govuk_field_with_form(:select, form, attribute, **govuk_select_options) do |govuk_field_options, error_message|
|
60
|
+
concat(govuk_select_field_with_form(form, attribute, items, error_message, **govuk_field_options))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
# Generates the select HTML for {govuk_select}
|
67
|
+
#
|
68
|
+
# @param attribute [String, Symbol] the attribute of the select field
|
69
|
+
# @param items [Array] array of option items for the select, see {govuk_map_select_items}
|
70
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
71
|
+
# @param govuk_select_options [Hash] options that will be used in customising the HTML
|
72
|
+
#
|
73
|
+
# @option (see _govuk_select_field)
|
74
|
+
#
|
75
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select}
|
76
|
+
|
77
|
+
def govuk_select_field(attribute, items, error_message, **govuk_select_options)
|
78
|
+
_govuk_select_field(error_message, **govuk_select_options) do |govuk_select_classes, govuk_select_attributes|
|
79
|
+
select_tag(
|
80
|
+
attribute,
|
81
|
+
options_for_select(
|
82
|
+
govuk_map_select_items(items),
|
83
|
+
govuk_select_options[:selected]
|
84
|
+
),
|
85
|
+
class: govuk_select_classes,
|
86
|
+
**govuk_select_attributes
|
87
|
+
)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Generates the select HTML for {govuk_select_with_form}
|
92
|
+
#
|
93
|
+
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the select field
|
94
|
+
# @param attribute [String, Symbol] the attribute of the select field
|
95
|
+
# @param items [Array] array of option items for the select, see {govuk_map_select_items}
|
96
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
97
|
+
# @param govuk_select_options [Hash] options that will be used in customising the HTML
|
98
|
+
#
|
99
|
+
# @option (see _govuk_select_field)
|
100
|
+
#
|
101
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select_with_form}
|
102
|
+
|
103
|
+
def govuk_select_field_with_form(form, attribute, items, error_message, **govuk_select_options)
|
104
|
+
_govuk_select_field(error_message, **govuk_select_options) do |govuk_select_classes, govuk_select_attributes|
|
105
|
+
form.select(
|
106
|
+
attribute,
|
107
|
+
govuk_map_select_items(items),
|
108
|
+
govuk_select_options[:select_options] || {},
|
109
|
+
class: govuk_select_classes,
|
110
|
+
**govuk_select_attributes
|
111
|
+
)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Wrapper method used by {govuk_select_field} and {govuk_select_field_with_form} to generate the select HTML
|
116
|
+
#
|
117
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
118
|
+
# @param govuk_select_options [Hash] options that will be used in customising the HTML
|
119
|
+
#
|
120
|
+
# @option govuk_select_options [String] :classes additional CSS classes for the select HTML
|
121
|
+
# @option govuk_select_options [String] :selected (nil) the selected option
|
122
|
+
# @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
|
+
|
131
|
+
def _govuk_select_field(error_message, **govuk_select_options)
|
132
|
+
govuk_select_classes = ['govuk-select']
|
133
|
+
govuk_select_classes += ['govuk-select--error'] if error_message
|
134
|
+
govuk_select_classes << govuk_select_options[:classes]
|
135
|
+
govuk_select_options[:attributes] ||= {}
|
136
|
+
|
137
|
+
yield(govuk_select_classes, govuk_select_options[:attributes])
|
138
|
+
end
|
139
|
+
|
140
|
+
# Maps the items into an array that can be used to generate the options for
|
141
|
+
# {govuk_select_field} and {govuk_select_field_with_form}
|
142
|
+
#
|
143
|
+
# @param items [Array] array of option items for the select
|
144
|
+
#
|
145
|
+
# @option items [String] :text the text of the option item.
|
146
|
+
# If this is blank the value is used
|
147
|
+
# @option items [String] :value the value of the option item
|
148
|
+
# @option items [Hash] :attributes ({}) any additional attributes that will added as part of the option HTML
|
149
|
+
#
|
150
|
+
# @return [Array] array of option params that are used in {govuk_select_field} and {govuk_select_field_with_form}
|
151
|
+
|
152
|
+
def govuk_map_select_items(items)
|
153
|
+
items.map do |item|
|
154
|
+
[
|
155
|
+
item[:text] || item[:value],
|
156
|
+
item[:value],
|
157
|
+
(item[:attributes] || {})
|
158
|
+
]
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../field'
|
4
|
+
|
5
|
+
module CrownMarketplaceUtils
|
6
|
+
module GovUkHelper
|
7
|
+
module Field
|
8
|
+
# = GOV.UK Textarea
|
9
|
+
#
|
10
|
+
# This helper is used for generating the textarea component from the
|
11
|
+
# {https://design-system.service.gov.uk/components/textarea GDS - Components - Textarea}
|
12
|
+
#
|
13
|
+
# This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
|
14
|
+
|
15
|
+
module Textarea
|
16
|
+
include Field
|
17
|
+
|
18
|
+
# Generates the HTML for the GOV.UK textarea component
|
19
|
+
#
|
20
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
21
|
+
# @param error_message [String] the error message to be displayed
|
22
|
+
# @param govuk_textarea_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
|
23
|
+
#
|
24
|
+
# @option govuk_textarea_options [Hash] :form_group_options see {govuk_field}
|
25
|
+
# @option govuk_textarea_options [Hash] :label see {govuk_field}
|
26
|
+
# @option govuk_textarea_options [Hash] :hint see {govuk_field}
|
27
|
+
# @option govuk_textarea_options [Hash] :textarea_options ({}) the options that will be used when rendering the textarea.
|
28
|
+
# See {govuk_textarea_field} for more details.
|
29
|
+
#
|
30
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK textarea
|
31
|
+
# which can then be rendered on the page
|
32
|
+
|
33
|
+
def govuk_textarea(attribute, error_message = nil, **govuk_textarea_options)
|
34
|
+
govuk_field(:textarea, attribute, error_message, **govuk_textarea_options) do |govuk_field_options|
|
35
|
+
concat(govuk_textarea_field(attribute, error_message, **govuk_field_options))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Generates the HTML for the GOV.UK textarea component using an ActionView::Helpers::FormBuilder.
|
40
|
+
# Unlike {#govuk_textarea}, 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 textarea
|
43
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
44
|
+
# @param govuk_textarea_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
|
45
|
+
#
|
46
|
+
# @option govuk_textarea_options [Hash] :form_group_options see {govuk_field_with_form}
|
47
|
+
# @option govuk_textarea_options [Hash] :label see {govuk_field_with_form}
|
48
|
+
# @option govuk_textarea_options [Hash] :hint see {govuk_field_with_form}
|
49
|
+
# @option govuk_textarea_options [Hash] :textarea_options ({}) the options that will be used when rendering the textarea.
|
50
|
+
# See {govuk_textarea_field_with_form} for more details.
|
51
|
+
#
|
52
|
+
# @return (see govuk_textarea)
|
53
|
+
|
54
|
+
def govuk_textarea_with_form(form, attribute, **govuk_textarea_options)
|
55
|
+
govuk_field_with_form(:textarea, form, attribute, **govuk_textarea_options) do |govuk_field_options, error_message|
|
56
|
+
concat(govuk_textarea_field_with_form(form, attribute, error_message, **govuk_field_options))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# Generates the textarea HTML for {govuk_textarea}
|
63
|
+
#
|
64
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
65
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
66
|
+
# @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
|
67
|
+
#
|
68
|
+
# @option (see _govuk_textarea_field)
|
69
|
+
#
|
70
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the textarea field which is used in {govuk_textarea}
|
71
|
+
|
72
|
+
def govuk_textarea_field(attribute, error_message, **govuk_text_textarea_options)
|
73
|
+
_govuk_textarea_field(error_message, **govuk_text_textarea_options) do |govuk_text_textarea_classes, govuk_text_textarea_rows, govuk_text_textarea_attributes|
|
74
|
+
text_area_tag(attribute, govuk_text_textarea_options[:content], class: govuk_text_textarea_classes, rows: govuk_text_textarea_rows, **govuk_text_textarea_attributes)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Generates the textarea HTML for {govuk_textarea_with_form}
|
79
|
+
#
|
80
|
+
# @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the textarea
|
81
|
+
# @param attribute [String, Symbol] the attribute of the textarea
|
82
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
83
|
+
# @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
|
84
|
+
#
|
85
|
+
# @option (see _govuk_textarea_field)
|
86
|
+
#
|
87
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the textarea field which is used in {govuk_textarea_with_form}
|
88
|
+
|
89
|
+
def govuk_textarea_field_with_form(form, attribute, error_message, **govuk_text_textarea_options)
|
90
|
+
_govuk_textarea_field(error_message, **govuk_text_textarea_options) do |govuk_text_textarea_classes, govuk_text_textarea_rows, govuk_text_textarea_attributes|
|
91
|
+
form.text_area(attribute, class: govuk_text_textarea_classes, rows: govuk_text_textarea_rows, **govuk_text_textarea_attributes)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Wrapper method used by {govuk_textarea_field} and {govuk_textarea_field_with_form} to generate the textarea HTML
|
96
|
+
#
|
97
|
+
# @param error_message [String] used to indicate if there is an error which will add extra classes
|
98
|
+
# @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
|
99
|
+
#
|
100
|
+
# @option govuk_text_textarea_options [String] :classes additional CSS classes for the textarea HTML
|
101
|
+
# @option govuk_text_textarea_options [String] :content (nil) the content of the textarea
|
102
|
+
# @option govuk_text_textarea_options [String] :rows (5) the number of rows for the text area
|
103
|
+
# @option govuk_text_textarea_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
|
104
|
+
#
|
105
|
+
# @yield the textarea HTML generated by {govuk_textarea_field} or {govuk_textarea_field_with_form}
|
106
|
+
#
|
107
|
+
# @yieldparam govuk_text_textarea_classes [Array] the classes for the label HTML
|
108
|
+
# @yieldparam govuk_text_textarea_rows [Integer, String] (5) the number of rows for the text area
|
109
|
+
# @yieldparam govuk_text_textarea_attributes [Hash] additional attributes that will added as part of the HTML
|
110
|
+
#
|
111
|
+
# @return [ActiveSupport::SafeBuffer] the HTML for the textarea field which is used in {govuk_textarea_field} or {govuk_textarea_field_with_form}
|
112
|
+
|
113
|
+
def _govuk_textarea_field(error_message, **govuk_text_textarea_options)
|
114
|
+
govuk_text_textarea_classes = ['govuk-textarea']
|
115
|
+
govuk_text_textarea_classes += ['govuk-textarea--error'] if error_message
|
116
|
+
govuk_text_textarea_classes << govuk_text_textarea_options[:classes]
|
117
|
+
govuk_text_textarea_options[:attributes] ||= {}
|
118
|
+
govuk_text_textarea_options[:rows] ||= 5
|
119
|
+
|
120
|
+
yield(govuk_text_textarea_classes, govuk_text_textarea_options[:rows], govuk_text_textarea_options[:attributes])
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
|