govuk_design_system_formbuilder 2.1.7 → 2.3.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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +3 -3
  4. data/lib/govuk_design_system_formbuilder.rb +10 -0
  5. data/lib/govuk_design_system_formbuilder/base.rb +9 -0
  6. data/lib/govuk_design_system_formbuilder/builder.rb +28 -19
  7. data/lib/govuk_design_system_formbuilder/containers/button_group.rb +15 -0
  8. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +2 -2
  9. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +11 -8
  10. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +2 -2
  11. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +1 -1
  12. data/lib/govuk_design_system_formbuilder/elements/caption.rb +3 -3
  13. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +15 -14
  14. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +2 -2
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +4 -2
  16. data/lib/govuk_design_system_formbuilder/elements/date.rb +13 -12
  17. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +1 -1
  18. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +11 -4
  19. data/lib/govuk_design_system_formbuilder/elements/file.rb +3 -2
  20. data/lib/govuk_design_system_formbuilder/elements/hint.rb +1 -1
  21. data/lib/govuk_design_system_formbuilder/elements/inputs/email.rb +1 -0
  22. data/lib/govuk_design_system_formbuilder/elements/inputs/number.rb +1 -0
  23. data/lib/govuk_design_system_formbuilder/elements/inputs/password.rb +1 -0
  24. data/lib/govuk_design_system_formbuilder/elements/inputs/phone.rb +1 -0
  25. data/lib/govuk_design_system_formbuilder/elements/inputs/text.rb +1 -0
  26. data/lib/govuk_design_system_formbuilder/elements/inputs/url.rb +1 -0
  27. data/lib/govuk_design_system_formbuilder/elements/label.rb +3 -3
  28. data/lib/govuk_design_system_formbuilder/elements/legend.rb +3 -5
  29. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +3 -3
  30. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +3 -3
  31. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +8 -6
  32. data/lib/govuk_design_system_formbuilder/elements/select.rb +22 -19
  33. data/lib/govuk_design_system_formbuilder/elements/submit.rb +21 -14
  34. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +5 -4
  35. data/lib/govuk_design_system_formbuilder/traits/caption.rb +1 -1
  36. data/lib/govuk_design_system_formbuilder/traits/error.rb +2 -2
  37. data/lib/govuk_design_system_formbuilder/traits/fieldset_item.rb +2 -2
  38. data/lib/govuk_design_system_formbuilder/traits/hint.rb +2 -2
  39. data/lib/govuk_design_system_formbuilder/traits/html_attributes.rb +9 -0
  40. data/lib/govuk_design_system_formbuilder/traits/input.rb +5 -5
  41. data/lib/govuk_design_system_formbuilder/traits/label.rb +1 -1
  42. data/lib/govuk_design_system_formbuilder/traits/supplemental.rb +2 -2
  43. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  44. metadata +35 -4
@@ -36,11 +36,11 @@ module GOVUKDesignSystemFormBuilder
36
36
  end
37
37
 
38
38
  def label_element
39
- @label_element ||= Label.new(@builder, @object_name, @attribute_name, @checkbox, value: @value, link_errors: @link_errors)
39
+ @label_element ||= Label.new(*bound, @checkbox, value: @value, link_errors: @link_errors)
40
40
  end
41
41
 
42
42
  def hint_element
43
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
43
+ @hint_element ||= Elements::Hint.new(*bound, **hint_options, **hint_content)
44
44
  end
45
45
 
46
46
  def hint_options
@@ -8,8 +8,9 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::Hint
9
9
  include Traits::FieldsetItem
10
10
  include Traits::Conditional
11
+ include Traits::HTMLAttributes
11
12
 
12
- def initialize(builder, object_name, attribute_name, value, unchecked_value, label:, hint:, link_errors:, multiple:, &block)
13
+ def initialize(builder, object_name, attribute_name, value, unchecked_value, label:, hint:, link_errors:, multiple:, **kwargs, &block)
13
14
  super(builder, object_name, attribute_name)
14
15
 
15
16
  @value = value
@@ -18,6 +19,7 @@ module GOVUKDesignSystemFormBuilder
18
19
  @hint = hint
19
20
  @multiple = multiple
20
21
  @link_errors = link_errors
22
+ @html_attributes = kwargs
21
23
 
22
24
  if block_given?
23
25
  @conditional_content = wrap_conditional(block)
