govuk_design_system_formbuilder 1.2.3 → 2.0.0b1

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 (43) 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 +155 -60
  6. data/lib/govuk_design_system_formbuilder/containers/character_count.rb +8 -7
  7. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +18 -10
  8. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +13 -4
  9. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +15 -55
  10. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +19 -10
  11. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +13 -4
  12. data/lib/govuk_design_system_formbuilder/containers/radios.rb +22 -11
  13. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +3 -3
  14. data/lib/govuk_design_system_formbuilder/elements/caption.rb +6 -4
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +21 -12
  16. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +9 -7
  17. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +24 -16
  18. data/lib/govuk_design_system_formbuilder/elements/date.rb +47 -40
  19. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +4 -4
  20. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +15 -15
  21. data/lib/govuk_design_system_formbuilder/elements/file.rb +8 -7
  22. data/lib/govuk_design_system_formbuilder/elements/hint.rb +43 -16
  23. data/lib/govuk_design_system_formbuilder/elements/inputs/email.rb +2 -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 +35 -27
  30. data/lib/govuk_design_system_formbuilder/elements/legend.rb +79 -0
  31. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +23 -14
  32. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +24 -8
  33. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +19 -7
  34. data/lib/govuk_design_system_formbuilder/elements/select.rb +24 -19
  35. data/lib/govuk_design_system_formbuilder/elements/submit.rb +11 -6
  36. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +31 -25
  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 +9 -8
  41. data/lib/govuk_design_system_formbuilder/traits/label.rb +2 -2
  42. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  43. metadata +11 -10
@@ -14,17 +14,18 @@ module GOVUKDesignSystemFormBuilder
14
14
  def html
15
15
  return yield unless limit?
16
16
 
