govuk_design_system_formbuilder 5.13.0 → 6.0.0
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/README.md +5 -5
- data/lib/govuk_design_system_formbuilder/base.rb +10 -1
- data/lib/govuk_design_system_formbuilder/builder.rb +88 -18
- data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +1 -9
- data/lib/govuk_design_system_formbuilder/elements/collection_select.rb +5 -2
- data/lib/govuk_design_system_formbuilder/elements/date.rb +10 -104
- data/lib/govuk_design_system_formbuilder/elements/file.rb +5 -2
- data/lib/govuk_design_system_formbuilder/elements/inputs/email.rb +1 -0
- data/lib/govuk_design_system_formbuilder/elements/inputs/number.rb +1 -0
- data/lib/govuk_design_system_formbuilder/elements/inputs/phone.rb +1 -0
- data/lib/govuk_design_system_formbuilder/elements/inputs/text.rb +1 -0
- data/lib/govuk_design_system_formbuilder/elements/inputs/url.rb +1 -0
- data/lib/govuk_design_system_formbuilder/elements/select.rb +5 -2
- data/lib/govuk_design_system_formbuilder/elements/time.rb +66 -0
- data/lib/govuk_design_system_formbuilder/traits/content_before_and_after.rb +24 -0
- data/lib/govuk_design_system_formbuilder/traits/date_input.rb +118 -0
- data/lib/govuk_design_system_formbuilder/traits/input.rb +13 -7
- data/lib/govuk_design_system_formbuilder/version.rb +1 -1
- data/lib/govuk_design_system_formbuilder.rb +28 -4
- metadata +12 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e5cdce7fa87b070e4c8335c43ee6a23e8ad7ca61933f1ad0a3a80339c3e1959a
|
|
4
|
+
data.tar.gz: b3446d561dc3a96c23d6140ad0f966edd39517891eac18e1a3c41d894c8b48c5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d74467eeac3d4b1001452df29de4d3a6e92159346f820b7d70b06ea74a34e175748891abebdc0867245fbaafffae30bef8d36c8637104f46ce11d82ac832790d
|
|
7
|
+
data.tar.gz: c4752085f758ddf51b544c9a6b59425fdcccfc5ea316560ccafedb18a40f002bd3f228da78d8df379e3cc588266ad3fa871f84aa56b3f9c4123e31f73c2e8cba
|
data/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# GOV.UK Form Builder for Ruby on Rails
|
|
2
2
|
|
|
3
|
-
[](https://github.com/x-govuk/govuk-form-builder/actions/workflows/tests.yml)
|
|
4
4
|
[](https://badge.fury.io/rb/govuk_design_system_formbuilder)
|
|
5
5
|
[](https://rubygems.org/gems/govuk_design_system_formbuilder)
|
|
6
|
-
|
|
7
|
-
[
|
|
7
|
+
[](https://design-system.service.gov.uk)
|
|
8
|
+
[](https://weblog.rubyonrails.org/releases/)
|
|
9
|
+
[](https://www.ruby-lang.org/en/downloads/)
|
|
10
10
|
|
|
11
11
|
This library provides an easy-to-use form builder for the [GOV.UK Design System](https://design-system.service.gov.uk/).
|
|
12
12
|
|
|
@@ -80,7 +80,7 @@ module GOVUKDesignSystemFormBuilder
|
|
|
80
80
|
[
|
|
81
81
|
@object_name,
|
|
82
82
|
attribute,
|
|
83
|
-
value,
|
|
83
|
+
format_value(value),
|
|
84
84
|
id_type
|
|
85
85
|
]
|
|
86
86
|
.compact
|
|
@@ -89,6 +89,15 @@ module GOVUKDesignSystemFormBuilder
|
|
|
89
89
|
.tr(replace, delimiter)
|
|
90
90
|
end
|
|
91
91
|
|
|
92
|
+
def format_value(value)
|
|
93
|
+
case value
|
|
94
|
+
when Integer, Float
|
|
95
|
+
(value.negative?) ? "minus-#{value.abs}" : value
|
|
96
|
+
else
|
|
97
|
+
value
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
92
101
|
def warn(message)
|
|
93
102
|
return unless config.enable_logger
|
|
94
103
|
|
|
@@ -43,6 +43,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
43
43
|
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
44
44
|
# @param prefix_text [String] the text placed before the input. No prefix will be added if left +nil+
|
|
45
45
|
# @param suffix_text [String] the text placed after the input. No suffix will be added if left +nil+
|
|
46
|
+
# @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
47
|
+
# @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
46
48
|
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input
|
|
47
49
|
# @return [ActiveSupport::SafeBuffer] HTML output
|
|
48
50
|
# @see https://design-system.service.gov.uk/components/text-input/ GOV.UK Text input
|
|
@@ -66,8 +68,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
66
68
|
# = f.govuk_text_field :callsign,
|
|
67
69
|
# label: -> { tag.h3('Call-sign') }
|
|
68
70
|
#
|
|
69
|
-
def govuk_text_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, **kwargs, &block)
|
|
70
|
-
Elements::Inputs::Text.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, **kwargs, &block).html
|
|
71
|
+
def govuk_text_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, before_input: nil, after_input: nil, **kwargs, &block)
|
|
72
|
+
Elements::Inputs::Text.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, before_input:, after_input:, **kwargs, &block).html
|
|
71
73
|
end
|
|
72
74
|
|
|
73
75
|
# Generates a input of type +tel+
|
|
@@ -96,6 +98,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
96
98
|
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
97
99
|
# @param prefix_text [String] the text placed before the input. No prefix will be added if left +nil+
|
|
98
100
|
# @param suffix_text [String] the text placed after the input. No suffix will be added if left +nil+
|
|
101
|
+
# @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
102
|
+
# @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
99
103
|
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input
|
|
100
104
|
# @return [ActiveSupport::SafeBuffer] HTML output
|
|
101
105
|
# @see https://design-system.service.gov.uk/components/text-input/ GOV.UK Text input
|
|
@@ -120,8 +124,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
120
124
|
# = f.govuk_phone_field :work_number,
|
|
121
125
|
# label: -> { tag.h3('Work number') }
|
|
122
126
|
#
|
|
123
|
-
def govuk_phone_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, **kwargs, &block)
|
|
124
|
-
Elements::Inputs::Phone.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, **kwargs, &block).html
|
|
127
|
+
def govuk_phone_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, before_input: nil, after_input: nil, **kwargs, &block)
|
|
128
|
+
Elements::Inputs::Phone.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, before_input:, after_input:, **kwargs, &block).html
|
|
125
129
|
end
|
|
126
130
|
|
|
127
131
|
# Generates a input of type +email+
|
|
@@ -150,6 +154,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
150
154
|
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
151
155
|
# @param prefix_text [String] the text placed before the input. No prefix will be added if left +nil+
|
|
152
156
|
# @param suffix_text [String] the text placed after the input. No suffix will be added if left +nil+
|
|
157
|
+
# @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
158
|
+
# @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
153
159
|
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input
|
|
154
160
|
# @return [ActiveSupport::SafeBuffer] HTML output
|
|
155
161
|
# @see https://design-system.service.gov.uk/components/text-input/ GOV.UK Text input
|
|
@@ -172,8 +178,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
172
178
|
# = f.govuk_email_field :personal_email,
|
|
173
179
|
# label: -> { tag.h3('Personal email address') }
|
|
174
180
|
#
|
|
175
|
-
def govuk_email_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, **kwargs, &block)
|
|
176
|
-
Elements::Inputs::Email.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, **kwargs, &block).html
|
|
181
|
+
def govuk_email_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, before_input: nil, after_input: nil, **kwargs, &block)
|
|
182
|
+
Elements::Inputs::Email.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, before_input:, after_input:, **kwargs, &block).html
|
|
177
183
|
end
|
|
178
184
|
|
|
179
185
|
# Generates a input of type +url+
|
|
@@ -202,6 +208,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
202
208
|
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
203
209
|
# @param prefix_text [String] the text placed before the input. No prefix will be added if left +nil+
|
|
204
210
|
# @param suffix_text [String] the text placed after the input. No suffix will be added if left +nil+
|
|
211
|
+
# @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
212
|
+
# @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
205
213
|
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input
|
|
206
214
|
# @return [ActiveSupport::SafeBuffer] HTML output
|
|
207
215
|
# @see https://design-system.service.gov.uk/components/text-input/ GOV.UK Text input
|
|
@@ -224,8 +232,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
224
232
|
# = f.govuk_url_field :work_website,
|
|
225
233
|
# label: -> { tag.h3("Enter your company's website") }
|
|
226
234
|
#
|
|
227
|
-
def govuk_url_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, **kwargs, &block)
|
|
228
|
-
Elements::Inputs::URL.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, **kwargs, &block).html
|
|
235
|
+
def govuk_url_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, before_input: nil, after_input: nil, **kwargs, &block)
|
|
236
|
+
Elements::Inputs::URL.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, before_input:, after_input:, **kwargs, &block).html
|
|
229
237
|
end
|
|
230
238
|
|
|
231
239
|
# Generates a input of type +number+
|
|
@@ -254,6 +262,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
254
262
|
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
255
263
|
# @param prefix_text [String] the text placed before the input. No prefix will be added if left +nil+
|
|
256
264
|
# @param suffix_text [String] the text placed after the input. No suffix will be added if left +nil+
|
|
265
|
+
# @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
266
|
+
# @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
257
267
|
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input
|
|
258
268
|
# @return [ActiveSupport::SafeBuffer] HTML output
|
|
259
269
|
# @see https://design-system.service.gov.uk/components/text-input/ GOV.UK Text input
|
|
@@ -279,8 +289,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
279
289
|
# = f.govuk_url_field :personal_best_over_100m,
|
|
280
290
|
# label: -> { tag.h3("How many seconds does it take you to run 100m?") }
|
|
281
291
|
#
|
|
282
|
-
def govuk_number_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, **kwargs, &block)
|
|
283
|
-
Elements::Inputs::Number.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, **kwargs, &block).html
|
|
292
|
+
def govuk_number_field(attribute_name, hint: {}, label: {}, caption: {}, width: nil, extra_letter_spacing: false, form_group: {}, prefix_text: nil, suffix_text: nil, before_input: nil, after_input: nil, **kwargs, &block)
|
|
293
|
+
Elements::Inputs::Number.new(self, object_name, attribute_name, hint:, label:, caption:, width:, extra_letter_spacing:, form_group:, prefix_text:, suffix_text:, before_input:, after_input:, **kwargs, &block).html
|
|
284
294
|
end
|
|
285
295
|
|
|
286
296
|
# Generates a password input
|
|
@@ -422,6 +432,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
422
432
|
# @param options [Hash] Options hash passed through to Rails' +collection_select+ helper
|
|
423
433
|
# @param form_group [Hash] configures the form group
|
|
424
434
|
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
435
|
+
# @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
436
|
+
# @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
425
437
|
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input
|
|
426
438
|
# @see https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select Rails collection_select (called by govuk_collection_select)
|
|
427
439
|
# @return [ActiveSupport::SafeBuffer] HTML output
|
|
@@ -443,7 +455,7 @@ module GOVUKDesignSystemFormBuilder
|
|
|
443
455
|
# = f.govuk_collection_select(:team, @teams, :id, :name) do
|
|
444
456
|
# label: -> { tag.h3("Which team did you represent?") }
|
|
445
457
|
#
|
|
446
|
-
def govuk_collection_select(attribute_name, collection, value_method, text_method, options: {}, hint: {}, label: {}, caption: {}, form_group: {}, **kwargs, &block)
|
|
458
|
+
def govuk_collection_select(attribute_name, collection, value_method, text_method, options: {}, hint: {}, label: {}, caption: {}, form_group: {}, before_input: nil, after_input: nil, **kwargs, &block)
|
|
447
459
|
Elements::CollectionSelect.new(
|
|
448
460
|
self,
|
|
449
461
|
object_name,
|
|
@@ -456,6 +468,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
456
468
|
caption:,
|
|
457
469
|
options:,
|
|
458
470
|
form_group:,
|
|
471
|
+
before_input:,
|
|
472
|
+
after_input:,
|
|
459
473
|
**kwargs,
|
|
460
474
|
&block
|
|
461
475
|
).html
|
|
@@ -479,6 +493,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
479
493
|
# @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
|
|
480
494
|
# @param form_group [Hash] configures the form group
|
|
481
495
|
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
496
|
+
# @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
497
|
+
# @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
482
498
|
# @param block [Block] build the contents of the select element manually for exact control
|
|
483
499
|
# @see https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select Rails select (called by govuk_collection_select)
|
|
484
500
|
# @return [ActiveSupport::SafeBuffer] HTML output
|
|
@@ -492,8 +508,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
492
508
|
#
|
|
493
509
|
# = f.govuk_select :hat_colour, options_for_select(@colours)
|
|
494
510
|
#
|
|
495
|
-
def govuk_select(attribute_name, choices = nil, options: {}, label: {}, hint: {}, form_group: {}, caption: {}, **kwargs, &block)
|
|
496
|
-
Elements::Select.new(self, object_name, attribute_name, choices, options:, label:, hint:, form_group:, caption:, **kwargs, &block).html
|
|
511
|
+
def govuk_select(attribute_name, choices = nil, options: {}, label: {}, hint: {}, form_group: {}, caption: {}, before_input: nil, after_input: nil, **kwargs, &block)
|
|
512
|
+
Elements::Select.new(self, object_name, attribute_name, choices, options:, label:, hint:, form_group:, caption:, before_input:, after_input:, **kwargs, &block).html
|
|
497
513
|
end
|
|
498
514
|
|
|
499
515
|
# Generates a radio button for each item in the supplied collection
|
|
@@ -943,6 +959,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
943
959
|
# @param form_group [Hash] configures the form group
|
|
944
960
|
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
945
961
|
# @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element
|
|
962
|
+
# @param before_inputs [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
963
|
+
# @param after_inputs [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
946
964
|
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input group
|
|
947
965
|
# @param date_of_birth [Boolean] if +true+ {https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete#Values birth date auto completion attributes}
|
|
948
966
|
# will be added to the inputs
|
|
@@ -957,13 +975,63 @@ module GOVUKDesignSystemFormBuilder
|
|
|
957
975
|
# hint: { text: 'Leave this field blank if you don't know exactly' } do
|
|
958
976
|
#
|
|
959
977
|
# p.govuk-inset-text
|
|
960
|
-
# | If you don't fill this in you won't be
|
|
978
|
+
# | If you don't fill this in you won't be eligible for a refund
|
|
961
979
|
#
|
|
962
980
|
# @example A date input with legend supplied as a proc
|
|
963
981
|
# = f.govuk_date_field :finishes_on,
|
|
964
982
|
# legend: -> { tag.h3('Which category do you belong to?') }
|
|
965
|
-
def govuk_date_field(attribute_name, hint: {}, legend: {}, caption: {}, date_of_birth: false, omit_day: false, maxlength_enabled: false, segments: config.default_date_segments, segment_names: config.default_date_segment_names, form_group: {}, **kwargs, &block)
|
|
966
|
-
Elements::Date.new(self, object_name, attribute_name, hint:, legend:, caption:, date_of_birth:, omit_day:, maxlength_enabled:, segments:, segment_names:, form_group:, **kwargs, &block).html
|
|
983
|
+
def govuk_date_field(attribute_name, hint: {}, legend: {}, caption: {}, date_of_birth: false, omit_day: false, maxlength_enabled: false, segments: config.default_date_segments, segment_names: config.default_date_segment_names, before_inputs: nil, after_inputs: nil, form_group: {}, **kwargs, &block)
|
|
984
|
+
Elements::Date.new(self, object_name, attribute_name, hint:, legend:, caption:, date_of_birth:, omit_day:, maxlength_enabled:, segments:, segment_names:, form_group:, before_inputs:, after_inputs:, **kwargs, &block).html
|
|
985
|
+
end
|
|
986
|
+
|
|
987
|
+
# Generates three inputs for the +hour+, +minute+ and +second+ components of a time
|
|
988
|
+
#
|
|
989
|
+
# @note When using this input values will be retrieved from the attribute if it is a Time object or a multiparam date hash
|
|
990
|
+
# @param attribute_name [Symbol] The name of the attribute
|
|
991
|
+
# @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+. When a +Proc+ is
|
|
992
|
+
# supplied the hint will be wrapped in a +div+ instead of a +span+
|
|
993
|
+
# @option hint text [String] the hint text
|
|
994
|
+
# @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
|
|
995
|
+
# @param legend [NilClass,Hash,Proc] options for configuring the legend. Legend will be omitted if +nil+.
|
|
996
|
+
# @option legend text [String] the fieldset legend's text content
|
|
997
|
+
# @option legend size [String] the size of the fieldset legend font, can be +xl+, +l+, +m+ or +s+
|
|
998
|
+
# @option legend tag [Symbol,String] the tag used for the fieldset's header, defaults to +h1+.
|
|
999
|
+
# @option legend hidden [Boolean] control the visibility of the legend. Hidden legends will still be read by screenreaders
|
|
1000
|
+
# @option legend kwargs [Hash] additional arguments are applied as attributes on the +legend+ element
|
|
1001
|
+
# @param caption [Hash] configures or sets the caption content which is inserted above the legend
|
|
1002
|
+
# @option caption text [String] the caption text
|
|
1003
|
+
# @option caption size [String] the size of the caption, can be +xl+, +l+ or +m+. Defaults to +m+
|
|
1004
|
+
# @option caption kwargs [Hash] additional arguments are applied as attributes on the caption +span+ element
|
|
1005
|
+
# @param omit_second [Boolean] do not render a second input, only capture hour and minute
|
|
1006
|
+
# @param maxlength_enabled [Boolean] adds maxlength attribute to hour, minute and second inputs (2)
|
|
1007
|
+
# @param segments [Hash] allows Rails' multiparameter attributes to be overridden on a field-by-field basis. Hash must
|
|
1008
|
+
# contain +hour:+, +minute:+ and +second:+ keys. Defaults to the default value set in the +default_time_segments+ setting in {GOVUKDesignSystemFormBuilder.DEFAULTS}
|
|
1009
|
+
# @param segment_names [Hash] allows the day, month and year labels to be overridden. Hash must
|
|
1010
|
+
# contain +hour:+, +minute:+ and +second:+ keys. Defaults to the default value set in the +default_time_segment_names+ setting in {GOVUKDesignSystemFormBuilder.DEFAULTS}
|
|
1011
|
+
# @param form_group [Hash] configures the form group
|
|
1012
|
+
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
1013
|
+
# @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element
|
|
1014
|
+
# @param before_inputs [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
1015
|
+
# @param after_inputs [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
1016
|
+
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input group
|
|
1017
|
+
# @return [ActiveSupport::SafeBuffer] HTML output
|
|
1018
|
+
#
|
|
1019
|
+
# @see https://github.com/alphagov/govuk-frontend/issues/1449 GOV.UK date input element attributes, using text instead of number
|
|
1020
|
+
# @see https://design-system.service.gov.uk/styles/typography/#headings-with-captions Headings with captions
|
|
1021
|
+
#
|
|
1022
|
+
# @example A regular time input with a legend, hint and injected content
|
|
1023
|
+
# = f.govuk_time_field :starts_at,
|
|
1024
|
+
# legend: { 'When does your event start?' },
|
|
1025
|
+
# hint: { text: 'Leave this field blank if you don't know exactly' } do
|
|
1026
|
+
#
|
|
1027
|
+
# p.govuk-inset-text
|
|
1028
|
+
# | If you don't fill this, the event will be all day
|
|
1029
|
+
#
|
|
1030
|
+
# @example A time input with legend supplied as a proc
|
|
1031
|
+
# = f.govuk_time_field :finishes_at,
|
|
1032
|
+
# legend: -> { tag.h3('When does the event finish?') }
|
|
1033
|
+
def govuk_time_field(attribute_name, hint: {}, legend: {}, caption: {}, omit_second: false, maxlength_enabled: false, segments: config.default_time_segments, segment_names: config.default_time_segment_names, before_inputs: nil, after_inputs: nil, form_group: {}, **kwargs, &block)
|
|
1034
|
+
Elements::Time.new(self, object_name, attribute_name, hint:, legend:, caption:, omit_second:, maxlength_enabled:, segments:, segment_names:, form_group:, before_inputs:, after_inputs:, **kwargs, &block).html
|
|
967
1035
|
end
|
|
968
1036
|
|
|
969
1037
|
# Generates a summary of errors in the form, each linking to the corresponding
|
|
@@ -1049,6 +1117,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
1049
1117
|
# @param form_group [Hash] configures the form group
|
|
1050
1118
|
# @option form_group kwargs [Hash] additional attributes added to the form group
|
|
1051
1119
|
# @param javascript [Boolean] Configures whether to add HTML for the javascript-enhanced version of the component
|
|
1120
|
+
# @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
|
|
1121
|
+
# @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
|
|
1052
1122
|
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input
|
|
1053
1123
|
#
|
|
1054
1124
|
# @example A photo upload field with file type specifier and injected content
|
|
@@ -1067,8 +1137,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
1067
1137
|
# @note Remember to set +multipart: true+ when creating a form with file
|
|
1068
1138
|
# uploads, {https://guides.rubyonrails.org/form_helpers.html#uploading-files see
|
|
1069
1139
|
# the Rails documentation} for more information
|
|
1070
|
-
def govuk_file_field(attribute_name, label: {}, caption: {}, hint: {}, form_group: {}, javascript: false, **kwargs, &block)
|
|
1071
|
-
Elements::File.new(self, object_name, attribute_name, label:, caption:, hint:, form_group:, javascript:, **kwargs, &block).html
|
|
1140
|
+
def govuk_file_field(attribute_name, label: {}, caption: {}, hint: {}, form_group: {}, javascript: false, before_input: nil, after_input: nil, **kwargs, &block)
|
|
1141
|
+
Elements::File.new(self, object_name, attribute_name, label:, caption:, hint:, form_group:, javascript:, before_input:, after_input:, **kwargs, &block).html
|
|
1072
1142
|
end
|
|
1073
1143
|
end
|
|
1074
1144
|
end
|
|
@@ -10,15 +10,7 @@ module GOVUKDesignSystemFormBuilder
|
|
|
10
10
|
def html
|
|
11
11
|
return if @content.blank?
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
tag.div(id: supplemental_id) { @content }
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
private
|
|
19
|
-
|
|
20
|
-
def supplemental_id
|
|
21
|
-
build_id('supplemental')
|
|
13
|
+
tag.div { @content }
|
|
22
14
|
end
|
|
23
15
|
end
|
|
24
16
|
end
|
|
@@ -7,8 +7,9 @@ module GOVUKDesignSystemFormBuilder
|
|
|
7
7
|
include Traits::Supplemental
|
|
8
8
|
include Traits::HTMLAttributes
|
|
9
9
|
include Traits::Select
|
|
10
|
+
include Traits::ContentBeforeAndAfter
|
|
10
11
|
|
|
11
|
-
def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint:, label:, caption:, form_group:, options: {}, **kwargs, &block)
|
|
12
|
+
def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint:, label:, caption:, form_group:, before_input:, after_input:, options: {}, **kwargs, &block)
|
|
12
13
|
super(builder, object_name, attribute_name, &block)
|
|
13
14
|
|
|
14
15
|
@collection = collection
|
|
@@ -20,6 +21,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
20
21
|
@hint = hint
|
|
21
22
|
@form_group = form_group
|
|
22
23
|
@html_attributes = kwargs
|
|
24
|
+
@before_input = before_input
|
|
25
|
+
@after_input = after_input
|
|
23
26
|
|
|
24
27
|
# FIXME remove this soon, worth informing people who miss the release notes that the
|
|
25
28
|
# args have changed though.
|
|
@@ -30,7 +33,7 @@ module GOVUKDesignSystemFormBuilder
|
|
|
30
33
|
|
|
31
34
|
def html
|
|
32
35
|
Containers::FormGroup.new(*bound, **@form_group).html do
|
|
33
|
-
safe_join([label_element, supplemental_content, hint_element, error_element, collection_select])
|
|
36
|
+
safe_join([label_element, supplemental_content, hint_element, error_element, before_input_content, collection_select, after_input_content])
|
|
34
37
|
end
|
|
35
38
|
end
|
|
36
39
|
|
|
@@ -8,10 +8,12 @@ module GOVUKDesignSystemFormBuilder
|
|
|
8
8
|
include Traits::Supplemental
|
|
9
9
|
include Traits::HTMLClasses
|
|
10
10
|
include Traits::Localisation
|
|
11
|
+
include Traits::DateInput
|
|
12
|
+
include Traits::ContentBeforeAndAfter
|
|
11
13
|
|
|
12
14
|
MULTIPARAMETER_KEY = { day: 3, month: 2, year: 1 }.freeze
|
|
13
15
|
|
|
14
|
-
def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_day:, maxlength_enabled:, segments:, form_group:, segment_names:, date_of_birth: false, **kwargs, &block)
|
|
16
|
+
def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_day:, maxlength_enabled:, segments:, form_group:, segment_names:, before_inputs:, after_inputs:, date_of_birth: false, **kwargs, &block)
|
|
15
17
|
super(builder, object_name, attribute_name, &block)
|
|
16
18
|
|
|
17
19
|
@legend = legend
|
|
@@ -24,36 +26,24 @@ module GOVUKDesignSystemFormBuilder
|
|
|
24
26
|
@segment_names = segment_names
|
|
25
27
|
@form_group = form_group
|
|
26
28
|
@html_attributes = kwargs
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def html
|
|
30
|
-
Containers::FormGroup.new(*bound, **@form_group, **@html_attributes).html do
|
|
31
|
-
Containers::Fieldset.new(*bound, **fieldset_options).html do
|
|
32
|
-
safe_join([supplemental_content, hint_element, error_element, date])
|
|
33
|
-
end
|
|
34
|
-
end
|
|
29
|
+
@before_input = before_inputs
|
|
30
|
+
@after_input = after_inputs
|
|
35
31
|
end
|
|
36
32
|
|
|
37
33
|
private
|
|
38
34
|
|
|
39
|
-
def
|
|
40
|
-
|
|
35
|
+
def parts
|
|
36
|
+
[day, month, year]
|
|
41
37
|
end
|
|
42
38
|
|
|
43
|
-
def
|
|
44
|
-
|
|
45
|
-
safe_join([day, month, year])
|
|
46
|
-
end
|
|
39
|
+
def fieldset_options
|
|
40
|
+
{ legend: @legend, caption: @caption, described_by: [error_id, hint_id] }
|
|
47
41
|
end
|
|
48
42
|
|
|
49
43
|
def omit_day?
|
|
50
44
|
@omit_day
|
|
51
45
|
end
|
|
52
46
|
|
|
53
|
-
def maxlength_enabled?
|
|
54
|
-
@maxlength_enabled
|
|
55
|
-
end
|
|
56
|
-
|
|
57
47
|
def day
|
|
58
48
|
if omit_day?
|
|
59
49
|
return tag.input(
|
|
@@ -75,95 +65,11 @@ module GOVUKDesignSystemFormBuilder
|
|
|
75
65
|
date_part(:year, width: 4)
|
|
76
66
|
end
|
|
77
67
|
|
|
78
|
-
def
|
|
79
|
-
tag.div(class: %(#{brand}-date-input__item)) do
|
|
80
|
-
tag.div(class: %(#{brand}-form-group)) do
|
|
81
|
-
safe_join([label(segment, link_errors), input(segment, link_errors, width, value(segment))])
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
def value(segment)
|
|
87
|
-
attribute = @builder.object.try(@attribute_name)
|
|
88
|
-
|
|
89
|
-
return unless attribute
|
|
90
|
-
|
|
91
|
-
if attribute.respond_to?(segment)
|
|
92
|
-
attribute.send(segment)
|
|
93
|
-
elsif attribute.respond_to?(:fetch)
|
|
94
|
-
attribute.fetch(MULTIPARAMETER_KEY[segment]) do
|
|
95
|
-
warn("No key '#{segment}' found in MULTIPARAMETER_KEY hash. Expected to find #{MULTIPARAMETER_KEY.values}")
|
|
96
|
-
|
|
97
|
-
nil
|
|
98
|
-
end
|
|
99
|
-
else
|
|
100
|
-
fail(ArgumentError, "invalid Date-like object: must be a Date, Time, DateTime or Hash in MULTIPARAMETER_KEY format")
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
def label(segment, link_errors)
|
|
105
|
-
tag.label(
|
|
106
|
-
segment_label_text(segment),
|
|
107
|
-
class: label_classes,
|
|
108
|
-
for: id(segment, link_errors)
|
|
109
|
-
)
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
def segment_label_text(segment)
|
|
113
|
-
localised_text(:label, segment) || @segment_names.fetch(segment)
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
def input(segment, link_errors, width, value)
|
|
117
|
-
tag.input(
|
|
118
|
-
id: id(segment, link_errors),
|
|
119
|
-
class: classes(width),
|
|
120
|
-
name: name(segment),
|
|
121
|
-
type: 'text',
|
|
122
|
-
inputmode: 'numeric',
|
|
123
|
-
value:,
|
|
124
|
-
autocomplete: date_of_birth_autocomplete_value(segment),
|
|
125
|
-
maxlength: (width if maxlength_enabled?),
|
|
126
|
-
)
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
def classes(width)
|
|
130
|
-
build_classes(
|
|
131
|
-
%(input),
|
|
132
|
-
%(date-input__input),
|
|
133
|
-
%(input--width-#{width}),
|
|
134
|
-
%(input--error) => has_errors?,
|
|
135
|
-
).prefix(brand)
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
# if the field has errors we want the govuk_error_summary to
|
|
139
|
-
# be able to link to the day field. Otherwise, generate IDs
|
|
140
|
-
# in the normal fashion
|
|
141
|
-
def id(segment, link_errors)
|
|
142
|
-
if has_errors? && link_errors
|
|
143
|
-
field_id(link_errors:)
|
|
144
|
-
else
|
|
145
|
-
[@object_name, @attribute_name, @segments.fetch(segment)].join("_")
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
def name(segment)
|
|
150
|
-
format(
|
|
151
|
-
"%<object_name>s[%<input_name>s(%<segment>s)]",
|
|
152
|
-
object_name: @object_name,
|
|
153
|
-
input_name: @attribute_name,
|
|
154
|
-
segment: @segments.fetch(segment)
|
|
155
|
-
)
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
def date_of_birth_autocomplete_value(segment)
|
|
68
|
+
def autocomplete_value(segment)
|
|
159
69
|
return unless @date_of_birth
|
|
160
70
|
|
|
161
71
|
{ day: 'bday-day', month: 'bday-month', year: 'bday-year' }.fetch(segment)
|
|
162
72
|
end
|
|
163
|
-
|
|
164
|
-
def label_classes
|
|
165
|
-
build_classes(%(label), %(date-input__label)).prefix(brand)
|
|
166
|
-
end
|
|
167
73
|
end
|
|
168
74
|
end
|
|
169
75
|
end
|
|
@@ -9,8 +9,9 @@ module GOVUKDesignSystemFormBuilder
|
|
|
9
9
|
include Traits::Supplemental
|
|
10
10
|
include Traits::HTMLAttributes
|
|
11
11
|
include Traits::HTMLClasses
|
|
12
|
+
include Traits::ContentBeforeAndAfter
|
|
12
13
|
|
|
13
|
-
def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, javascript:, **kwargs, &block)
|
|
14
|
+
def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, javascript:, before_input:, after_input:, **kwargs, &block)
|
|
14
15
|
super(builder, object_name, attribute_name, &block)
|
|
15
16
|
|
|
16
17
|
@label = label
|
|
@@ -19,11 +20,13 @@ module GOVUKDesignSystemFormBuilder
|
|
|
19
20
|
@html_attributes = kwargs
|
|
20
21
|
@form_group = form_group
|
|
21
22
|
@javascript = javascript
|
|
23
|
+
@before_input = before_input
|
|
24
|
+
@after_input = after_input
|
|
22
25
|
end
|
|
23
26
|
|
|
24
27
|
def html
|
|
25
28
|
Containers::FormGroup.new(*bound, **@form_group).html do
|
|
26
|
-
safe_join([label_element, supplemental_content, hint_element, error_element, file_html])
|
|
29
|
+
safe_join([label_element, supplemental_content, hint_element, error_element, before_input_content, file_html, after_input_content])
|
|
27
30
|
end
|
|
28
31
|
end
|
|
29
32
|
|
|
@@ -6,8 +6,9 @@ module GOVUKDesignSystemFormBuilder
|
|
|
6
6
|
include Traits::Hint
|
|
7
7
|
include Traits::HTMLAttributes
|
|
8
8
|
include Traits::Select
|
|
9
|
+
include Traits::ContentBeforeAndAfter
|
|
9
10
|
|
|
10
|
-
def initialize(builder, object_name, attribute_name, choices, options:, form_group:, label:, hint:, caption:, **kwargs, &block)
|
|
11
|
+
def initialize(builder, object_name, attribute_name, choices, options:, form_group:, label:, hint:, caption:, before_input:, after_input:, **kwargs, &block)
|
|
11
12
|
# assign the block to an variable rather than passing to super so
|
|
12
13
|
# we can send it through to #select
|
|
13
14
|
super(builder, object_name, attribute_name)
|
|
@@ -20,11 +21,13 @@ module GOVUKDesignSystemFormBuilder
|
|
|
20
21
|
@choices = choices
|
|
21
22
|
@options = options
|
|
22
23
|
@html_attributes = kwargs
|
|
24
|
+
@before_input = before_input
|
|
25
|
+
@after_input = after_input
|
|
23
26
|
end
|
|
24
27
|
|
|
25
28
|
def html
|
|
26
29
|
Containers::FormGroup.new(*bound, **@form_group).html do
|
|
27
|
-
safe_join([label_element, hint_element, error_element, select])
|
|
30
|
+
safe_join([label_element, hint_element, error_element, before_input_content, select, after_input_content])
|
|
28
31
|
end
|
|
29
32
|
end
|
|
30
33
|
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
module GOVUKDesignSystemFormBuilder
|
|
2
|
+
module Elements
|
|
3
|
+
class Time < Base
|
|
4
|
+
using PrefixableArray
|
|
5
|
+
|
|
6
|
+
include Traits::Error
|
|
7
|
+
include Traits::Hint
|
|
8
|
+
include Traits::Supplemental
|
|
9
|
+
include Traits::HTMLClasses
|
|
10
|
+
include Traits::Localisation
|
|
11
|
+
include Traits::DateInput
|
|
12
|
+
include Traits::ContentBeforeAndAfter
|
|
13
|
+
|
|
14
|
+
def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_second:, maxlength_enabled:, segments:, form_group:, before_inputs:, after_inputs:, segment_names:, **kwargs, &block)
|
|
15
|
+
super(builder, object_name, attribute_name, &block)
|
|
16
|
+
|
|
17
|
+
@legend = legend
|
|
18
|
+
@caption = caption
|
|
19
|
+
@hint = hint
|
|
20
|
+
@omit_second = omit_second
|
|
21
|
+
@maxlength_enabled = maxlength_enabled
|
|
22
|
+
@segments = segments
|
|
23
|
+
@segment_names = segment_names
|
|
24
|
+
@form_group = form_group
|
|
25
|
+
@html_attributes = kwargs
|
|
26
|
+
@before_input = before_inputs
|
|
27
|
+
@after_input = after_inputs
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def parts
|
|
33
|
+
[hour, minute, second]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def omit_second?
|
|
37
|
+
@omit_second
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def hour
|
|
41
|
+
date_part(:hour, width: 2, link_errors: true)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def minute
|
|
45
|
+
date_part(:minute, width: 2)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def second
|
|
49
|
+
if omit_second?
|
|
50
|
+
return tag.input(
|
|
51
|
+
id: id(:second, false),
|
|
52
|
+
name: name(:second),
|
|
53
|
+
type: 'hidden',
|
|
54
|
+
value: value(:second) || 0,
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
date_part(:second, width: 2)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def autocomplete_value(_segment)
|
|
62
|
+
nil
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module GOVUKDesignSystemFormBuilder
|
|
2
|
+
module Traits
|
|
3
|
+
module ContentBeforeAndAfter
|
|
4
|
+
def before_input_content
|
|
5
|
+
build_content(@before_input)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def after_input_content
|
|
9
|
+
build_content(@after_input)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def build_content(content)
|
|
15
|
+
case content
|
|
16
|
+
when String
|
|
17
|
+
content
|
|
18
|
+
when Proc
|
|
19
|
+
content.call
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
module GOVUKDesignSystemFormBuilder
|
|
2
|
+
module Traits
|
|
3
|
+
module DateInput
|
|
4
|
+
using PrefixableArray
|
|
5
|
+
|
|
6
|
+
MULTIPARAMETER_KEY = { year: 1, month: 2, day: 3, hour: 4, minute: 5, second: 6 }.freeze
|
|
7
|
+
SEGMENT_METHOD = { year: :year, month: :month, day: :day, hour: :hour, minute: :min, second: :sec }.freeze
|
|
8
|
+
|
|
9
|
+
def html
|
|
10
|
+
Containers::FormGroup.new(*bound, **@form_group, **@html_attributes).html do
|
|
11
|
+
Containers::Fieldset.new(*bound, **fieldset_options).html do
|
|
12
|
+
safe_join([supplemental_content, hint_element, error_element, date_input])
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def date_input
|
|
20
|
+
tag.div(class: %(#{brand}-date-input)) do
|
|
21
|
+
safe_join([before_input_content, *parts, after_input_content])
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def maxlength_enabled?
|
|
26
|
+
@maxlength_enabled
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def fieldset_options
|
|
30
|
+
{ legend: @legend, caption: @caption, described_by: [error_id, hint_id, supplemental_id] }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def segment_label_text(segment)
|
|
34
|
+
localised_text(:label, segment) || @segment_names.fetch(segment)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def classes(width)
|
|
38
|
+
build_classes(
|
|
39
|
+
%(input),
|
|
40
|
+
%(date-input__input),
|
|
41
|
+
%(input--width-#{width}),
|
|
42
|
+
%(input--error) => has_errors?,
|
|
43
|
+
).prefix(brand)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def date_part(segment, width:, link_errors: false)
|
|
47
|
+
tag.div(class: %(#{brand}-date-input__item)) do
|
|
48
|
+
tag.div(class: %(#{brand}-form-group)) do
|
|
49
|
+
safe_join([label(segment, link_errors), input(segment, link_errors, width, value(segment))])
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def value(segment)
|
|
55
|
+
attribute = @builder.object.try(@attribute_name)
|
|
56
|
+
|
|
57
|
+
return unless attribute
|
|
58
|
+
|
|
59
|
+
if attribute.respond_to?(:fetch)
|
|
60
|
+
attribute.fetch(MULTIPARAMETER_KEY[segment]) do
|
|
61
|
+
warn("No key '#{segment}' found in MULTIPARAMETER_KEY hash. Expected to find #{MULTIPARAMETER_KEY.values}")
|
|
62
|
+
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
elsif attribute.respond_to?(SEGMENT_METHOD[segment])
|
|
66
|
+
attribute.send(SEGMENT_METHOD.fetch(segment))
|
|
67
|
+
else
|
|
68
|
+
fail(ArgumentError, "invalid Date-like object: must be a Date, Time, DateTime or Hash in MULTIPARAMETER_KEY format")
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def label(segment, link_errors)
|
|
73
|
+
tag.label(
|
|
74
|
+
segment_label_text(segment),
|
|
75
|
+
class: label_classes,
|
|
76
|
+
for: id(segment, link_errors)
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def input(segment, link_errors, width, value)
|
|
81
|
+
tag.input(
|
|
82
|
+
id: id(segment, link_errors),
|
|
83
|
+
class: classes(width),
|
|
84
|
+
name: name(segment),
|
|
85
|
+
type: 'text',
|
|
86
|
+
inputmode: 'numeric',
|
|
87
|
+
value:,
|
|
88
|
+
autocomplete: autocomplete_value(segment),
|
|
89
|
+
maxlength: (width if maxlength_enabled?),
|
|
90
|
+
)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# if the field has errors we want the govuk_error_summary to
|
|
94
|
+
# be able to link to the hour field. Otherwise, generate IDs
|
|
95
|
+
# in the normal fashion
|
|
96
|
+
def id(segment, link_errors)
|
|
97
|
+
if has_errors? && link_errors
|
|
98
|
+
field_id(link_errors:)
|
|
99
|
+
else
|
|
100
|
+
[@object_name, @attribute_name, @segments.fetch(segment)].join("_")
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def name(segment)
|
|
105
|
+
format(
|
|
106
|
+
"%<object_name>s[%<input_name>s(%<segment>s)]",
|
|
107
|
+
object_name: @object_name,
|
|
108
|
+
input_name: @attribute_name,
|
|
109
|
+
segment: @segments.fetch(segment)
|
|
110
|
+
)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def label_classes
|
|
114
|
+
build_classes(%(label), %(date-input__label)).prefix(brand)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module GOVUKDesignSystemFormBuilder
|
|
2
2
|
module Traits
|
|
3
3
|
module Input
|
|
4
|
-
def initialize(builder, object_name, attribute_name, hint:, label:, caption:, prefix_text:, suffix_text:, width:, extra_letter_spacing:, form_group:, **kwargs, &block)
|
|
4
|
+
def initialize(builder, object_name, attribute_name, hint:, label:, caption:, prefix_text:, suffix_text:, width:, extra_letter_spacing:, form_group:, before_input:, after_input:, **kwargs, &block)
|
|
5
5
|
super(builder, object_name, attribute_name, &block)
|
|
6
6
|
|
|
7
7
|
@width = width
|
|
@@ -13,6 +13,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
13
13
|
@html_attributes = kwargs
|
|
14
14
|
@form_group = form_group
|
|
15
15
|
@extra_letter_spacing = extra_letter_spacing
|
|
16
|
+
@before_input = before_input
|
|
17
|
+
@after_input = after_input
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
def html
|
|
@@ -24,16 +26,20 @@ module GOVUKDesignSystemFormBuilder
|
|
|
24
26
|
private
|
|
25
27
|
|
|
26
28
|
def content
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
safe_join([
|
|
30
|
+
before_input_content,
|
|
31
|
+
if affixed?
|
|
32
|
+
affixed_input
|
|
33
|
+
else
|
|
34
|
+
input
|
|
35
|
+
end,
|
|
36
|
+
after_input_content
|
|
37
|
+
])
|
|
32
38
|
end
|
|
33
39
|
|
|
34
40
|
def affixed_input
|
|
35
41
|
tag.div(class: %(#{brand}-input__wrapper)) do
|
|
36
|
-
safe_join([prefix, input, suffix])
|
|
42
|
+
safe_join([prefix, input, suffix].compact)
|
|
37
43
|
end
|
|
38
44
|
end
|
|
39
45
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require 'active_support/
|
|
1
|
+
require 'active_support/ordered_options'
|
|
2
2
|
require 'html_attributes_utils'
|
|
3
3
|
|
|
4
4
|
[%w(presenters *.rb), %w(refinements *.rb), %w(traits *.rb), %w(*.rb), %w(elements ** *.rb), %w(containers ** *.rb)]
|
|
@@ -6,7 +6,16 @@ require 'html_attributes_utils'
|
|
|
6
6
|
.each { |file| require file }
|
|
7
7
|
|
|
8
8
|
module GOVUKDesignSystemFormBuilder
|
|
9
|
-
|
|
9
|
+
class << self
|
|
10
|
+
class_attribute :config,
|
|
11
|
+
instance_predicate: false,
|
|
12
|
+
default: ActiveSupport::OrderedOptions.new
|
|
13
|
+
|
|
14
|
+
def inherited(klass) # :nodoc:
|
|
15
|
+
klass.config = ActiveSupport::InheritableOptions.new(config)
|
|
16
|
+
super
|
|
17
|
+
end
|
|
18
|
+
end
|
|
10
19
|
|
|
11
20
|
# @!group Defaults
|
|
12
21
|
|
|
@@ -102,6 +111,8 @@ module GOVUKDesignSystemFormBuilder
|
|
|
102
111
|
default_submit_button_text: 'Continue',
|
|
103
112
|
default_date_segments: { day: '3i', month: '2i', year: '1i' },
|
|
104
113
|
default_date_segment_names: { day: 'Day', month: 'Month', year: 'Year' },
|
|
114
|
+
default_time_segments: { hour: '4i', minute: '5i', second: '6i' },
|
|
115
|
+
default_time_segment_names: { hour: 'Hour', minute: 'Minute', second: 'Second' },
|
|
105
116
|
default_radio_divider_text: 'or',
|
|
106
117
|
default_check_box_divider_text: 'or',
|
|
107
118
|
default_error_message_prefix: 'Error',
|
|
@@ -133,7 +144,20 @@ module GOVUKDesignSystemFormBuilder
|
|
|
133
144
|
enable_nested_localisation: true,
|
|
134
145
|
}.freeze
|
|
135
146
|
|
|
136
|
-
DEFAULTS.
|
|
147
|
+
DEFAULTS.each do |key, value|
|
|
148
|
+
reader = "def #{key}; config.#{key}; end"
|
|
149
|
+
reader_line = __LINE__
|
|
150
|
+
writer = "def #{key}=(value); config.#{key} = value; end"
|
|
151
|
+
writer_line = __LINE__
|
|
152
|
+
|
|
153
|
+
singleton_class.class_eval reader, __FILE__, reader_line
|
|
154
|
+
singleton_class.class_eval writer, __FILE__, writer_line
|
|
155
|
+
|
|
156
|
+
class_eval reader, __FILE__, reader_line
|
|
157
|
+
class_eval writer, __FILE__, writer_line
|
|
158
|
+
|
|
159
|
+
send("#{key}=", value)
|
|
160
|
+
end
|
|
137
161
|
# @!endgroup
|
|
138
162
|
|
|
139
163
|
class << self
|
|
@@ -146,7 +170,7 @@ module GOVUKDesignSystemFormBuilder
|
|
|
146
170
|
# conf.default_error_summary_title = 'OMG'
|
|
147
171
|
# end
|
|
148
172
|
def configure
|
|
149
|
-
yield
|
|
173
|
+
yield config
|
|
150
174
|
end
|
|
151
175
|
|
|
152
176
|
# Resets each of the configurable values to its default
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: govuk_design_system_formbuilder
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 6.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Peter Yates
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-02-28 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: html-attributes-utils
|
|
@@ -128,14 +128,14 @@ dependencies:
|
|
|
128
128
|
requirements:
|
|
129
129
|
- - "~>"
|
|
130
130
|
- !ruby/object:Gem::Version
|
|
131
|
-
version: 5.
|
|
131
|
+
version: 5.2.0
|
|
132
132
|
type: :development
|
|
133
133
|
prerelease: false
|
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
|
135
135
|
requirements:
|
|
136
136
|
- - "~>"
|
|
137
137
|
- !ruby/object:Gem::Version
|
|
138
|
-
version: 5.
|
|
138
|
+
version: 5.2.0
|
|
139
139
|
- !ruby/object:Gem::Dependency
|
|
140
140
|
name: simplecov
|
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -170,28 +170,28 @@ dependencies:
|
|
|
170
170
|
requirements:
|
|
171
171
|
- - "~>"
|
|
172
172
|
- !ruby/object:Gem::Version
|
|
173
|
-
version: 4.
|
|
173
|
+
version: 4.14.6
|
|
174
174
|
type: :development
|
|
175
175
|
prerelease: false
|
|
176
176
|
version_requirements: !ruby/object:Gem::Requirement
|
|
177
177
|
requirements:
|
|
178
178
|
- - "~>"
|
|
179
179
|
- !ruby/object:Gem::Version
|
|
180
|
-
version: 4.
|
|
180
|
+
version: 4.14.6
|
|
181
181
|
- !ruby/object:Gem::Dependency
|
|
182
182
|
name: rouge
|
|
183
183
|
requirement: !ruby/object:Gem::Requirement
|
|
184
184
|
requirements:
|
|
185
185
|
- - "~>"
|
|
186
186
|
- !ruby/object:Gem::Version
|
|
187
|
-
version: 4.
|
|
187
|
+
version: 4.7.0
|
|
188
188
|
type: :development
|
|
189
189
|
prerelease: false
|
|
190
190
|
version_requirements: !ruby/object:Gem::Requirement
|
|
191
191
|
requirements:
|
|
192
192
|
- - "~>"
|
|
193
193
|
- !ruby/object:Gem::Version
|
|
194
|
-
version: 4.
|
|
194
|
+
version: 4.7.0
|
|
195
195
|
- !ruby/object:Gem::Dependency
|
|
196
196
|
name: rubypants
|
|
197
197
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -337,11 +337,14 @@ files:
|
|
|
337
337
|
- lib/govuk_design_system_formbuilder/elements/select.rb
|
|
338
338
|
- lib/govuk_design_system_formbuilder/elements/submit.rb
|
|
339
339
|
- lib/govuk_design_system_formbuilder/elements/text_area.rb
|
|
340
|
+
- lib/govuk_design_system_formbuilder/elements/time.rb
|
|
340
341
|
- lib/govuk_design_system_formbuilder/presenters/error_summary.rb
|
|
341
342
|
- lib/govuk_design_system_formbuilder/proxy.rb
|
|
342
343
|
- lib/govuk_design_system_formbuilder/refinements/prefixable_array.rb
|
|
343
344
|
- lib/govuk_design_system_formbuilder/traits/caption.rb
|
|
344
345
|
- lib/govuk_design_system_formbuilder/traits/collection_item.rb
|
|
346
|
+
- lib/govuk_design_system_formbuilder/traits/content_before_and_after.rb
|
|
347
|
+
- lib/govuk_design_system_formbuilder/traits/date_input.rb
|
|
345
348
|
- lib/govuk_design_system_formbuilder/traits/error.rb
|
|
346
349
|
- lib/govuk_design_system_formbuilder/traits/fieldset_item.rb
|
|
347
350
|
- lib/govuk_design_system_formbuilder/traits/hint.rb
|
|
@@ -378,7 +381,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
378
381
|
- !ruby/object:Gem::Version
|
|
379
382
|
version: '0'
|
|
380
383
|
requirements: []
|
|
381
|
-
rubygems_version: 3.
|
|
384
|
+
rubygems_version: 3.5.22
|
|
382
385
|
signing_key:
|
|
383
386
|
specification_version: 4
|
|
384
387
|
summary: GOV.UK Form Builder for Ryby on Rails
|