govuk_design_system_formbuilder 1.2.7 → 2.0.0b4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/lib/govuk_design_system_formbuilder/builder.rb +213 -90
  4. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +21 -10
  5. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +4 -3
  6. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +10 -10
  7. data/lib/govuk_design_system_formbuilder/elements/caption.rb +10 -5
  8. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +12 -12
  9. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +6 -2
  10. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +13 -8
  11. data/lib/govuk_design_system_formbuilder/elements/date.rb +8 -8
  12. data/lib/govuk_design_system_formbuilder/elements/file.rb +8 -8
  13. data/lib/govuk_design_system_formbuilder/elements/hint.rb +35 -8
  14. data/lib/govuk_design_system_formbuilder/elements/label.rb +18 -12
  15. data/lib/govuk_design_system_formbuilder/elements/legend.rb +18 -10
  16. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +14 -14
  17. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +9 -1
  18. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +12 -4
  19. data/lib/govuk_design_system_formbuilder/elements/select.rb +11 -11
  20. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +11 -11
  21. data/lib/govuk_design_system_formbuilder/traits/caption.rb +1 -9
  22. data/lib/govuk_design_system_formbuilder/traits/collection_item.rb +1 -1
  23. data/lib/govuk_design_system_formbuilder/traits/hint.rb +15 -2
  24. data/lib/govuk_design_system_formbuilder/traits/input.rb +49 -13
  25. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  26. metadata +11 -11
@@ -4,28 +4,39 @@ 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:, multiple:, &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
+ @multiple = multiple
17
+ @block_content = capture { block.call }
17
18
  end
18
19
 
19
20
  def html
20
- 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
21
22
  Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
22
- safe_join([hint_element, error_element, checkboxes])
23
+ safe_join([hint_element, error_element, hidden_field, checkboxes])
23
24
  end
24
25
  end
25
26
  end
26
27
 
27
28
  private
28
29
 
30
+ def hidden_field
31
+ return unless @multiple
32
+
33
+ @builder.hidden_field(@attribute_name, value: "", name: hidden_field_name)
34
+ end
35
+
36
+ def hidden_field_name
37
+ format("%<object_name>s[%<attribute_name>s][]", object_name: @object_name, attribute_name: @attribute_name)
38
+ end
39
+
29
40
  def fieldset_options
30
41
  {
31
42
  legend: @legend,
@@ -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
- 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
@@ -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
@@ -11,7 +11,7 @@ module GOVUKDesignSystemFormBuilder
11
11
  @checkbox = checkbox
12
12
  @item = checkbox.object
13
13
  @value = checkbox.value
14
- @hint_text = retrieve(@item, hint_method)
14
+ @hint = { text: retrieve(@item, hint_method) }
15
15
  @link_errors = link_errors
16
16
  end
17
17
 
@@ -40,7 +40,11 @@ module GOVUKDesignSystemFormBuilder
40
40
  end
41
41
 
42
42
  def hint_element
43
- @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 }
44
48
  end
45
49
  end
46
50
  end
@@ -8,14 +8,15 @@ 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, unchecked_value, label:, hint:, link_errors:, multiple:, &block)
12
12
  super(builder, object_name, attribute_name)
13
13
 
14
- @value = value
15
- @label = label
16
- @hint_text = hint_text
17
- @multiple = multiple
18
- @link_errors = link_errors
14
+ @value = value
15
+ @unchecked_value = unchecked_value
16
+ @label = label
17
+ @hint = hint
18
+ @multiple = multiple
19
+ @link_errors = link_errors
19
20
 
20
21
  if block_given?
21
22
  @conditional_content = wrap_conditional(block)
@@ -36,7 +37,7 @@ module GOVUKDesignSystemFormBuilder
36
37
  end
37
38
 
38
39
  def check_box
39
- @builder.check_box(@attribute_name, options, @value, false)
40
+ @builder.check_box(@attribute_name, options, @value, @unchecked_value)
40
41
  end
41
42
 
42
43
  def options
@@ -62,7 +63,11 @@ module GOVUKDesignSystemFormBuilder
62
63
  end
63
64
 
64
65
  def hint_element
65
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, checkbox: true)
66
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
67
+ end
68
+
69
+ def hint_options
70
+ { checkbox: true }
66
71
  end
67
72
 
68
73
  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
@@ -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
@@ -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,10 +37,14 @@ 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
@@ -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
- 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