govuk_design_system_formbuilder 1.2.4 → 2.0.0b2

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 (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
@@ -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:, 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
14
  @value = value
15
15
  @label = label
16
- @hint_text = hint_text
16
+ @hint = hint
17
17
  @multiple = multiple
18
18
  @link_errors = link_errors
19
19
 
@@ -24,31 +24,35 @@ module GOVUKDesignSystemFormBuilder
24
24
  end
25
25
 
26
26
  def html
27
- safe_join([check_box, @conditional_content])
27
+ safe_join([item, @conditional_content])
28
28
  end
29
29
 
30
30
  private
31
31
 
32
- def check_box
33
- content_tag('div', class: %(#{brand}-checkboxes__item)) do
34
- safe_join([input, label_element, hint_element])
32
+ def item
33
+ tag.div(class: %(#{brand}-checkboxes__item)) do
34
+ safe_join([check_box, label_element, hint_element])
35
35
  end
36
36
  end
37
37
 
38
- def input
39
- @builder.check_box(@attribute_name, input_options, @value, false)
38
+ def check_box
39
+ @builder.check_box(@attribute_name, options, @value, false)
40
40
  end
41
41
 
42
- def input_options
42
+ def options
43
43
  {
44
44
  id: field_id(link_errors: @link_errors),
45
- class: check_box_classes,
45
+ class: classes,
46
46
  multiple: @multiple,
47
47
  aria: { describedby: hint_id },
48
48
  data: { 'aria-controls' => @conditional_id }
49
49
  }
50
50
  end
51
51
 
52
+ def classes
53
+ %w(checkboxes__input).prefix(brand)
54
+ end
55
+
52
56
  def label_element
53
57
  @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_content, **label_options)
54
58
  end
@@ -58,15 +62,15 @@ module GOVUKDesignSystemFormBuilder
58
62
  end
59
63
 
60
64
  def hint_element
61
- @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)
62
66
  end
63
67
 
64
- def conditional_classes
65
- %w(checkboxes__conditional checkboxes__conditional--hidden).prefix(brand)
68
+ def hint_options
69
+ { checkbox: true }
66
70
  end
67
71
 
68
- def check_box_classes
69
- %w(checkboxes__input).prefix(brand)
72
+ def conditional_classes
73
+ %w(checkboxes__conditional checkboxes__conditional--hidden).prefix(brand)
70
74
  end
71
75
  end
72
76
  end
@@ -9,18 +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:, &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
15
  @legend = legend
16
16
  @caption = caption
17
- @hint_text = hint_text
17
+ @hint = hint
18
18
  @date_of_birth = date_of_birth
19
19
  @omit_day = omit_day
20
+ @form_group = form_group
20
21
  end
21
22
 
22
23
  def html
23
- Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
24
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
24
25
  Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
25
26
  safe_join([supplemental_content, hint_element, error_element, date])
26
27
  end
@@ -34,7 +35,7 @@ module GOVUKDesignSystemFormBuilder
34
35
  end
35
36
 
36
37
  def date
37
- content_tag('div', class: %(#{brand}-date-input)) do
38
+ tag.div(class: %(#{brand}-date-input)) do
38
39
  safe_join([day, month, year])
39
40
  end
40
41
  end
@@ -60,8 +61,8 @@ module GOVUKDesignSystemFormBuilder
60
61
  def date_part(segment, width:, link_errors: false)
61
62
  value = @builder.object.try(@attribute_name).try(segment)
62
63
 
63
- content_tag('div', class: %w(date-input__item).prefix(brand)) do
64
- content_tag('div', class: %w(form-group).prefix(brand)) do
64
+ tag.div(class: %(#{brand}-date-input__item)) do
65
+ tag.div(class: %(#{brand}-form-group)) do
65
66
  safe_join([label(segment, link_errors), input(segment, link_errors, width, value)])
66
67
  end
67
68
  end
@@ -71,15 +72,15 @@ module GOVUKDesignSystemFormBuilder
71
72
  tag.label(
72
73
  segment.capitalize,
73
74
  class: label_classes,
74
- for: input_id(segment, link_errors)
75
+ for: id(segment, link_errors)
75
76
  )
76
77
  end
77
78
 
78
79
  def input(segment, link_errors, width, value)
79
80
  tag.input(
80
- id: input_id(segment, link_errors),
81
- class: input_classes(width),
82
- name: input_name(segment),
81
+ id: id(segment, link_errors),
82
+ class: classes(width),
83
+ name: name(segment),
83
84
  type: 'text',
84
85
  pattern: '[0-9]*',
85
86
  inputmode: 'numeric',
@@ -88,21 +89,17 @@ module GOVUKDesignSystemFormBuilder
88
89
  )
89
90
  end
90
91
 
91
- def input_classes(width)
92
+ def classes(width)
92
93
  %w(input date-input__input).prefix(brand).tap do |classes|
93
94
  classes.push(%(#{brand}-input--width-#{width}))
94
95
  classes.push(%(#{brand}-input--error)) if has_errors?
95
96
  end
96
97
  end
97
98
 
98
- def label_classes
99
- %w(label date-input__label).prefix(brand)
100
- end
101
-
102
99
  # if the field has errors we want the govuk_error_summary to
103
100
  # be able to link to the day field. Otherwise, generate IDs
104
101
  # in the normal fashion
105
- def input_id(segment, link_errors)
102
+ def id(segment, link_errors)
106
103
  if has_errors? && link_errors
107
104
  field_id(link_errors: link_errors)
108
105
  else
@@ -110,7 +107,7 @@ module GOVUKDesignSystemFormBuilder
110
107
  end
111
108
  end
112
109
 
113
- def input_name(segment)
110
+ def name(segment)
114
111
  format(
115
112
  "%<object_name>s[%<input_name>s(%<segment>s)]",
116
113
  object_name: @object_name,
@@ -124,6 +121,10 @@ module GOVUKDesignSystemFormBuilder
124
121
 
125
122
  { day: 'bday-day', month: 'bday-month', year: 'bday-year' }.fetch(segment)
126
123
  end
124
+
125
+ def label_classes
126
+ %w(label date-input__label).prefix(brand)
127
+ end
127
128
  end
128
129
  end
129
130
  end
@@ -12,18 +12,18 @@ module GOVUKDesignSystemFormBuilder
12
12
  def html
13
13
  return nil unless has_errors?
14
14
 
15
- content_tag('span', class: %(#{brand}-error-message), id: error_id) do
16
- safe_join([error_prefix, error_message])
15
+ tag.span(class: %(#{brand}-error-message), id: error_id) do
16
+ safe_join([hidden_prefix, message])
17
17
  end
18
18
  end
19
19
 
20
20
  private
21
21
 
22
- def error_prefix
22
+ def hidden_prefix
23
23
  tag.span('Error: ', class: %(#{brand}-visually-hidden))
24
24
  end
25
25
 
26
- def error_message
26
+ def message
27
27
  @builder.object.errors.messages[@attribute_name]&.first
28
28
  end
29
29
  end
@@ -12,32 +12,32 @@ module GOVUKDesignSystemFormBuilder
12
12
  def html
13
13
  return nil unless object_has_errors?
14
14
 
15
- content_tag('div', class: error_summary_class, **error_summary_options) do
16
- safe_join([error_title, error_summary])
15
+ tag.div(class: summary_class, **summary_options) do
16
+ safe_join([title, summary])
17
17
  end
18
18
  end
19
19
 
20
20
  private
21
21
 
22
- def error_title
23
- tag.h2(@title, id: error_summary_title_id, class: error_summary_class('title'))
22
+ def title
23
+ tag.h2(@title, id: summary_title_id, class: summary_class('title'))
24
24
  end
25
25
 
26
- def error_summary
27
- content_tag('div', class: error_summary_class('body')) do
28
- content_tag('ul', class: [%(#{brand}-list), error_summary_class('list')]) do
29
- safe_join(error_list)
26
+ def summary
27
+ tag.div(class: summary_class('body')) do
28
+ tag.ul(class: [%(#{brand}-list), summary_class('list')]) do
29
+ safe_join(list)
30
30
  end
31
31
  end
32
32
  end
33
33
 
34
- def error_list
34
+ def list
35
35
  @builder.object.errors.messages.map do |attribute, messages|
36
- error_list_item(attribute, messages.first)
36
+ list_item(attribute, messages.first)
37
37
  end
38
38
  end
39
39
 
40
- def error_list_item(attribute, message)
40
+ def list_item(attribute, message)
41
41
  tag.li(link_to(message, same_page_link(field_id(attribute)), data: { turbolinks: false }))
42
42
  end
43
43
 
@@ -45,7 +45,7 @@ module GOVUKDesignSystemFormBuilder
45
45
  '#'.concat(target)
46
46
  end
47
47
 
48
- def error_summary_class(part = nil)
48
+ def summary_class(part = nil)
49
49
  if part
50
50
  %(#{brand}-error-summary).concat('__', part)
51
51
  else
@@ -57,7 +57,7 @@ module GOVUKDesignSystemFormBuilder
57
57
  build_id('field-error', attribute_name: attribute)
58
58
  end
59
59
 
60
- def error_summary_title_id
60
+ def summary_title_id
61
61
  'error-summary-title'
62
62
  end
63
63
 
@@ -65,7 +65,7 @@ module GOVUKDesignSystemFormBuilder
65
65
  @builder.object.errors.any?
66
66
  end
67
67
 
68
- def error_summary_options
68
+ def summary_options
69
69
  {
70
70
  tabindex: -1,
71
71
  role: 'alert',
@@ -73,7 +73,7 @@ module GOVUKDesignSystemFormBuilder
73
73
  module: %(#{brand}-error-summary)
74
74
  },
75
75
  aria: {
76
- labelledby: error_summary_title_id
76
+ labelledby: summary_title_id
77
77
  }
78
78
  }
79
79
  end
@@ -8,17 +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:, **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
14
+ @label = label
15
+ @caption = caption
16
+ @hint = hint
17
+ @html_attributes = kwargs
18
+ @form_group = form_group
18
19
  end
19
20
 
20
21
  def html
21
- Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
22
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
22
23
  safe_join([label_element, supplemental_content, hint_element, error_element, file])
23
24
  end
24
25
  end
@@ -26,18 +27,18 @@ module GOVUKDesignSystemFormBuilder
26
27
  private
27
28
 
28
29
  def file
29
- @builder.file_field(@attribute_name, **file_options, **@extra_options)
30
+ @builder.file_field(@attribute_name, **options, **@html_attributes)
30
31
  end
31
32
 
32
- def file_options
33
+ def options
33
34
  {
34
35
  id: field_id(link_errors: true),
35
- class: file_classes,
36
+ class: classes,
36
37
  aria: { describedby: described_by(hint_id, error_id, supplemental_id) }
37
38
  }
38
39
  end
39
40
 
40
- def file_classes
41
+ def classes
41
42
  %w(file-upload).prefix(brand).tap do |c|
42
43
  c.push(%(#{brand}-file-upload--error)) if has_errors?
43
44
  end
@@ -3,31 +3,58 @@ 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 = hint_text(text)
14
- @radio = radio
15
- @checkbox = checkbox
11
+ @radio = radio
12
+ @checkbox = checkbox
13
+ @html_attributes = kwargs
14
+
15
+ if content
16
+ @raw = capture { content.call }
17
+ else
18
+ @text = retrieve_text(text)
19
+ @value = value
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: hint_classes, id: hint_id)
36
+ build_id('hint')
22
37
  end
23
38
 
24
39
  private
25
40
 
26
- def hint_text(supplied)
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
+
53
+ def retrieve_text(supplied)
27
54
  supplied.presence || localised_text(:hint)
28
55
  end
29
56
 
30
- def hint_classes
57
+ def classes
31
58
  %w(hint).prefix(brand).push(radio_class, checkbox_class).compact
32
59
  end
33
60
 
@@ -2,14 +2,14 @@ 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
10
8
  include Traits::Label
11
9
  include Traits::Supplemental
12
10
 
11
+ private
12
+
13
13
  def builder_method
14
14
  :email_field
15
15
  end
@@ -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