govuk_design_system_formbuilder 1.2.5 → 2.0.0b3

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -2,8 +2,6 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Email < Base
5
- using PrefixableArray
6
-
7
5
  include Traits::Input
8
6
  include Traits::Error
9
7
  include Traits::Hint
@@ -2,8 +2,6 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Number < Base
5
- using PrefixableArray
6
-
7
5
  include Traits::Input
8
6
  include Traits::Error
9
7
  include Traits::Hint
@@ -2,8 +2,6 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Password < Base
5
- using PrefixableArray
6
-
7
5
  include Traits::Input
8
6
  include Traits::Error
9
7
  include Traits::Hint
@@ -2,8 +2,6 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Phone < Base
5
- using PrefixableArray
6
-
7
5
  include Traits::Input
8
6
  include Traits::Error
9
7
  include Traits::Hint
@@ -2,8 +2,6 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Text < Base
5
- using PrefixableArray
6
-
7
5
  include Traits::Input
8
6
  include Traits::Error
9
7
  include Traits::Hint
@@ -2,8 +2,6 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class URL < Base
5
- using PrefixableArray
6
-
7
5
  include Traits::Input
8
6
  include Traits::Error
9
7
  include Traits::Hint
@@ -6,27 +6,29 @@ module GOVUKDesignSystemFormBuilder
6
6
  include Traits::Caption
7
7
  include Traits::Localisation
8
8
 
9
- def initialize(builder, object_name, attribute_name, text: nil, value: nil, size: nil, hidden: false, radio: false, checkbox: false, tag: nil, link_errors: true, content: nil, caption: nil)
9
+ def initialize(builder, object_name, attribute_name, text: nil, value: nil, size: nil, hidden: false, radio: false, checkbox: false, tag: nil, link_errors: true, content: nil, caption: nil, **kwargs)
10
10
  super(builder, object_name, attribute_name)
11
11
 
12
+ @value = value # used by field_id
13
+ @tag = tag
14
+ @radio = radio
15
+ @checkbox = checkbox
16
+ @link_errors = link_errors
17
+ @html_attributes = kwargs
18
+
12
19
  # content is passed in directly via a proc and overrides
13
20
  # the other display options
14
21
  if content
15
- @content = content.call
22
+ @content = capture { content.call }
16
23
  else
17
- @value = value # used by field_id
18
- @text = retrieve_text(text, hidden)
19
- @size_class = size_class(size)
20
- @radio = radio
21
- @checkbox = checkbox
22
- @tag = tag
23
- @link_errors = link_errors
24
- @caption = caption
24
+ @text = retrieve_text(text, hidden)
25
+ @size_class = size_class(size)
26
+ @caption = caption
25
27
  end
26
28
  end
27
29
 
28
30
  def html
29
- return nil if [@content, @text].all?(&:blank?)
31
+ return nil unless active?
30
32
 
31
33
  if @tag.present?
