govuk_design_system_formbuilder 1.2.5 → 2.0.0b3

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/lib/govuk_design_system_formbuilder.rb +1 -1
  4. data/lib/govuk_design_system_formbuilder/base.rb +1 -1
  5. data/lib/govuk_design_system_formbuilder/builder.rb +196 -90
  6. data/lib/govuk_design_system_formbuilder/containers/character_count.rb +1 -1
  7. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +1 -1
  8. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +9 -9
  9. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +1 -1
  10. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +4 -3
  11. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +10 -10
  12. data/lib/govuk_design_system_formbuilder/containers/radios.rb +1 -1
  13. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +1 -1
  14. data/lib/govuk_design_system_formbuilder/elements/caption.rb +10 -5
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +12 -12
  16. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +7 -5
  17. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +8 -4
  18. data/lib/govuk_design_system_formbuilder/elements/date.rb +11 -11
  19. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +3 -3
  20. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +3 -3
  21. data/lib/govuk_design_system_formbuilder/elements/file.rb +8 -8
  22. data/lib/govuk_design_system_formbuilder/elements/hint.rb +35 -8
  23. data/lib/govuk_design_system_formbuilder/elements/inputs/email.rb +0 -2
  24. data/lib/govuk_design_system_formbuilder/elements/inputs/number.rb +0 -2
  25. data/lib/govuk_design_system_formbuilder/elements/inputs/password.rb +0 -2
  26. data/lib/govuk_design_system_formbuilder/elements/inputs/phone.rb +0 -2
  27. data/lib/govuk_design_system_formbuilder/elements/inputs/text.rb +0 -2
  28. data/lib/govuk_design_system_formbuilder/elements/inputs/url.rb +0 -2
  29. data/lib/govuk_design_system_formbuilder/elements/label.rb +20 -14
  30. data/lib/govuk_design_system_formbuilder/elements/legend.rb +18 -10
  31. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +14 -14
  32. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +11 -5
  33. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +13 -5
  34. data/lib/govuk_design_system_formbuilder/elements/select.rb +11 -11
  35. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +6 -6
  36. data/lib/govuk_design_system_formbuilder/traits/caption.rb +1 -9
  37. data/lib/govuk_design_system_formbuilder/traits/collection_item.rb +1 -1
  38. data/lib/govuk_design_system_formbuilder/traits/conditional.rb +1 -1
  39. data/lib/govuk_design_system_formbuilder/traits/hint.rb +15 -2
  40. data/lib/govuk_design_system_formbuilder/traits/input.rb +8 -8
  41. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  42. metadata +11 -11
@@ -14,7 +14,7 @@ module GOVUKDesignSystemFormBuilder
14
14
  def html
15
15
  return yield unless limit?
16
16
 
17
- content_tag('div', **options) { yield }
17
+ tag.div(**options) { yield }
18
18
  end
19
19
 
20
20
  private
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  end
9
9
 
10
10
  def html
11
- content_tag('div', **options) { yield }
11
+ tag.div(**options) { yield }
12
12
  end
13
13
 
14
14
  private
@@ -4,20 +4,20 @@ module GOVUKDesignSystemFormBuilder
4
4
  include Traits::Error
5
5
  include Traits::Hint
6
6
 
7
- def initialize(builder, object_name, attribute_name, hint_text:, legend:, caption:, small:, classes:, form_group_classes:, &block)
7
+ def initialize(builder, object_name, attribute_name, hint:, legend:, caption:, small:, classes:, form_group:, &block)
8
8
  super(builder, object_name, attribute_name, &block)
9
9
 
10
- @legend = legend
11
- @caption = caption
12
- @hint_text = hint_text
13
- @small = small
14
- @classes = classes
15
- @form_group_classes = form_group_classes
16
- @block_content = capture { block.call }
10
+ @legend = legend
11
+ @caption = caption
12
+ @hint = hint
13
+ @small = small
14
+ @classes = classes
15
+ @form_group = form_group
16
+ @block_content = capture { block.call }
17
17
  end
18
18
 
19
19
  def html
20
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
20
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
21
21
  Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
22
22
  safe_join([hint_element, error_element, checkboxes])
23
23
  end
@@ -11,7 +11,7 @@ module GOVUKDesignSystemFormBuilder
11
11
  end
12
12
 
13
13
  def html
14
- content_tag('fieldset', **options) do
14
+ tag.fieldset(**options) do
15
15
  safe_join([legend_element, (@block_content || yield)])
16
16
  end
17
17
  end
