govuk_design_system_formbuilder 1.2.4 → 2.0.0b2

Sign up to get free protection for your applications and to get access to all the features.
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 +201 -78
  6. data/lib/govuk_design_system_formbuilder/containers/character_count.rb +2 -2
  7. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +13 -12
  8. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +4 -3
  9. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +12 -62
  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 +4 -3
  12. data/lib/govuk_design_system_formbuilder/containers/radios.rb +17 -13
  13. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +3 -1
  14. data/lib/govuk_design_system_formbuilder/elements/caption.rb +14 -7
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +12 -11
  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 +19 -15
  18. data/lib/govuk_design_system_formbuilder/elements/date.rb +18 -17
  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 +11 -10
  22. data/lib/govuk_design_system_formbuilder/elements/hint.rb +37 -10
  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 +28 -22
  30. data/lib/govuk_design_system_formbuilder/elements/legend.rb +87 -0
  31. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +14 -13
  32. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +13 -7
  33. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +15 -7
  34. data/lib/govuk_design_system_formbuilder/elements/select.rb +18 -17
  35. data/lib/govuk_design_system_formbuilder/elements/submit.rb +9 -4
  36. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +10 -9
  37. data/lib/govuk_design_system_formbuilder/traits/caption.rb +1 -9
  38. data/lib/govuk_design_system_formbuilder/traits/collection_item.rb +1 -1
  39. data/lib/govuk_design_system_formbuilder/traits/conditional.rb +1 -1
  40. data/lib/govuk_design_system_formbuilder/traits/hint.rb +15 -2
  41. data/lib/govuk_design_system_formbuilder/traits/input.rb +4 -3
  42. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  43. metadata +12 -11
@@ -14,12 +14,12 @@ module GOVUKDesignSystemFormBuilder
14
14
  def html
15
15
  return yield unless limit?
16
16
 
17
- content_tag('div', **character_count_options) { yield }
17
+ tag.div(**options) { yield }
18
18
  end
19
19
 
20
20
  private
21
21
 
22
- def character_count_options
22
+ def options
23
23
  {
24
24
  class: %(#{brand}-character-count),
25
25
  data: { module: %(#{brand}-character-count) }.merge(**limit, **threshold).compact
@@ -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,25 +8,28 @@ module GOVUKDesignSystemFormBuilder
10
8
  end
11
9
 
12
10
  def html
13
- content_tag('div', **check_boxes_options) do
14
- yield
15
- end
11
+ tag.div(**options) { yield }
16
12
  end
17
13
 
18
14
  private
19
15
 
20
- def check_boxes_options
16
+ def options
21
17
  {
22
- class: check_boxes_classes,
18
+ class: classes,
23
19
  data: { module: %(#{brand}-checkboxes) }
24
20
  }
25
21
  end
26
22
 
27
- def check_boxes_classes
28
- %w(checkboxes).prefix(brand).tap do |c|
29
- c.push(%(#{brand}-checkboxes--small)) if @small
30
- c.push(@classes) if @classes
31
- end
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)
32
33
  end
33
34
  end
34
35
  end
@@ -4,19 +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:, &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::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
20
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
@@ -1,90 +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', **fieldset_options) do
30
- safe_join([legend, (@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 fieldset_options
21
+ def options
37
22
  {
38
- class: fieldset_classes,
23
+ class: classes,
39
24
  aria: { describedby: @described_by }
40
25
  }
41
26
  end
42
27
 
43
- def fieldset_classes
44
- %w(fieldset).prefix(brand)
45
- end
46
-
47
- def legend
48
- @legend_raw || legend_content
49
- end
50
-
51
- def legend_content
52
- if legend_text.present?
53
- content_tag('legend', class: legend_classes) do
54
- content_tag(@legend_options.dig(:tag), class: legend_heading_classes) do
55
- safe_join([caption_element, legend_text])
56
- end
57
- end
58
- end
59
- end
60
-
61
- def legend_text
62
- @legend_options.dig(:text) || localised_text(:legend)
63
- end
64
-
65
- def legend_size
66
- @legend_options.dig(:size).tap do |size|
67
- fail "invalid size '#{size}', must be #{LEGEND_SIZES.join(', ')}" unless size.in?(LEGEND_SIZES)
68
- end
69
- end
70
-
71
- def legend_classes
72
- [%(fieldset__legend), %(fieldset__legend--#{legend_size})].prefix(brand).tap do |classes|
73
- classes.push(%(#{brand}-visually-hidden)) if @legend_options.dig(:hidden)
74
- end
28
+ def classes
29
+ %(#{brand}-fieldset)
75
30
  end
76
31
 
77
- def legend_heading_classes
78
- %w(fieldset__heading).prefix(brand)
32
+ def legend_element
33
+ @legend_element ||= Elements::Legend.new(@builder, @object_name, @attribute_name, **legend_options)
79
34
  end
80
35
 
81
- def legend_defaults
82
- {
83
- hidden: false,
84
- text: nil,
85
- tag: config.default_legend_tag,
86
- size: config.default_legend_size
87
- }
36
+ def legend_options
37
+ { legend: @legend, caption: @caption }
88
38
  end
89
39
  end
90
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
+ @html_attributes = 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, **@html_attributes) { 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,20 +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:, &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::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
21
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
@@ -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,26 +11,32 @@ module GOVUKDesignSystemFormBuilder
13
11
  end
14
12
 
15
13
  def html
16
- content_tag('div', **radios_options) do
17
- yield
18
- end
14
+ tag.div(**options) { yield }
19
15
  end
20
16
 
21
17
  private
22
18
 
23
- def radios_options
19
+ def options
24
20
  {
25
- class: radios_classes,
21
+ class: classes,
26
22
  data: { module: %(#{brand}-radios) }
27
23
  }
28
24
  end
29
25
 
30
- def radios_classes
31
- %w(radios).prefix(brand).tap do |c|
32
- c.push(%(#{brand}-radios--inline)) if @inline
33
- c.push(%(#{brand}-radios--small)) if @small
34
- c.push(@classes) if @classes
35
- end
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)
36
40
  end
37
41
  end
38
42
  end
@@ -10,9 +10,11 @@ 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
+ private
17
+
16
18
  def supplemental_id
17
19
  build_id('supplemental')
18
20
  end
@@ -3,24 +3,31 @@ 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 = caption_text(text)
10
- @size_class = caption_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
- def caption_text(override)
20
+ private
21
+
22
+ def active?
23
+ @text.present?
24
+ end
25
+
26
+ def text(override)
20
27
  override || localised_text(:caption)
21
28
  end
22
29
 
23
- def caption_size_class(size)
30
+ def size_class(size)
24
31
  case size || config.default_caption_size
25
32
  when 'xl' then %(#{brand}-caption-xl)
26
33
  when 'l' then %(#{brand}-caption-l)
@@ -6,22 +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:, &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::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
25
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
@@ -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