govuk_design_system_formbuilder 1.2.0 → 1.2.5

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 (35) hide show
  1. checksums.yaml +4 -4
  2. data/lib/govuk_design_system_formbuilder.rb +4 -0
  3. data/lib/govuk_design_system_formbuilder/base.rb +5 -0
  4. data/lib/govuk_design_system_formbuilder/builder.rb +52 -33
  5. data/lib/govuk_design_system_formbuilder/containers/character_count.rb +8 -7
  6. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +18 -10
  7. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +27 -18
  8. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +15 -55
  9. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +18 -10
  10. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +28 -19
  11. data/lib/govuk_design_system_formbuilder/containers/radios.rb +22 -11
  12. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +3 -3
  13. data/lib/govuk_design_system_formbuilder/elements/caption.rb +8 -6
  14. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +29 -23
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +17 -15
  16. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +28 -32
  17. data/lib/govuk_design_system_formbuilder/elements/date.rb +55 -51
  18. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +7 -6
  19. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +24 -27
  20. data/lib/govuk_design_system_formbuilder/elements/file.rb +21 -22
  21. data/lib/govuk_design_system_formbuilder/elements/hint.rb +13 -16
  22. data/lib/govuk_design_system_formbuilder/elements/inputs/email.rb +2 -0
  23. data/lib/govuk_design_system_formbuilder/elements/label.rb +40 -29
  24. data/lib/govuk_design_system_formbuilder/elements/legend.rb +79 -0
  25. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +46 -36
  26. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +24 -14
  27. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +18 -19
  28. data/lib/govuk_design_system_formbuilder/elements/select.rb +28 -34
  29. data/lib/govuk_design_system_formbuilder/elements/submit.rb +34 -25
  30. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +39 -40
  31. data/lib/govuk_design_system_formbuilder/traits/caption.rb +5 -6
  32. data/lib/govuk_design_system_formbuilder/traits/input.rb +21 -29
  33. data/lib/govuk_design_system_formbuilder/traits/label.rb +2 -2
  34. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  35. metadata +16 -15
@@ -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
+ content_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 || build_legend
38
- end
39
-
40
- def build_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.html, 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,32 @@
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)
7
5
  super(builder, object_name, attribute_name)
6
+
7
+ @classes = classes
8
8
  end
9
9
 
10
10
  def html
11
- content_tag('div', class: form_group_classes) do
12
- yield
13
- end
11
+ content_tag('div', class: classes) { yield }
14
12
  end
15
13
 
16
14
  private