32
34
  content_tag(@tag, class: %(#{brand}-label-wrapper)) { label }
@@ -35,19 +37,23 @@ module GOVUKDesignSystemFormBuilder
35
37
  end
36
38
  end
37
39
 
40
+ def active?
41
+ [@content, @text].any?(&:present?)
42
+ end
43
+
38
44
  private
39
45
 
40
46
  def label
41
- @builder.label(@attribute_name, **options) do
47
+ @builder.label(@attribute_name, **options, **@html_attributes) do
42
48
  @content || safe_join([caption, @text])
43
49
  end
44
50
  end
45
51
 
46
52
  def retrieve_text(option_text, hidden)
47
- text = [option_text, localised_text(:label), @attribute_name.capitalize].compact.first.to_s
53
+ text = [option_text, localised_text(:label), @attribute_name.capitalize].compact.first
48
54
 
49
55
  if hidden
50
- tag.span(text, class: %w(visually-hidden).prefix(brand))
56
+ tag.span(text, class: %(#{brand}-visually-hidden))
51
57
  else
52
58
  text
53
59
  end
@@ -4,15 +4,19 @@ module GOVUKDesignSystemFormBuilder
4
4
  include Traits::Caption
5
5
  include Traits::Localisation
6
6
 
7
- def initialize(builder, object_name, attribute_name, legend:, caption:)
7
+ def initialize(builder, object_name, attribute_name, legend:, caption:, **kwargs)
8
8
  super(builder, object_name, attribute_name)
9
9
 
10
+ @html_attributes = kwargs
11
+
10
12
  case legend
13
+ when NilClass
14
+ @active = false
11
15
  when Proc
12
- @raw = legend.call
16
+ @raw = capture { legend.call }
13
17
  when Hash
14
18
  defaults.merge(legend).tap do |l|
15
- @text = text(l.dig(:text))
19
+ @text = retrieve_text(l.dig(:text))
16
20
  @hidden = l.dig(:hidden)
17
21
  @tag = l.dig(:tag)
18
22
  @size = l.dig(:size)
@@ -29,18 +33,22 @@ module GOVUKDesignSystemFormBuilder
29
33
 
30
34
  private
31
35
 
36
+ def active?
37
+ [@text, @raw].any?(&:present?)
38
+ end
39
+
32
40
  def content
33
- if @text.present?
34
- content_tag('legend', class: classes) do
35
- content_tag(@tag, class: heading_classes) do
36
- safe_join([caption_element, @text])
37
- end
41
+ return nil unless active?
42
+
43
+ tag.legend(class: classes, **@html_attributes) do
44
+ content_tag(@tag, class: heading_classes) do
45
+ safe_join([caption_element, @text])
38
46
  end
39
47
  end
40
48
  end
41
49
 
42
- def text(supplied_text)
43
- supplied_text || localised_text(:legend)
50
+ def retrieve_text(supplied_text)
51
+ [supplied_text, localised_text(:legend), @attribute_name&.capitalize].compact.first
44
52
  end
45
53
 
46
54
  def classes
@@ -6,25 +6,25 @@ 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:, hint_text:, legend:, caption:, inline:, small:, bold_labels:, classes:, form_group_classes:, &block)
9
+ def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint_method:, hint:, legend:, caption:, inline:, small:, bold_labels:, 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
- @inline = inline
17
- @small = small
18
- @legend = legend
19
- @caption = caption
20
- @hint_text = hint_text
21
- @classes = classes
22
- @form_group_classes = form_group_classes
23
- @bold_labels = hint_method.present? || bold_labels
12
+ @collection = collection
13
+ @value_method = value_method
14
+ @text_method = text_method
15
+ @hint_method = hint_method
16
+ @inline = inline
17
+ @small = small
18
+ @legend = legend
19
+ @caption = caption
20
+ @hint = hint
21
+ @classes = classes
22
+ @form_group = form_group
23
+ @bold_labels = hint_method.present? || bold_labels
24
24
  end
25
25
 
26
26
  def html
27
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
27
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
28
28
  Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
29
29
  safe_join([supplemental_content, hint_element, error_element, radios])
30
30
  end
@@ -2,8 +2,6 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Radios
4
4
  class CollectionRadioButton < Base
5
- using PrefixableArray
6
-
7
5
  include Traits::Hint
8
6
  include Traits::CollectionItem
9
7
 
@@ -23,7 +21,7 @@ module GOVUKDesignSystemFormBuilder
23
21
  end
24
22
 
25
23
  def html
26
- content_tag('div', class: %(#{brand}-radios__item)) do
24
+ tag.div(class: %(#{brand}-radios__item)) do
27
25
  safe_join([radio, label_element, hint_element])
28
26
  end
29
27
  end
@@ -38,12 +36,20 @@ module GOVUKDesignSystemFormBuilder
38
36
  {
39
37
  id: field_id(link_errors: @link_errors),
40
38
  aria: { describedby: hint_id },
41
- class: %w(radios__input).prefix(brand)
39
+ class: %(#{brand}-radios__input)
42
40
  }
43
41
  end
44
42
 
45
43
  def hint_element
46
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, radio: true)
44
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
45
+ end
46
+
47
+ def hint_content
48
+ { text: @hint_text }
49
+ end
50
+
51
+ def hint_options
52
+ { value: @value, radio: true }
47
53
  end
48
54
 
49
55
  def label_element
@@ -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:, &block)
11
+ def initialize(builder, object_name, attribute_name, value, label:, hint:, link_errors:, &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
  @link_errors = has_errors? && link_errors
18
18
 
19
19
  if block_given?
@@ -29,21 +29,29 @@ module GOVUKDesignSystemFormBuilder
29
29
  private
30
30
 
31
31
  def radio
32
- content_tag('div', class: %(#{brand}-radios__item)) do
32
+ tag.div(class: %(#{brand}-radios__item)) do
33
33
  safe_join([input, label_element, hint_element])
34
34
  end
35
35
  end
36
36
 
37
+ def radio_options
38
+ { radio: true }
39
+ end
40
+
37
41
  def label_element
38
42
  @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_content, **label_options)
39
43
  end
40
44
 
41
45
  def label_options
42
- { radio: true, value: @value, link_errors: @link_errors }
46
+ { value: @value, link_errors: @link_errors }.merge(radio_options)
43
47
  end
44
48
 
45
49
  def hint_element
46
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, radio: true)
50
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
51
+ end
52
+
53
+ def hint_options
54
+ { value: @value }.merge(radio_options)
47
55
  end
48
56
 
49
57
  def input
@@ -6,22 +6,22 @@ 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:, options: {}, html_options: {}, hint_text:, label:, caption:, form_group_classes:, &block)
9
+ def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, options: {}, html_options: {}, hint:, label:, caption:, 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
- @options = options
16
- @html_options = html_options
17
- @label = label
18
- @caption = caption
19
- @hint_text = hint_text
20
- @form_group_classes = form_group_classes
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
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
  safe_join([label_element, supplemental_content, hint_element, error_element, select])
26
26
  end
27
27
  end
@@ -8,23 +8,23 @@ 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:, rows:, max_words:, max_chars:, threshold:, form_group_classes:, **kwargs, &block)
11
+ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, rows:, max_words:, max_chars:, threshold:, form_group:, **kwargs, &block)
12
12
  super(builder, object_name, attribute_name, &block)
13
13
 
14
14
  @label = label
15
15
  @caption = caption
16
- @hint_text = hint_text
16
+ @hint = hint
17
17
  @max_words = max_words
18
18
  @max_chars = max_chars
19
19
  @threshold = threshold
20
20
  @rows = rows
21
- @form_group_classes = form_group_classes
21
+ @form_group = form_group
22
22
  @html_attributes = kwargs
23
23
  end
24
24
 
25
25
  def html
26
26
  Containers::CharacterCount.new(@builder, **character_count_options).html do
27
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
27
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
28
28
  safe_join([label_element, supplemental_content, hint_element, error_element, text_area, limit_description])
29
29
  end
30
30
  end
@@ -42,7 +42,7 @@ module GOVUKDesignSystemFormBuilder
42
42
 
43
43
  def classes
44
44
  %w(textarea).prefix(brand).tap do |classes|
45
- classes.push(%(#{brand}-textarea--error)) if has_errors?
45
+ classes.push(%(#{brand}-textarea--error)) if has_errors?
46
46
  classes.push(%(#{brand}-js-character-count)) if limit?
47
47
  end
48
48
  end
@@ -79,7 +79,7 @@ module GOVUKDesignSystemFormBuilder
79
79
  def limit_description
80
80
  return nil unless limit?
81
81
 
82
- content_tag('span', id: limit_id, class: limit_description_classes, aria: { live: 'polite' }) do
82
+ tag.span(id: limit_id, class: limit_description_classes, aria: { live: 'polite' }) do
83
83
  "You can enter up to #{limit_quantity} #{limit_type}"
84
84
  end
85
85
  end
@@ -8,15 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  end
9
9
 
10
10
  def caption_options
11
- { text: nil }.merge({ text: caption_text, size: caption_size }.compact)
12
- end
13
-
14
- def caption_text
15
- @caption&.dig(:text)
16
- end
17
-
18
- def caption_size
19
- @caption&.dig(:size)
11
+ @caption
20
12
  end
21
13
  end
22
14
  end
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  when Symbol, String
9
9
  item.send(method)
10
10
  when Proc
11
- method.call(item)
11
+ capture { method.call(item) }
12
12
  end
13
13
  end
14
14
  end
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  end
9
9
 
10
10
  def wrap_conditional(block)
11
- content_tag('div', class: conditional_classes, id: conditional_id) do
11
+ tag.div(class: conditional_classes, id: conditional_id) do
12
12
  capture { block.call }
13
13
  end
14
14
  end
@@ -2,7 +2,7 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Traits
3
3
  module Hint
4
4
  def hint_id
5
- return nil if @hint_text.blank?
5
+ return nil unless hint_element.active?
6
6
 
7
7
  build_id('hint')
8
8
  end
@@ -10,7 +10,20 @@ module GOVUKDesignSystemFormBuilder
10
10
  private
11
11
 
12
12
  def hint_element
13
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text)
13
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_content)
14
+ end
15
+
16
+ def hint_content
17
+ case @hint
18
+ when NilClass
19
+ {}
20
+ when Hash
21
+ @hint
22
+ when Proc
23
+ { content: @hint }
24
+ else
25
+ fail(ArgumentError, %(hint must be a Proc or Hash))
26
+ end
14
27
  end
15
28
  end
16
29
  end