govuk_design_system_formbuilder 1.2.4 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/lib/govuk_design_system_formbuilder/builder.rb +46 -29
  3. data/lib/govuk_design_system_formbuilder/containers/character_count.rb +2 -2
  4. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +13 -12
  5. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +9 -8
  6. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +12 -62
  7. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +18 -10
  8. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +10 -9
  9. data/lib/govuk_design_system_formbuilder/containers/radios.rb +17 -13
  10. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +2 -0
  11. data/lib/govuk_design_system_formbuilder/elements/caption.rb +6 -4
  12. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +12 -11
  13. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +2 -2
  14. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +11 -11
  15. data/lib/govuk_design_system_formbuilder/elements/date.rb +19 -18
  16. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +3 -3
  17. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +15 -15
  18. data/lib/govuk_design_system_formbuilder/elements/file.rb +11 -10
  19. data/lib/govuk_design_system_formbuilder/elements/hint.rb +4 -4
  20. data/lib/govuk_design_system_formbuilder/elements/inputs/email.rb +2 -0
  21. data/lib/govuk_design_system_formbuilder/elements/label.rb +11 -11
  22. data/lib/govuk_design_system_formbuilder/elements/legend.rb +79 -0
  23. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +14 -13
  24. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +2 -2
  25. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +2 -2
  26. data/lib/govuk_design_system_formbuilder/elements/select.rb +18 -17
  27. data/lib/govuk_design_system_formbuilder/elements/submit.rb +9 -4
  28. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +7 -6
  29. data/lib/govuk_design_system_formbuilder/traits/input.rb +8 -7
  30. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  31. metadata +3 -2
@@ -1,24 +1,32 @@
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)
7
5
  super(builder, object_name, attribute_name)
6
+
7
+ @classes = classes
8
8
  end
9
9
 
10
10
  def html
11
- content_tag('div', class: form_group_classes) do
12
- yield
13
- end
11
+ content_tag('div', class: classes) { yield }
14
12
  end
15
13
 
16
14
  private
17
15
 
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
16
+ def classes
17
+ [form_group_class, error_class, custom_classes].flatten.compact
18
+ end
19
+
20
+ def form_group_class
21
+ %(#{brand}-form-group)
22
+ end
23
+
24
+ def error_class
25
+ %(#{brand}-form-group--error) if has_errors?
26
+ end
27
+
28
+ def custom_classes
29
+ Array.wrap(@classes)
22
30
  end
23
31
  end
24
32
  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_text:, legend:, caption:, inline:, small:, classes:, form_group_classes:, &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
- @block_content = capture { block.call }
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 }
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, classes: @form_group_classes).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
+ content_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
@@ -13,6 +13,8 @@ module GOVUKDesignSystemFormBuilder
13
13
  content_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
@@ -6,8 +6,8 @@ module GOVUKDesignSystemFormBuilder
6
6
  def initialize(builder, object_name, attribute_name, text:, size: nil)
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
11
  end
12
12
 
13
13
  def html
@@ -16,11 +16,13 @@ module GOVUKDesignSystemFormBuilder
16
16
  tag.span(@text, class: @size_class)
17
17
  end
18
18
 
19
- def caption_text(override)
19
+ private
20
+
21
+ def text(override)
20
22
  override || localised_text(:caption)
21
23
  end
22
24
 
23
- def caption_size_class(size)
25
+ def size_class(size)
24
26
  case size || config.default_caption_size
25
27
  when 'xl' then %(#{brand}-caption-xl)
26
28
  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_text:, legend:, caption:, small:, classes:, form_group_classes:, &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_text = hint_text
20
+ @classes = classes
21
+ @form_group_classes = form_group_classes
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, classes: @form_group_classes).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
@@ -26,10 +26,10 @@ module GOVUKDesignSystemFormBuilder
26
26
  private
27
27
 
28
28
  def check_box
29
- @checkbox.check_box(**check_box_options)
29
+ @checkbox.check_box(**options)
30
30
  end
31
31
 
32
- def check_box_options
32
+ def options
33
33
  {
34
34
  id: field_id(link_errors: @link_errors),
35
35
  class: %(#{brand}-checkboxes__input),
@@ -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
32
+ def item
33
33
  content_tag('div', class: %(#{brand}-checkboxes__item)) do
34
- safe_join([input, label_element, hint_element])
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
@@ -64,10 +68,6 @@ module GOVUKDesignSystemFormBuilder
64
68
  def conditional_classes
65
69
  %w(checkboxes__conditional checkboxes__conditional--hidden).prefix(brand)
66
70
  end
67
-
68
- def check_box_classes
69
- %w(checkboxes__input).prefix(brand)
70
- end
71
71
  end
72
72
  end
73
73
  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_text:, date_of_birth: false, omit_day:, form_group_classes:, &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
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
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, classes: @form_group_classes).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
@@ -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
@@ -13,17 +13,17 @@ module GOVUKDesignSystemFormBuilder
13
13
  return nil unless has_errors?
14
14
 
15
15
  content_tag('span', class: %(#{brand}-error-message), id: error_id) do
16
- safe_join([error_prefix, error_message])
16
+ safe_join([prefix, message])
17
17
  end
18
18
  end
19
19
 
20
20
  private
21
21
 
22
- def error_prefix
22
+ def 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
+ content_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
+ content_tag('div', class: summary_class('body')) do
28
+ content_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