@@ -1,14 +1,15 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
3
  class FormGroup < Base
4
- def initialize(builder, object_name, attribute_name, classes: nil)
4
+ def initialize(builder, object_name, attribute_name, classes: nil, **kwargs)
5
5
  super(builder, object_name, attribute_name)
6
6
 
7
- @classes = classes
7
+ @classes = classes
8
+ @html_attributes = kwargs
8
9
  end
9
10
 
10
11
  def html
11
- content_tag('div', class: classes) { yield }
12
+ tag.div(class: classes, **@html_attributes) { yield }
12
13
  end
13
14
 
14
15
  private
@@ -4,21 +4,21 @@ module GOVUKDesignSystemFormBuilder
4
4
  include Traits::Hint
5
5
  include Traits::Error
6
6
 
7
- def initialize(builder, object_name, attribute_name, hint_text:, legend:, caption:, inline:, small:, classes:, form_group_classes:, &block)
7
+ def initialize(builder, object_name, attribute_name, hint:, legend:, caption:, inline:, small:, classes:, form_group:, &block)
8
8
  super(builder, object_name, attribute_name)
9
9
 
10
- @inline = inline
11
- @small = small
12
- @legend = legend
13
- @caption = caption
14
- @hint_text = hint_text
15
- @classes = classes
16
- @form_group_classes = form_group_classes
17
- @block_content = capture { block.call }
10
+ @inline = inline
11
+ @small = small
12
+ @legend = legend
13
+ @caption = caption
14
+ @hint = hint
15
+ @classes = classes
16
+ @form_group = form_group
17
+ @block_content = capture { block.call }
18
18
  end
19
19
 
20
20
  def html
21
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
21
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
22
22
  Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
23
23
  safe_join([hint_element, error_element, radios])
24
24
  end
@@ -11,7 +11,7 @@ module GOVUKDesignSystemFormBuilder
11
11
  end
12
12
 
13
13
  def html
14
- content_tag('div', **options) { yield }
14
+ tag.div(**options) { yield }
15
15
  end
16
16
 
17
17
  private
@@ -10,7 +10,7 @@ module GOVUKDesignSystemFormBuilder
10
10
  def html
11
11
  return nil if @content.blank?
12
12
 
13
- content_tag('div', id: supplemental_id) { @content }
13
+ tag.div(id: supplemental_id) { @content }
14
14
  end
15
15
 
16
16
  private
@@ -3,21 +3,26 @@ module GOVUKDesignSystemFormBuilder
3
3
  class Caption < Base
4
4
  include Traits::Localisation
5
5
 
6
- def initialize(builder, object_name, attribute_name, text:, size: nil)
6
+ def initialize(builder, object_name, attribute_name, text: nil, size: nil, **kwargs)
7
7
  super(builder, object_name, attribute_name)
8
8
 
9
- @text = text(text)
10
- @size_class = size_class(size)
9
+ @text = text(text)
10
+ @size_class = size_class(size)
11
+ @html_attributes = kwargs
11
12
  end
12
13
 
13
14
  def html
14
- return nil if @text.blank?
15
+ return nil unless active?
15
16
 
16
- tag.span(@text, class: @size_class)
17
+ tag.span(@text, class: @size_class, **@html_attributes)
17
18
  end
18
19
 
19
20
  private
20
21
 
22
+ def active?
23
+ @text.present?
24
+ end
25
+
21
26
  def text(override)
22
27
  override || localised_text(:caption)
23
28
  end
@@ -6,23 +6,23 @@ module GOVUKDesignSystemFormBuilder
6
6
  include Traits::Hint
7
7
  include Traits::Supplemental
8
8
 
9
- def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint_method: nil, hint_text:, legend:, caption:, small:, classes:, form_group_classes:, &block)
9
+ def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint_method: nil, hint:, legend:, caption:, small:, classes:, form_group:, &block)
10
10
  super(builder, object_name, attribute_name, &block)
11
11
 
12
- @collection = collection
13
- @value_method = value_method
14
- @text_method = text_method
15
- @hint_method = hint_method
16
- @small = small
17
- @legend = legend
18
- @caption = caption
19
- @hint_text = hint_text
20
- @classes = classes
21
- @form_group_classes = form_group_classes
12
+ @collection = collection
13
+ @value_method = value_method
14
+ @text_method = text_method
15
+ @hint_method = hint_method
16
+ @small = small
17
+ @legend = legend
18
+ @caption = caption
19
+ @hint = hint
20
+ @classes = classes
21
+ @form_group = form_group
22
22
  end