17
- content_tag(
18
- 'div',
19
- class: %(#{brand}-character-count),
20
- data: { module: %(#{brand}-character-count) }.merge(**limit, **threshold).compact
21
- ) do
22
- yield
23
- end
17
+ tag.div(**options) { yield }
24
18
  end
25
19
 
26
20
  private
27
21
 
22
+ def options
23
+ {
24
+ class: %(#{brand}-character-count),
25
+ data: { module: %(#{brand}-character-count) }.merge(**limit, **threshold).compact
26
+ }
27
+ end
28
+
28
29
  def limit
29
30
  if @max_words
30
31
  { maxwords: @max_words }
@@ -1,8 +1,6 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
3
  class CheckBoxes < Base
4
- using PrefixableArray
5
-
6
4
  def initialize(builder, small:, classes: nil)
7
5
  @builder = builder
8
6
  @small = small
@@ -10,18 +8,28 @@ module GOVUKDesignSystemFormBuilder
10
8
  end
11
9
 
12
10
  def html
13
- content_tag('div', class: check_boxes_classes, data: { module: %(#{brand}-checkboxes) }) do
14
- yield
15
- end
11
+ tag.div(**options) { yield }
16
12
  end
17
13
 
18
14
  private
19
15
 
20
- def check_boxes_classes
21
- %w(checkboxes).prefix(brand).tap do |c|
22
- c.push(%(#{brand}-checkboxes--small)) if @small
23
- c.push(@classes) if @classes
24
- end
16
+ def options
17
+ {
18
+ class: classes,
19
+ data: { module: %(#{brand}-checkboxes) }
20
+ }
21
+ end
22
+
23
+ def classes
24
+ [%(#{brand}-checkboxes), small_class, custom_classes].flatten.compact
25
+ end
26
+
27
+ def small_class
28
+ %(#{brand}-checkboxes--small) if @small
29
+ end
30
+
31
+ def custom_classes
32
+ Array.wrap(@classes)
25
33
  end
26
34
  end
27
35
  end
@@ -4,20 +4,21 @@ 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:, &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
10
  @legend = legend
11
11
  @caption = caption
12
- @hint_text = hint_text
12
+ @hint = hint
13
13
  @small = small
14
14
  @classes = classes
15
+ @form_group = form_group
15
16
  @block_content = capture { block.call }
16
17
  end
17
18
 
18
19
  def html
19
- Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
20
- Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, caption: @caption, described_by: [error_element.error_id, hint_element.hint_id]).html do
20
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
21
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
21
22
  safe_join([hint_element, error_element, checkboxes])
22
23
  end
23
24
  end
@@ -25,6 +26,14 @@ module GOVUKDesignSystemFormBuilder
25
26
 
26
27
  private
27
28
 
29
+ def fieldset_options
30
+ {
31
+ legend: @legend,
32
+ caption: @caption,
33
+ described_by: [error_element.error_id, hint_element.hint_id]
34
+ }
35
+ end
36
+
28
37
  def checkboxes
29
38
  Containers::CheckBoxes.new(@builder, small: @small, classes: @classes).html do
30
39
  @block_content
@@ -1,80 +1,40 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
3
  class Fieldset < Base
4
- using PrefixableArray
5
-
6
- include Traits::Caption
7
- include Traits::Localisation
8
-
9
- LEGEND_SIZES = %w(xl l m s).freeze
10
-
11
4
  def initialize(builder, object_name = nil, attribute_name = nil, legend: {}, caption: {}, described_by: nil, &block)
12
5
  super(builder, object_name, attribute_name, &block)
13
6
 
7
+ @legend = legend
8
+ @caption = caption
14
9
  @described_by = described_by(described_by)
15
10
  @attribute_name = attribute_name
16
-
17
- case legend
18
- when Proc
19
- @legend_raw = legend.call
20
- when Hash
21
- @legend_options = legend_defaults.merge(legend)
22
- @caption = caption
23
- else
24
- fail(ArgumentError, %(legend must be a Proc or Hash))
25
- end
26
11
  end
27
12
 
28
13
  def html
29
- content_tag('fieldset', class: fieldset_classes, aria: { describedby: @described_by }) do
30
- safe_join([legend_content, (@block_content || yield)])
14
+ tag.fieldset(**options) do
15
+ safe_join([legend_element, (@block_content || yield)])
31
16
  end
32
17
  end
33
18
 
34
19
  private
35
20
 
36
- def legend_content
37
- @legend_raw || legend
38
- end
39
-
40
- def legend
41
- if legend_text.present?
42
- content_tag('legend', class: legend_classes) do
43
- content_tag(@legend_options.dig(:tag), class: legend_heading_classes) do
44
- safe_join([caption_element, legend_text])
45
- end
46
- end
47
- end
48
- end
49
-
50
- def legend_text
51
- [@legend_options.dig(:text), localised_text(:legend)].compact.first
21
+ def options
22
+ {
23
+ class: classes,
24
+ aria: { describedby: @described_by }
25
+ }
52
26
  end
53
27
 
54
- def fieldset_classes
55
- %w(fieldset).prefix(brand)
28
+ def classes
29
+ %(#{brand}-fieldset)
56
30
  end
57
31
 
58
- def legend_classes
59
- size = @legend_options.dig(:size)
60
- fail "invalid size '#{size}', must be #{LEGEND_SIZES.join(', ')}" unless size.in?(LEGEND_SIZES)
61
-
62
- [%(fieldset__legend), %(fieldset__legend--#{size})].prefix(brand).tap do |classes|
63
- classes.push(%(#{brand}-visually-hidden)) if @legend_options.dig(:hidden)
64
- end
32
+ def legend_element
33
+ @legend_element ||= Elements::Legend.new(@builder, @object_name, @attribute_name, **legend_options)
65
34
  end
66
35
 
67
- def legend_heading_classes
68
- %w(fieldset__heading).prefix(brand)
69
- end
70
-
71
- def legend_defaults
72
- {
73
- hidden: false,
74
- text: nil,
75
- tag: config.default_legend_tag,
76
- size: config.default_legend_size
77
- }
36
+ def legend_options
37
+ { legend: @legend, caption: @caption }
78
38
  end
79
39
  end
80
40
  end
@@ -1,24 +1,33 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
3
  class FormGroup < Base
4
- using PrefixableArray
5
-
6
- def initialize(builder, object_name, attribute_name)
4
+ def initialize(builder, object_name, attribute_name, classes: nil, **kwargs)
7
5
  super(builder, object_name, attribute_name)
6
+
7
+ @classes = classes
8
+ @extra_options = kwargs
8
9
  end
9
10
 
10
11
  def html
11
- content_tag('div', class: form_group_classes) do
12
- yield
13
- end
12
+ tag.div(class: classes, **@extra_options) { yield }
14
13
  end
15
14
 
16
15
  private
17
16
 
18
- def form_group_classes
19
- %w(form-group).prefix(brand).tap do |classes|
20
- classes.push(%(#{brand}-form-group--error)) if has_errors?
21
- end
17
+ def classes
18
+ [form_group_class, error_class, custom_classes].flatten.compact
19
+ end
20
+
21
+ def form_group_class
22
+ %(#{brand}-form-group)
23
+ end
24
+
25
+ def error_class
26
+ %(#{brand}-form-group--error) if has_errors?
27
+ end
28
+
29
+ def custom_classes
30
+ Array.wrap(@classes)
22
31
  end
23
32
  end
24
33
  end
@@ -4,21 +4,22 @@ 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:, &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
10
  @inline = inline
11
11
  @small = small
12
12
  @legend = legend
13
13
  @caption = caption
14
- @hint_text = hint_text
14
+ @hint = hint
15
15
  @classes = classes
16
+ @form_group = form_group
16
17
  @block_content = capture { block.call }
17
18
  end
18
19
 
19
20
  def html
20
- Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
21
- Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, caption: @caption, described_by: [error_element.error_id, hint_element.hint_id]).html do
21
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
22
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
22
23
  safe_join([hint_element, error_element, radios])
23
24
  end
24
25
  end
@@ -26,6 +27,14 @@ module GOVUKDesignSystemFormBuilder
26
27
 
27
28
  private
28
29
 
30
+ def fieldset_options
31
+ {
32
+ legend: @legend,
33
+ caption: @caption,
34
+ described_by: [error_element.error_id, hint_element.hint_id]
35
+ }
36
+ end
37
+
29
38
  def radios
30
39
  Containers::Radios.new(@builder, inline: @inline, small: @small, classes: @classes).html do
31
40
  @block_content
@@ -1,8 +1,6 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
3
  class Radios < Base
4
- using PrefixableArray
5
-
6
4
  include Traits::Hint
7
5
 
8
6
  def initialize(builder, inline:, small:, classes:)
@@ -13,19 +11,32 @@ module GOVUKDesignSystemFormBuilder
13
11
  end
14
12
 
15
13
  def html
16
- content_tag('div', class: radios_classes, data: { module: %(#{brand}-radios) }) do
17
- yield
18
- end
14
+ tag.div(**options) { yield }
19
15
  end
20
16
 
21
17
  private
22
18
 
23
- def radios_classes
24
- %w(radios).prefix(brand).tap do |c|
25
- c.push(%(#{brand}-radios--inline)) if @inline
26
- c.push(%(#{brand}-radios--small)) if @small
27
- c.push(@classes) if @classes
28
- end
19
+ def options
20
+ {
21
+ class: classes,
22
+ data: { module: %(#{brand}-radios) }
23
+ }
24
+ end
25
+
26
+ def classes
27
+ [%(#{brand}-radios), inline_class, small_class, custom_classes].flatten.compact
28
+ end
29
+
30
+ def inline_class
31
+ %(#{brand}-radios--inline) if @inline
32
+ end
33
+
34
+ def small_class
35
+ %(#{brand}-radios--small) if @small
36
+ end
37
+
38
+ def custom_classes
39
+ Array.wrap(@classes)
29
40
  end
30
41
  end
31
42
  end
@@ -10,11 +10,11 @@ module GOVUKDesignSystemFormBuilder
10
10
  def html
11
11
  return nil if @content.blank?
12
12
 
13
- content_tag('div', id: supplemental_id) do
14
- @content
15
- end
13
+ tag.div(id: supplemental_id) { @content }
16
14
  end
17
15
 
16
+ private
17
+
18
18
  def supplemental_id
19
19
  build_id('supplemental')
20
20
  end
@@ -6,8 +6,8 @@ module GOVUKDesignSystemFormBuilder
6
6
  def initialize(builder, object_name, attribute_name, text:, size: nil)
7
7
  super(builder, object_name, attribute_name)
8
8
 
9
- @text = caption_text(text)
10
- @size_class = caption_size_class(size)
9
+ @text = text(text)
10
+ @size_class = size_class(size)
11
11
  end
12
12
 
13
13
  def html
@@ -16,11 +16,13 @@ module GOVUKDesignSystemFormBuilder
16
16
  tag.span(@text, class: @size_class)
17
17
  end
18
18
 
19
- def caption_text(override)
19
+ private
20
+
21
+ def text(override)
20
22
  override || localised_text(:caption)
21
23
  end
22
24
 
23
- def caption_size_class(size)
25
+ def size_class(size)
24
26
  case size || config.default_caption_size
25
27
  when 'xl' then %(#{brand}-caption-xl)
26
28
  when 'l' then %(#{brand}-caption-l)
@@ -6,23 +6,24 @@ 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:, &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
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
21
22
  end
22
23
 
23
24
  def html
24
- Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
25
- Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, caption: @caption, described_by: [error_id, hint_id, supplemental_id]).html do
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
27
  safe_join([supplemental_content, hint_element, error_element, check_boxes])
27
28
  end
28
29
  end
@@ -30,6 +31,14 @@ module GOVUKDesignSystemFormBuilder
30
31
 
31
32
  private
32
33
 
34
+ def fieldset_options
35
+ {
36
+ legend: @legend,
37
+ caption: @caption,
38
+ described_by: [error_id, hint_id, supplemental_id]
39
+ }
40
+ end
41
+
33
42
  def check_boxes
34
43
  Containers::CheckBoxes.new(@builder, small: @small, classes: @classes).html do
35
44
  collection
@@ -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
@@ -26,10 +24,10 @@ module GOVUKDesignSystemFormBuilder
26
24
  private
27
25
 
28
26
  def check_box
29
- @checkbox.check_box(**check_box_options)
27
+ @checkbox.check_box(**options)
30
28
  end
31
29
 
32
- def check_box_options
30
+ def options
33
31
  {
34
32
  id: field_id(link_errors: @link_errors),
35
33
  class: %(#{brand}-checkboxes__input),
@@ -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