@@ -38,7 +40,7 @@ module GOVUKDesignSystemFormBuilder
38
40
  end
39
41
 
40
42
  def check_box
41
- @builder.check_box(@attribute_name, options, @value, @unchecked_value)
43
+ @builder.check_box(@attribute_name, attributes(@html_attributes), @value, @unchecked_value)
42
44
  end
43
45
 
44
46
  def options
@@ -9,21 +9,22 @@ module GOVUKDesignSystemFormBuilder
9
9
 
10
10
  SEGMENTS = { day: '3i', month: '2i', year: '1i' }.freeze
11
11
 
12
- def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_day:, form_group:, wildcards:, date_of_birth: false, &block)
12
+ def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_day:, form_group:, wildcards:, date_of_birth: false, **kwargs, &block)
13
13
  super(builder, object_name, attribute_name, &block)
14
14
 
15
- @legend = legend
16
- @caption = caption
17
- @hint = hint
18
- @date_of_birth = date_of_birth
19
- @omit_day = omit_day
20
- @form_group = form_group
21
- @wildcards = wildcards
15
+ @legend = legend
16
+ @caption = caption
17
+ @hint = hint
18
+ @date_of_birth = date_of_birth
19
+ @omit_day = omit_day
20
+ @form_group = form_group
21
+ @wildcards = wildcards
22
+ @html_attributes = kwargs
22
23
  end
23
24
 
24
25
  def html
25
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
26
- Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
26
+ Containers::FormGroup.new(*bound, **@form_group, **@html_attributes).html do
27
+ Containers::Fieldset.new(*bound, **fieldset_options).html do
27
28
  safe_join([supplemental_content, hint_element, error_element, date])
28
29
  end
29
30
  end
@@ -46,7 +47,7 @@ module GOVUKDesignSystemFormBuilder
46
47
  end
47
48
 
48
49
  def day
49
- return nil if omit_day?
50
+ return if omit_day?
50
51
 
51
52
  date_part(:day, width: 2, link_errors: true)
52
53
  end
@@ -124,7 +125,7 @@ module GOVUKDesignSystemFormBuilder
124
125
  end
125
126
 
126
127
  def date_of_birth_autocomplete_value(segment)
127
- return nil unless @date_of_birth
128
+ return unless @date_of_birth
128
129
 
129
130
  { day: 'bday-day', month: 'bday-month', year: 'bday-year' }.fetch(segment)
130
131
  end
@@ -6,7 +6,7 @@ module GOVUKDesignSystemFormBuilder
6
6
  include Traits::Error
7
7
 
8
8
  def html
9
- return nil unless has_errors?
9
+ return unless has_errors?
10
10
 