23
23
 
24
24
  def html
25
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
25
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
26
26
  Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
27
27
  safe_join([supplemental_content, hint_element, error_element, check_boxes])
28
28
  end
@@ -2,8 +2,6 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module CheckBoxes
4
4
  class CollectionCheckBox < Base
5
- using PrefixableArray
6
-
7
5
  include Traits::CollectionItem
8
6
  include Traits::Hint
9
7
 
@@ -13,12 +11,12 @@ module GOVUKDesignSystemFormBuilder
13
11
  @checkbox = checkbox
14
12
  @item = checkbox.object
15
13
  @value = checkbox.value
16
- @hint_text = retrieve(@item, hint_method)
14
+ @hint = { text: retrieve(@item, hint_method) }
17
15
  @link_errors = link_errors
18
16
  end
19
17
 
20
18
  def html
21
- content_tag('div', class: %(#{brand}-checkboxes__item)) do
19
+ tag.div(class: %(#{brand}-checkboxes__item)) do
22
20
  safe_join([check_box, label_element, hint_element])
23
21
  end
24
22
  end
@@ -42,7 +40,11 @@ module GOVUKDesignSystemFormBuilder
42
40
  end
43
41
 
44
42
  def hint_element
45
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, checkbox: true)
43
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
44
+ end
45
+
46
+ def hint_options
47
+ { value: @value, checkbox: true }
46
48
  end
47
49
  end
48
50
  end
@@ -8,12 +8,12 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::Hint
9
9
  include Traits::Conditional
10
10
 
11
- def initialize(builder, object_name, attribute_name, value, label:, hint_text:, link_errors:, multiple:, &block)
11
+ def initialize(builder, object_name, attribute_name, value, label:, hint:, link_errors:, multiple:, &block)
12
12
  super(builder, object_name, attribute_name)
13
13
 
14
14
  @value = value
15
15
  @label = label
16
- @hint_text = hint_text
16
+ @hint = hint
17
17
  @multiple = multiple
18
18
  @link_errors = link_errors
19
19
 
@@ -30,7 +30,7 @@ module GOVUKDesignSystemFormBuilder
30
30
  private
31
31
 
32
32
  def item
33
- content_tag('div', class: %(#{brand}-checkboxes__item)) do
33
+ tag.div(class: %(#{brand}-checkboxes__item)) do
34
34
  safe_join([check_box, label_element, hint_element])
35
35
  end
36
36
  end
@@ -62,7 +62,11 @@ module GOVUKDesignSystemFormBuilder
62
62
  end
63
63
 
64
64
  def hint_element
65
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, checkbox: true)
65
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
66
+ end
67
+
68
+ def hint_options
69
+ { checkbox: true }
66
70
  end
67
71
 
68
72
  def conditional_classes
@@ -9,19 +9,19 @@ 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_text:, date_of_birth: false, omit_day:, form_group_classes:, &block)
12
+ def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, date_of_birth: false, omit_day:, form_group:, &block)
13
13
  super(builder, object_name, attribute_name, &block)
14
14
 
15
- @legend = legend
16
- @caption = caption
17
- @hint_text = hint_text
18
- @date_of_birth = date_of_birth
19
- @omit_day = omit_day
20
- @form_group_classes = form_group_classes
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
21
  end
22
22
 
23
23
  def html
24
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
24
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
25
25
  Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
26
26
  safe_join([supplemental_content, hint_element, error_element, date])
27
27
  end
@@ -35,7 +35,7 @@ module GOVUKDesignSystemFormBuilder
35
35
  end
36
36
 
37
37
  def date
38
- content_tag('div', class: %(#{brand}-date-input)) do
38
+ tag.div(class: %(#{brand}-date-input)) do
39
39
  safe_join([day, month, year])
40
40
  end
41
41
  end
@@ -61,8 +61,8 @@ module GOVUKDesignSystemFormBuilder
61
61
  def date_part(segment, width:, link_errors: false)
62
62
  value = @builder.object.try(@attribute_name).try(segment)
63
63
 
64
- content_tag('div', class: %w(date-input__item).prefix(brand)) do
65
- content_tag('div', class: %w(form-group).prefix(brand)) do
64
+ tag.div(class: %(#{brand}-date-input__item)) do
65
+ tag.div(class: %(#{brand}-form-group)) do
66
66
  safe_join([label(segment, link_errors), input(segment, link_errors, width, value)])
67
67
  end
68
68
  end
@@ -12,14 +12,14 @@ module GOVUKDesignSystemFormBuilder
12
12
  def html
13
13
  return nil unless has_errors?
14
14
 
15
- content_tag('span', class: %(#{brand}-error-message), id: error_id) do
16
- safe_join([prefix, message])
15
+ tag.span(class: %(#{brand}-error-message), id: error_id) do
16
+ safe_join([hidden_prefix, message])
17
17
  end
18
18
  end
19
19
 
20
20
  private
21
21
 
22
- def prefix
22
+ def hidden_prefix
23
23
  tag.span('Error: ', class: %(#{brand}-visually-hidden))
24
24
  end
25
25
 
@@ -12,7 +12,7 @@ module GOVUKDesignSystemFormBuilder
12
12
  def html
13
13
  return nil unless object_has_errors?
14
14
 
15
- content_tag('div', class: summary_class, **summary_options) do
15
+ tag.div(class: summary_class, **summary_options) do
16
16
  safe_join([title, summary])
17
17
  end
18
18
  end
@@ -24,8 +24,8 @@ module GOVUKDesignSystemFormBuilder
24
24
  end
25
25
 
26
26
  def summary
27
- content_tag('div', class: summary_class('body')) do
28
- content_tag('ul', class: [%(#{brand}-list), summary_class('list')]) do
27
+ tag.div(class: summary_class('body')) do
28
+ tag.ul(class: [%(#{brand}-list), summary_class('list')]) do
29
29
  safe_join(list)
30
30
  end
31
31
  end
@@ -8,18 +8,18 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
10
 
11
- def initialize(builder, object_name, attribute_name, hint_text:, label:, caption:, form_group_classes:, **kwargs, &block)
11
+ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, **kwargs, &block)
12
12
  super(builder, object_name, attribute_name, &block)
13
13
 
14
- @label = label
15
- @caption = caption
16
- @hint_text = hint_text
17
- @extra_options = kwargs
18
- @form_group_classes = form_group_classes
14
+ @label = label
15
+ @caption = caption
16
+ @hint = hint
17
+ @html_attributes = kwargs
18
+ @form_group = form_group
19
19
  end
20
20
 
21
21
  def html
22
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
22
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
23
23
  safe_join([label_element, supplemental_content, hint_element, error_element, file])
24
24
  end
25
25
  end
@@ -27,7 +27,7 @@ module GOVUKDesignSystemFormBuilder
27
27
  private
28
28
 
29
29
  def file
30
- @builder.file_field(@attribute_name, **options, **@extra_options)
30
+ @builder.file_field(@attribute_name, **options, **@html_attributes)
31
31
  end
32
32
 
33
33
  def options
@@ -3,26 +3,53 @@ module GOVUKDesignSystemFormBuilder
3
3
  class Hint < Base
4
4
  using PrefixableArray
5
5
 
6
- include Traits::Hint
7
6
  include Traits::Localisation
8
7
 
9
- def initialize(builder, object_name, attribute_name, text, value = nil, radio: false, checkbox: false)
8
+ def initialize(builder, object_name, attribute_name, value: nil, text: nil, content: nil, radio: false, checkbox: false, **kwargs)
10
9
  super(builder, object_name, attribute_name)
11
10
 
12
- @value = value
13
- @hint_text = retrieve_text(text)
14
- @radio = radio
15
- @checkbox = checkbox
11
+ @value = value
12
+ @radio = radio
13
+ @checkbox = checkbox
14
+ @html_attributes = kwargs
15
+
16
+ if content
17
+ @raw = capture { content.call }
18
+ else
19
+ @text = retrieve_text(text)
20
+ end
21
+ end
22
+
23
+ def active?
24
+ [@text, @raw].any?(&:present?)
16
25
  end
17
26
 
18
27
  def html
19
- return nil if @hint_text.blank?
28
+ return nil unless active?
29
+
30
+ content_tag(hint_tag, **hint_options, **@html_attributes) { hint_body }
31
+ end
32
+
33
+ def hint_id
34
+ return nil unless active?
20
35
 
21
- tag.span(@hint_text, class: classes, id: hint_id)
36
+ build_id('hint')
22
37
  end
23
38
 
24
39
  private
25
40
 
41
+ def hint_options
42
+ { class: classes, id: hint_id }
43
+ end
44
+
45
+ def hint_tag
46
+ @raw.presence ? 'div' : 'span'
47
+ end
48
+
49
+ def hint_body
50
+ @raw || @text
51
+ end
52
+
26
53
  def retrieve_text(supplied)
27
54
  supplied.presence || localised_text(:hint)
28
55
  end