govuk_design_system_formbuilder 1.2.9 → 2.0.0b1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,20 +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:, form_group_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
- @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
+ @block_content = capture { block.call }
17
17
  end
18
18
 
19
19
  def html
20
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
20
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
21
21
  Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
22
22
  safe_join([hint_element, error_element, checkboxes])
23
23
  end
@@ -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
+ @extra_options = kwargs
8
9
  end
9
10
 
10
11
  def html
11
- tag.div(class: classes) { yield }
12
+ tag.div(class: classes, **@extra_options) { 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
@@ -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,15 +8,14 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::Hint
9
9
  include Traits::Conditional
10
10
 
11
- def initialize(builder, object_name, attribute_name, value, unchecked_value, label:, hint_text:, link_errors:, multiple:, &block)
11
+ def initialize(builder, object_name, attribute_name, value, label:, hint:, link_errors:, multiple:, &block)
12
12
  super(builder, object_name, attribute_name)
13
13
 
14
- @value = value
15
- @unchecked_value = unchecked_value
16
- @label = label
17
- @hint_text = hint_text
18
- @multiple = multiple
19
- @link_errors = link_errors
14
+ @value = value
15
+ @label = label
16
+ @hint = hint
17
+ @multiple = multiple
18
+ @link_errors = link_errors
20
19
 
21
20
  if block_given?
22
21
  @conditional_content = wrap_conditional(block)
@@ -37,7 +36,7 @@ module GOVUKDesignSystemFormBuilder
37
36
  end
38
37
 
39
38
  def check_box
40
- @builder.check_box(@attribute_name, options, @value, @unchecked_value)
39
+ @builder.check_box(@attribute_name, options, @value, false)
41
40
  end
42
41
 
43
42
  def options
@@ -63,7 +62,11 @@ module GOVUKDesignSystemFormBuilder
63
62
  end
64
63
 
65
64
  def hint_element
66
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, checkbox: true)
65
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
66
+ end
67
+
68
+ def hint_options
69
+ { checkbox: true }
67
70
  end
68
71
 
69
72
  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
+ @extra_options = 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
@@ -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
+ @radio = radio
12
+ @checkbox = checkbox
13
+ @html_attributes = kwargs
14
+
15
+ if content
16
+ @content = capture { content.call }
17
+ else
18
+ @text = retrieve_text(text)
19
+ @value = value
20
+ end
21
+ end
22
+
23
+ def active?
24
+ [@text, @content].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
+ @content.presence ? 'div' : 'span'
47
+ end
48
+
49
+ def hint_body
50
+ @content || @text
51
+ end
52
+
26
53
  def retrieve_text(supplied)
27
54
  supplied.presence || localised_text(:hint)
28
55
  end
@@ -26,7 +26,7 @@ module GOVUKDesignSystemFormBuilder
26
26
  end
27
27
 
28
28
  def html
29
- return nil if [@content, @text].all?(&:blank?)
29
+ return nil unless active?
30
30
 
31
31
  if @tag.present?
32
32
  content_tag(@tag, class: %(#{brand}-label-wrapper)) { label }
@@ -35,6 +35,10 @@ module GOVUKDesignSystemFormBuilder
35
35
  end
36
36
  end
37
37
 
38
+ def active?
39
+ [@content, @text].any?(&:present?)
40
+ end
41
+
38
42
  private
39
43
 
40
44
  def label
@@ -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
@@ -41,7 +41,15 @@ module GOVUKDesignSystemFormBuilder
41
41
  end
42
42
 
43
43
  def hint_element
44
- @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 }
45
53
  end
46
54
 
47
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?
@@ -34,16 +34,24 @@ module GOVUKDesignSystemFormBuilder
34
34
  end
35
35
  end
36
36
 
37
+ def radio_options
38
+ { radio: true }
39
+ end
40
+
37
41
  def label_element
38
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_content, **label_options)
42
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_content, **label_options, **@label)
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