11
11
  tag.span(class: %(#{brand}-error-message), id: error_id) do
12
12
  safe_join([hidden_prefix, message])
@@ -2,18 +2,20 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class ErrorSummary < Base
4
4
  include Traits::Error
5
+ include Traits::HTMLAttributes
5
6
 
6
- def initialize(builder, object_name, title, link_base_errors_to:)
7
+ def initialize(builder, object_name, title, link_base_errors_to:, **kwargs)
7
8
  super(builder, object_name, nil)
8
9
 
9
10
  @title = title
10
11
  @link_base_errors_to = link_base_errors_to
12
+ @html_attributes = kwargs
11
13
  end
12
14
 
13
15
  def html
14
- return nil unless object_has_errors?
16
+ return unless object_has_errors?
15
17
 
16
- tag.div(class: summary_class, **summary_options) do
18
+ tag.div(**attributes(@html_attributes)) do
17
19
  safe_join([title, summary])
18
20
  end
19
21
  end
@@ -46,6 +48,10 @@ module GOVUKDesignSystemFormBuilder
46
48
  '#'.concat(target)
47
49
  end
48
50
 
51
+ def classes
52
+ Array.wrap(summary_class)
53
+ end
54
+
49
55
  def summary_class(part = nil)
50
56
  if part
51
57
  %(#{brand}-error-summary).concat('__', part)
@@ -70,8 +76,9 @@ module GOVUKDesignSystemFormBuilder
70
76
  @builder.object.errors.any?
71
77
  end
72
78
 
73
- def summary_options
79
+ def options
74
80
  {
81
+ class: classes,
75
82
  tabindex: -1,
76
83
  role: 'alert',
77
84
  data: {
@@ -7,6 +7,7 @@ module GOVUKDesignSystemFormBuilder
7
7
  include Traits::Hint
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
+ include Traits::HTMLAttributes
10
11
 
11
12
  def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, **kwargs, &block)
12
13
  super(builder, object_name, attribute_name, &block)
@@ -19,7 +20,7 @@ module GOVUKDesignSystemFormBuilder
19
20
  end
20
21
 
21
22
  def html
22
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
23
+ Containers::FormGroup.new(*bound, **@form_group).html do
23
24
  safe_join([label_element, supplemental_content, hint_element, error_element, file])
24
25
  end
25
26
  end
@@ -27,7 +28,7 @@ module GOVUKDesignSystemFormBuilder
27
28
  private
28
29
 
29
30
  def file
30
- @builder.file_field(@attribute_name, **options, **@html_attributes)
31
+ @builder.file_field(@attribute_name, attributes(@html_attributes))
31
32
  end
32
33
 
33
34
  def options
@@ -31,7 +31,7 @@ module GOVUKDesignSystemFormBuilder
31
31
  end
32
32
 
33
33
  def hint_id
34
- return nil unless active?
34
+ return unless active?
35
35
 
36
36
  build_id('hint')
37
37
  end
@@ -7,6 +7,7 @@ module GOVUKDesignSystemFormBuilder
7
7
  include Traits::Hint
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
+ include Traits::HTMLAttributes
10
11
 
11
12
  private
12
13
 
@@ -7,6 +7,7 @@ module GOVUKDesignSystemFormBuilder
7
7
  include Traits::Hint
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
+ include Traits::HTMLAttributes
10
11
 
11
12
  def builder_method
12
13
  :number_field
@@ -7,6 +7,7 @@ module GOVUKDesignSystemFormBuilder
7
7
  include Traits::Hint
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
+ include Traits::HTMLAttributes
10
11
 
11
12
  def builder_method
12
13
  :password_field
@@ -7,6 +7,7 @@ module GOVUKDesignSystemFormBuilder
7
7
  include Traits::Hint
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
+ include Traits::HTMLAttributes
10
11
 
11
12
  def builder_method
12
13
  :phone_field
@@ -7,6 +7,7 @@ module GOVUKDesignSystemFormBuilder
7
7
  include Traits::Hint
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
+ include Traits::HTMLAttributes
10
11
 
11
12
  def builder_method
12
13
  :text_field
@@ -7,6 +7,7 @@ module GOVUKDesignSystemFormBuilder
7
7
  include Traits::Hint
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
+ include Traits::HTMLAttributes
10
11
 
11
12
  def builder_method
12
13
  :url_field
@@ -28,7 +28,7 @@ module GOVUKDesignSystemFormBuilder
28
28
  end
29
29
 
30
30
  def html
31
- return nil unless active?
31
+ return unless active?
32
32
 
33
33
  if @tag.present?
34
34
  content_tag(@tag, class: %(#{brand}-label-wrapper)) { label }
@@ -72,13 +72,13 @@ module GOVUKDesignSystemFormBuilder
72
72
  end
73
73
 
74
74
  def radio_class
75
- return nil unless @radio
75
+ return unless @radio
76
76
 
77
77
  %(#{brand}-radios__label)
78
78
  end
79
79
 
80
80
  def checkbox_class
81
- return nil unless @checkbox
81
+ return unless @checkbox
82
82
 
83
83
  %(#{brand}-checkboxes__label)
84
84
  end
@@ -39,11 +39,9 @@ module GOVUKDesignSystemFormBuilder
39
39
  def legend_content
40
40
  caption_and_text = safe_join([caption_element, @text])
41
41
 
42
- if @tag.present?
43
- content_tag(@tag, class: heading_classes) { caption_and_text }
44
- else
45
- caption_and_text
46
- end
42
+ return caption_and_text if @tag.blank?
43
+
44
+ content_tag(@tag, class: heading_classes) { caption_and_text }
47
45
  end
48
46
 
49
47
  def retrieve_text(supplied_text)
@@ -25,8 +25,8 @@ module GOVUKDesignSystemFormBuilder
25
25
  end
26
26
 
27
27
  def html
28
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
29
- Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
28
+ Containers::FormGroup.new(*bound, **@form_group).html do
29
+ Containers::Fieldset.new(*bound, **fieldset_options).html do
30
30
  safe_join([hidden_field, supplemental_content, hint_element, error_element, radios])
31
31
  end
32
32
  end
@@ -56,7 +56,7 @@ module GOVUKDesignSystemFormBuilder
56
56
 
57
57
  def collection
58
58
  @collection.map.with_index do |item, i|
59
- Elements::Radios::CollectionRadioButton.new(@builder, @object_name, @attribute_name, item, **collection_options(i)).html
59
+ Elements::Radios::CollectionRadioButton.new(*bound, item, **collection_options(i)).html
60
60
  end
61
61
  end
62
62
 
@@ -9,7 +9,7 @@ module GOVUKDesignSystemFormBuilder
9
9
  # error summary requires that the id of the first radio is linked-to from the corresponding
10
10
  # error message. As when the summary is built what happens later in the form is unknown, we
11
11
  # need to control this to ensure the link is generated correctly
12
- def initialize(builder, object_name, attribute_name, item, value_method:, text_method:, hint_method:, link_errors: false, bold_labels:)
12
+ def initialize(builder, object_name, attribute_name, item, value_method:, text_method:, hint_method:, bold_labels:, link_errors: false)
13
13
  super(builder, object_name, attribute_name)
14
14
 
15
15
  @item = item
@@ -41,7 +41,7 @@ module GOVUKDesignSystemFormBuilder
41
41
  end
42
42
 
43
43
  def hint_element
44
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
44
+ @hint_element ||= Elements::Hint.new(*bound, **hint_options, **hint_content)
45
45
  end
46
46
 
47
47
  def hint_content
@@ -53,7 +53,7 @@ module GOVUKDesignSystemFormBuilder
53
53
  end
54
54
 
55
55
  def label_element
56
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_options)
56
+ @label_element ||= Elements::Label.new(*bound, **label_options)
57
57
  end
58
58
 
59
59
  def label_options
@@ -8,14 +8,16 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::Hint
9
9
  include Traits::FieldsetItem
10
10
  include Traits::Conditional
11
+ include Traits::HTMLAttributes
11
12
 
12
- def initialize(builder, object_name, attribute_name, value, label:, hint:, link_errors:, &block)
13
+ def initialize(builder, object_name, attribute_name, value, label:, hint:, link_errors:, **kwargs, &block)
13
14
  super(builder, object_name, attribute_name)
14
15
 
15
- @value = value
16
- @label = label
17
- @hint = hint
18
- @link_errors = has_errors? && link_errors
16
+ @value = value
17
+ @label = label
18
+ @hint = hint
19
+ @link_errors = has_errors? && link_errors
20
+ @html_attributes = kwargs
19
21
 
20
22
  if block_given?
21
23
  @conditional_content = wrap_conditional(block)
@@ -40,7 +42,7 @@ module GOVUKDesignSystemFormBuilder
40
42
  end
41
43
 
42
44
  def input
43
- @builder.radio_button(@attribute_name, @value, **options)
45
+ @builder.radio_button(@attribute_name, @value, **attributes(@html_attributes))
44
46
  end
45
47
 
46
48
  def options
@@ -5,23 +5,30 @@ module GOVUKDesignSystemFormBuilder
5
5
  include Traits::Label
6
6
  include Traits::Hint
7
7
  include Traits::Supplemental
8
+ include Traits::HTMLAttributes
8
9
 
9
- def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint:, label:, caption:, form_group:, options: {}, html_options: {}, &block)
10
+ def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint:, label:, caption:, form_group:, options: {}, **kwargs, &block)
10
11
  super(builder, object_name, attribute_name, &block)
11
12
 
12
- @collection = collection
13
- @value_method = value_method
14
- @text_method = text_method
15
- @options = options
16
- @html_options = html_options
17
- @label = label
18
- @caption = caption
19
- @hint = hint
20
- @form_group = form_group
13
+ @collection = collection
14
+ @value_method = value_method
15
+ @text_method = text_method
16
+ @options = options
17
+ @label = label
18
+ @caption = caption
19
+ @hint = hint
20
+ @form_group = form_group
21
+ @html_attributes = kwargs
22
+
23
+ # FIXME remove this soon, worth informing people who miss the release notes that the
24
+ # args have changed though.
25
+ if :html_options.in?(kwargs.keys)
26
+ Rails.logger.warn("GOVUKDesignSystemFormBuilder: html_options has been deprecated, use keyword arguments instead")
27
+ end
21
28
  end
22
29
 
23
30
  def html
24
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
31
+ Containers::FormGroup.new(*bound, **@form_group).html do
25
32
  safe_join([label_element, supplemental_content, hint_element, error_element, select])
26
33
  end
27
34
  end
@@ -29,28 +36,24 @@ module GOVUKDesignSystemFormBuilder
29
36
  private
30
37
 
31
38
  def select
32
- @builder.collection_select(@attribute_name, @collection, @value_method, @text_method, @options, **options)
39
+ @builder.collection_select(@attribute_name, @collection, @value_method, @text_method, @options, **attributes(@html_attributes))
33
40
  end
34
41
 
35
42
  def options
36
- @html_options.deep_merge(
43
+ {
37
44
  id: field_id(link_errors: true),
38
45
  class: classes,
39
46
  aria: { describedby: described_by(hint_id, error_id, supplemental_id) }
40
- )
47
+ }
41
48
  end
42
49
 
43
50
  def classes
44
- [%(#{brand}-select), error_class, custom_classes].flatten.compact
51
+ [%(#{brand}-select), error_class].flatten.compact
45
52
  end
46
53
 
47
54
  def error_class
48
55
  %(#{brand}-select--error) if has_errors?
49
56
  end
50
-
51
- def custom_classes
52
- @html_options[:class]
53
- end
54
57
  end
55
58
  end
56
59
  end
@@ -2,8 +2,9 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class Submit < Base
4
4
  using PrefixableArray
5
+ include Traits::HTMLAttributes
5
6
 
6
- def initialize(builder, text, warning:, secondary:, classes:, prevent_double_click:, validate:, disabled:, &block)
7
+ def initialize(builder, text, warning:, secondary:, classes:, prevent_double_click:, validate:, disabled:, **kwargs, &block)
7
8
  super(builder, nil, nil)
8
9
 
9
10
  fail ArgumentError, 'buttons can be warning or secondary' if warning && secondary
@@ -15,31 +16,33 @@ module GOVUKDesignSystemFormBuilder
15
16
  @classes = classes
16
17
  @validate = validate
17
18
  @disabled = disabled
19
+ @html_attributes = kwargs
18
20
  @block_content = capture { block.call } if block_given?
19
21
  end
20
22
 
21
23
  def html
22
- safe_join([submit, @block_content])
24
+ @block_content.present? ? button_group : submit
23
25
  end
24
26
 
25
27
  private
26
28
 
27
- def submit
28
- @builder.submit(@text, class: classes, **options)
29
+ def button_group
30
+ Containers::ButtonGroup.new(@builder, buttons).html
29
31
  end
30
32
 
31
- def classes
32
- %w(button)
33
- .prefix(brand)
34
- .push(warning_class, secondary_class, disabled_class, padding_class, custom_classes)
35
- .flatten
36
- .compact
33
+ def buttons
34
+ safe_join([submit, @block_content])
35
+ end
36
+
37
+ def submit
38
+ @builder.submit(@text, **attributes(@html_attributes))
37
39
  end
38
40
 
39
41
  def options
40
42
  {
41
43
  formnovalidate: !@validate,
42
44
  disabled: @disabled,
45
+ class: classes,
43
46
  data: {
44
47
  module: %(#{brand}-button),
45
48
  'prevent-double-click': @prevent_double_click
@@ -47,6 +50,14 @@ module GOVUKDesignSystemFormBuilder
47
50
  }
48
51
  end
49
52
 
53
+ def classes
54
+ %w(button)
55
+ .prefix(brand)
56
+ .push(warning_class, secondary_class, disabled_class, custom_classes)
57
+ .flatten
58
+ .compact
59
+ end
60
+
50
61
  def warning_class
51
62
  %(#{brand}-button--warning) if @warning
52
63
  end
@@ -55,10 +66,6 @@ module GOVUKDesignSystemFormBuilder
55
66
  %(#{brand}-button--secondary) if @secondary
56
67
  end
57
68
 
58
- def padding_class
59
- %(#{brand}-!-margin-right-1) if @block_content
60
- end
61
-
62
69
  def disabled_class
63
70
  %(#{brand}-button--disabled) if @disabled
64
71
  end