17
15
 
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
16
+ def classes
17
+ [form_group_class, error_class, custom_classes].flatten.compact
18
+ end
19
+
20
+ def form_group_class
21
+ %(#{brand}-form-group)
22
+ end
23
+
24
+ def error_class
25
+ %(#{brand}-form-group--error) if has_errors?
26
+ end
27
+
28
+ def custom_classes
29
+ Array.wrap(@classes)
22
30
  end
23
31
  end
24
32
  end
@@ -4,33 +4,42 @@ 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_text:, legend:, caption:, inline:, small:, classes:, form_group_classes:, &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
- @block_content = capture { block.call }
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 }
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
22
- safe_join(
23
- [
24
- hint_element.html,
25
- error_element.html,
26
- Containers::Radios.new(@builder, inline: @inline, small: @small, classes: @classes).html do
27
- @block_content
28
- end
29
- ]
30
- )
21
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
22
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
23
+ safe_join([hint_element, error_element, radios])
31
24
  end
32
25
  end
33
26
  end
27
+
28
+ private
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
+
38
+ def radios
39
+ Containers::Radios.new(@builder, inline: @inline, small: @small, classes: @classes).html do
40
+ @block_content
41
+ end
42
+ end
34
43
  end
35
44
  end
36
45
  end
@@ -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
+ content_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
+ content_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
@@ -3,11 +3,11 @@ module GOVUKDesignSystemFormBuilder
3
3
  class Caption < Base
4
4
  include Traits::Localisation
5
5
 
6
- def initialize(builder, object_name, attribute_name, text:, size: 'm')
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,12 +16,14 @@ 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)
24
- case size
25
+ def size_class(size)
26
+ case size || config.default_caption_size
25
27
  when 'xl' then %(#{brand}-caption-xl)
26
28
  when 'l' then %(#{brand}-caption-l)
27
29
  when 'm' then %(#{brand}-caption-m)
@@ -6,39 +6,45 @@ 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_text:, legend:, caption:, small:, classes:, form_group_classes:, &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_text = hint_text
20
+ @classes = classes
21
+ @form_group_classes = form_group_classes
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
26
- safe_join(
27
- [
28
- supplemental_content.html,
29
- hint_element.html,
30
- error_element.html,
31
- Containers::CheckBoxes.new(@builder, small: @small, classes: @classes).html do
32
- build_collection
33
- end
34
- ]
35
- )
25
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
26
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
27
+ safe_join([supplemental_content, hint_element, error_element, check_boxes])
36
28
  end
37
29
  end
38
30
  end
39
31
 
40
32
  private
41
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
+
42
+ def check_boxes
43
+ Containers::CheckBoxes.new(@builder, small: @small, classes: @classes).html do
44
+ collection
45
+ end
46
+ end
47
+
42
48
  # Builds a collection of check {Elements::CheckBoxes::CheckBox}
43
49
  # @return [ActiveSupport::SafeBuffer] HTML output
44
50
  #
@@ -47,7 +53,7 @@ module GOVUKDesignSystemFormBuilder
47
53
  # be rendered when it happens we need to work on the chance that it might, so
48
54
  # the +link_errors+ variable is set to +true+ if this attribute has errors and
49
55
  # always set back to +false+ after the first checkbox has been rendered
50
- def build_collection
56
+ def collection
51
57
  link_errors = has_errors?
52
58
 
53
59
  @builder.collection_check_boxes(@attribute_name, @collection, @value_method, @text_method) do |check_box|
@@ -10,31 +10,33 @@ module GOVUKDesignSystemFormBuilder
10
10
  def initialize(builder, object_name, attribute_name, checkbox, hint_method = nil, link_errors: false)
11
11
  super(builder, object_name, attribute_name)
12
12
 
13
- @checkbox = checkbox
14
- @item = checkbox.object
15
- @value = checkbox.value
16
- @hint_text = retrieve(@item, hint_method)
13
+ @checkbox = checkbox
14
+ @item = checkbox.object
15
+ @value = checkbox.value
16
+ @hint_text = retrieve(@item, hint_method)
17
17
  @link_errors = link_errors
18
18
  end
19
19
 
20
20
  def html
21
21
  content_tag('div', class: %(#{brand}-checkboxes__item)) do
22
- safe_join(
23
- [
24
- @checkbox.check_box(
25
- id: field_id(link_errors: @link_errors),
26
- class: %(#{brand}-checkboxes__input),
27
- aria: { describedby: hint_id }
28
- ),
29
- label_element.html,
30
- hint_element.html
31
- ]
32
- )
22
+ safe_join([check_box, label_element, hint_element])
33
23
  end
34
24
  end
35
25
 
36
26
  private
37
27
 
28
+ def check_box
29
+ @checkbox.check_box(**options)
30
+ end
31
+
32
+ def options
33
+ {
34
+ id: field_id(link_errors: @link_errors),
35
+ class: %(#{brand}-checkboxes__input),
36
+ aria: { describedby: hint_id }
37
+ }
38
+ end
39
+
38
40
  def label_element
39
41
  @label_element ||= Label.new(@builder, @object_name, @attribute_name, @checkbox, value: @value, link_errors: @link_errors)
40
42
  end
@@ -24,41 +24,41 @@ module GOVUKDesignSystemFormBuilder
24
24
  end
25
25
 
26
26
  def html
27
- safe_join(
28
- [
29
- content_tag('div', class: %(#{brand}-checkboxes__item)) do
30
- safe_join(
31
- [
32
- input,
33
- label_element.html,
34
- hint_element.html,
35
- ]
36
- )
37
- end,
38
- @conditional_content
39
- ]
40
- )
27
+ safe_join([item, @conditional_content])
41
28
  end
42
29
 
43
30
  private
44
31
 
45
- def input
46
- @builder.check_box(
47
- @attribute_name,
48
- {
49
- id: field_id(link_errors: @link_errors),
50
- class: check_box_classes,
51
- multiple: @multiple,
52
- aria: { describedby: hint_id },
53
- data: { 'aria-controls' => @conditional_id }
54
- },
55
- @value,
56
- false
57
- )
32
+ def item
33
+ content_tag('div', class: %(#{brand}-checkboxes__item)) do
34
+ safe_join([check_box, label_element, hint_element])
35
+ end
36
+ end
37
+
38
+ def check_box
39
+ @builder.check_box(@attribute_name, options, @value, false)
40
+ end
41
+
42
+ def options
43
+ {
44
+ id: field_id(link_errors: @link_errors),
45
+ class: classes,
46
+ multiple: @multiple,
47
+ aria: { describedby: hint_id },
48
+ data: { 'aria-controls' => @conditional_id }
49
+ }
50
+ end
51
+
52
+ def classes
53
+ %w(checkboxes__input).prefix(brand)
58
54
  end
59
55
 
60
56
  def label_element
61
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, checkbox: true, value: @value, link_errors: @link_errors, **label_args)
57
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_content, **label_options)
58
+ end
59
+
60
+ def label_options
61
+ { checkbox: true, value: @value, link_errors: @link_errors }
62
62
  end
63
63
 
64
64
  def hint_element
@@ -68,10 +68,6 @@ module GOVUKDesignSystemFormBuilder
68
68
  def conditional_classes
69
69
  %w(checkboxes__conditional checkboxes__conditional--hidden).prefix(brand)
70
70
  end
71
-
72
- def check_box_classes
73
- %w(checkboxes__input).prefix(brand)
74
- end
75
71
  end
76
72
  end
77
73
  end