govuk_design_system_formbuilder 1.1.10 → 1.2.0

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 +7 -1
  4. data/lib/govuk_design_system_formbuilder/base.rb +12 -0
  5. data/lib/govuk_design_system_formbuilder/builder.rb +311 -74
  6. data/lib/govuk_design_system_formbuilder/containers/character_count.rb +2 -2
  7. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +5 -3
  8. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +3 -2
  9. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +37 -16
  10. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +4 -2
  11. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +3 -2
  12. data/lib/govuk_design_system_formbuilder/containers/radios.rb +7 -5
  13. data/lib/govuk_design_system_formbuilder/elements/caption.rb +34 -0
  14. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +3 -2
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +5 -3
  16. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +7 -4
  17. data/lib/govuk_design_system_formbuilder/elements/check_boxes/label.rb +8 -2
  18. data/lib/govuk_design_system_formbuilder/elements/date.rb +15 -11
  19. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +4 -2
  20. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +4 -4
  21. data/lib/govuk_design_system_formbuilder/elements/file.rb +6 -3
  22. data/lib/govuk_design_system_formbuilder/elements/hint.rb +5 -3
  23. data/lib/govuk_design_system_formbuilder/elements/inputs/email.rb +2 -0
  24. data/lib/govuk_design_system_formbuilder/elements/inputs/number.rb +2 -0
  25. data/lib/govuk_design_system_formbuilder/elements/inputs/password.rb +2 -0
  26. data/lib/govuk_design_system_formbuilder/elements/inputs/phone.rb +2 -0
  27. data/lib/govuk_design_system_formbuilder/elements/inputs/text.rb +2 -0
  28. data/lib/govuk_design_system_formbuilder/elements/inputs/url.rb +2 -0
  29. data/lib/govuk_design_system_formbuilder/elements/label.rb +30 -20
  30. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +3 -2
  31. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +4 -2
  32. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +7 -4
  33. data/lib/govuk_design_system_formbuilder/elements/select.rb +6 -3
  34. data/lib/govuk_design_system_formbuilder/elements/submit.rb +10 -7
  35. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +12 -5
  36. data/lib/govuk_design_system_formbuilder/traits/caption.rb +24 -0
  37. data/lib/govuk_design_system_formbuilder/traits/input.rb +16 -16
  38. data/lib/govuk_design_system_formbuilder/traits/label.rb +12 -1
  39. data/lib/govuk_design_system_formbuilder/traits/localisation.rb +18 -10
  40. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  41. metadata +16 -43
  42. data/lib/govuk_design_system_formbuilder/elements/check_boxes/hint.rb +0 -32
@@ -1,6 +1,8 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class ErrorMessage < Base
4
+ using PrefixableArray
5
+
4
6
  include Traits::Error
5
7
 
6
8
  def initialize(builder, object_name, attribute_name)
@@ -10,10 +12,10 @@ module GOVUKDesignSystemFormBuilder
10
12
  def html
11
13
  return nil unless has_errors?
12
14
 
13
- content_tag('span', class: 'govuk-error-message', id: error_id) do
15
+ content_tag('span', class: %(#{brand}-error-message), id: error_id) do
14
16
  safe_join(
15
17
  [
16
- tag.span('Error: ', class: 'govuk-visually-hidden'),
18
+ tag.span('Error: ', class: %(#{brand}-visually-hidden)),
17
19
  message
18
20
  ]
19
21
  )
@@ -17,7 +17,7 @@ module GOVUKDesignSystemFormBuilder
17
17
  [
18
18
  tag.h2(@title, id: error_summary_title_id, class: summary_class('title')),
19
19
  content_tag('div', class: summary_class('body')) do
20
- content_tag('ul', class: ['govuk-list', summary_class('list')]) do
20
+ content_tag('ul', class: [%(#{brand}-list), summary_class('list')]) do
21
21
  safe_join(
22
22
  @builder.object.errors.messages.map do |attribute, messages|
23
23
  error_list_item(attribute, messages.first)
@@ -50,9 +50,9 @@ module GOVUKDesignSystemFormBuilder
50
50
 
51
51
  def summary_class(part = nil)
52
52
  if part
53
- 'govuk-error-summary'.concat('__', part)
53
+ %(#{brand}-error-summary).concat('__', part)
54
54
  else
55
- 'govuk-error-summary'
55
+ %(#{brand}-error-summary)
56
56
  end
57
57
  end
58
58
 
@@ -73,7 +73,7 @@ module GOVUKDesignSystemFormBuilder
73
73
  tabindex: -1,
74
74
  role: 'alert',
75
75
  data: {
76
- module: 'govuk-error-summary'
76
+ module: %(#{brand}-error-summary)
77
77
  },
78
78
  aria: {
79
79
  labelledby: error_summary_title_id
@@ -1,15 +1,18 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class File < Base
4
+ using PrefixableArray
5
+
4
6
  include Traits::Error
5
7
  include Traits::Hint
6
8
  include Traits::Label
7
9
  include Traits::Supplemental
8
10
 
9
- def initialize(builder, object_name, attribute_name, hint_text:, label:, **extra_args, &block)
11
+ def initialize(builder, object_name, attribute_name, hint_text:, label:, caption:, **extra_args, &block)
10
12
  super(builder, object_name, attribute_name, &block)
11
13
 
12
14
  @label = label
15
+ @caption = caption
13
16
  @hint_text = hint_text
14
17
  @extra_args = extra_args
15
18
  end
@@ -37,8 +40,8 @@ module GOVUKDesignSystemFormBuilder
37
40
  private
38
41
 
39
42
  def file_classes
40
- %w(govuk-file-upload).tap do |c|
41
- c.push('govuk-file-upload--error') if has_errors?
43
+ %w(file-upload).prefix(brand).tap do |c|
44
+ c.push(%(#{brand}-file-upload--error)) if has_errors?
42
45
  end
43
46
  end
44
47
  end
@@ -1,6 +1,8 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class Hint < Base
4
+ using PrefixableArray
5
+
4
6
  include Traits::Hint
5
7
  include Traits::Localisation
6
8
 
@@ -29,15 +31,15 @@ module GOVUKDesignSystemFormBuilder
29
31
  end
30
32
 
31
33
  def hint_classes
32
- %w(govuk-hint).push(@radio_class, @checkbox_class).compact
34
+ %w(hint).prefix(brand).push(@radio_class, @checkbox_class).compact
33
35
  end
34
36
 
35
37
  def radio_class(radio)
36
- radio ? 'govuk-radios__hint' : nil
38
+ radio ? %(#{brand}-radios__hint) : nil
37
39
  end
38
40
 
39
41
  def checkbox_class(checkbox)
40
- checkbox ? 'govuk-checkboxes__hint' : nil
42
+ checkbox ? %(#{brand}-checkboxes__hint) : nil
41
43
  end
42
44
  end
43
45
  end
@@ -2,6 +2,8 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Email < Base
5
+ using PrefixableArray
6
+
5
7
  include Traits::Input
6
8
  include Traits::Error
7
9
  include Traits::Hint
@@ -2,6 +2,8 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Number < Base
5
+ using PrefixableArray
6
+
5
7
  include Traits::Input
6
8
  include Traits::Error
7
9
  include Traits::Hint
@@ -2,6 +2,8 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Password < Base
5
+ using PrefixableArray
6
+
5
7
  include Traits::Input
6
8
  include Traits::Error
7
9
  include Traits::Hint
@@ -2,6 +2,8 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Phone < Base
5
+ using PrefixableArray
6
+
5
7
  include Traits::Input
6
8
  include Traits::Error
7
9
  include Traits::Hint
@@ -2,6 +2,8 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Text < Base
5
+ using PrefixableArray
6
+
5
7
  include Traits::Input
6
8
  include Traits::Error
7
9
  include Traits::Hint
@@ -2,6 +2,8 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class URL < Base
5
+ using PrefixableArray
6
+
5
7
  include Traits::Input
6
8
  include Traits::Error
7
9
  include Traits::Hint
@@ -1,25 +1,35 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class Label < Base
4
+ using PrefixableArray
5
+
6
+ include Traits::Caption
4
7
  include Traits::Localisation
5
8
 
6
- def initialize(builder, object_name, attribute_name, text: nil, value: nil, size: nil, hidden: false, radio: false, checkbox: false, tag: nil, link_errors: true)
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)
7
10
  super(builder, object_name, attribute_name)
8
11
 
9
- @text = label_text(text, hidden)
10
- @value = value # used by field_id
11
- @size_class = label_size_class(size)
12
- @radio_class = radio_class(radio)
13
- @checkbox_class = checkbox_class(checkbox)
14
- @tag = tag
15
- @link_errors = link_errors
12
+ # content is passed in directly via a proc and overrides
13
+ # the other display options
14
+ if content
15
+ @content = content.call
16
+ else
17
+ @value = value # used by field_id
18
+ @text = label_text(text, hidden)
19
+ @size_class = label_size_class(size)
20
+ @radio_class = radio_class(radio)
21
+ @checkbox_class = checkbox_class(checkbox)
22
+ @tag = tag
23
+ @link_errors = link_errors
24
+ @caption = caption
25
+ end
16
26
  end
17
27
 
18
28
  def html
19
- return nil if @text.blank?
29
+ return nil if [@content, @text].all?(&:blank?)
20
30
 
21
31
  if @tag.present?
22
- content_tag(@tag, class: 'govuk-label-wrapper') { build_label }
32
+ content_tag(@tag, class: %(#{brand}-label-wrapper)) { build_label }
23
33
  else
24
34
  build_label
25
35
  end
@@ -32,36 +42,36 @@ module GOVUKDesignSystemFormBuilder
32
42
  @attribute_name,
33
43
  value: @value,
34
44
  for: field_id(link_errors: @link_errors),
35
- class: %w(govuk-label).push(@size_class, @weight_class, @radio_class, @checkbox_class).compact
45
+ class: %w(label).prefix(brand).push(@size_class, @weight_class, @radio_class, @checkbox_class).compact
36
46
  ) do
37
- @text
47
+ @content || safe_join([caption_element.html, @text])
38
48
  end
39
49
  end
40
50
 
41
51
  def label_text(option_text, hidden)
42
- text = [option_text, @value, localised_text(:label), @attribute_name.capitalize].compact.first.to_s
52
+ text = [option_text, localised_text(:label), @attribute_name.capitalize].compact.first.to_s
43
53
 
44
54
  if hidden
45
- tag.span(text, class: %w(govuk-visually-hidden))
55
+ tag.span(text, class: %w(visually-hidden).prefix(brand))
46
56
  else
47
57
  text
48
58
  end
49
59
  end
50
60
 
51
61
  def radio_class(radio)
52
- radio ? 'govuk-radios__label' : nil
62
+ radio ? %(#{brand}-radios__label) : nil
53
63
  end
54
64
 
55
65
  def checkbox_class(checkbox)
56
- checkbox ? 'govuk-checkboxes__label' : nil
66
+ checkbox ? %(#{brand}-checkboxes__label) : nil
57
67
  end
58
68
 
59
69
  def label_size_class(size)
60
70
  case size
61
- when 'xl' then "govuk-label--xl"
62
- when 'l' then "govuk-label--l"
63
- when 'm' then "govuk-label--m"
64
- when 's' then "govuk-label--s"
71
+ when 'xl' then %(#{brand}-label--xl)
72
+ when 'l' then %(#{brand}-label--l)
73
+ when 'm' then %(#{brand}-label--m)
74
+ when 's' then %(#{brand}-label--s)
65
75
  when nil then nil
66
76
  else
67
77
  fail "invalid size '#{size}', must be xl, l, m, s or nil"
@@ -6,7 +6,7 @@ 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:, inline:, small:, bold_labels:, classes:, &block)
9
+ def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint_method:, hint_text:, legend:, caption:, inline:, small:, bold_labels:, classes:, &block)
10
10
  super(builder, object_name, attribute_name, &block)
11
11
 
12
12
  @collection = collection
@@ -16,6 +16,7 @@ module GOVUKDesignSystemFormBuilder
16
16
  @inline = inline
17
17
  @small = small
18
18
  @legend = legend
19
+ @caption = caption
19
20
  @hint_text = hint_text
20
21
  @classes = classes
21
22
  @bold_labels = hint_method.present? || bold_labels
@@ -23,7 +24,7 @@ module GOVUKDesignSystemFormBuilder
23
24
 
24
25
  def html
25
26
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
26
- Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, described_by: [error_id, hint_id, supplemental_id]).html do
27
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, caption: @caption, described_by: [error_id, hint_id, supplemental_id]).html do
27
28
  safe_join(
28
29
  [
29
30
  supplemental_content.html,
@@ -2,6 +2,8 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Radios
4
4
  class CollectionRadioButton < Base
5
+ using PrefixableArray
6
+
5
7
  include Traits::Hint
6
8
  include Traits::CollectionItem
7
9
 
@@ -21,7 +23,7 @@ module GOVUKDesignSystemFormBuilder
21
23
  end
22
24
 
23
25
  def html
24
- content_tag('div', class: 'govuk-radios__item') do
26
+ content_tag('div', class: %(#{brand}-radios__item)) do
25
27
  safe_join(
26
28
  [
27
29
  @builder.radio_button(
@@ -29,7 +31,7 @@ module GOVUKDesignSystemFormBuilder
29
31
  @value,
30
32
  id: field_id(link_errors: @link_errors),
31
33
  aria: { describedby: hint_id },
32
- class: %w(govuk-radios__input)
34
+ class: %w(radios__input).prefix(brand)
33
35
  ),
34
36
  label_element.html,
35
37
  hint_element.html
@@ -2,6 +2,9 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Radios
4
4
  class FieldsetRadioButton < Base
5
+ using PrefixableArray
6
+
7
+ include Traits::Label
5
8
  include Traits::Hint
6
9
  include Traits::Conditional
7
10
 
@@ -22,7 +25,7 @@ module GOVUKDesignSystemFormBuilder
22
25
  def html
23
26
  safe_join(
24
27
  [
25
- content_tag('div', class: 'govuk-radios__item') do
28
+ content_tag('div', class: %(#{brand}-radios__item)) do
26
29
  safe_join(
27
30
  [
28
31
  input,
@@ -39,7 +42,7 @@ module GOVUKDesignSystemFormBuilder
39
42
  private
40
43
 
41
44
  def label_element
42
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, radio: true, value: @value, **@label, link_errors: @link_errors)
45
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, radio: true, value: @value, link_errors: @link_errors, **label_args)
43
46
  end
44
47
 
45
48
  def hint_element
@@ -53,12 +56,12 @@ module GOVUKDesignSystemFormBuilder
53
56
  id: field_id(link_errors: @link_errors),
54
57
  aria: { describedby: hint_id },
55
58
  data: { 'aria-controls' => @conditional_id },
56
- class: %w(govuk-radios__input)
59
+ class: %w(radios__input).prefix(brand)
57
60
  )
58
61
  end
59
62
 
60
63
  def conditional_classes
61
- %w(govuk-radios__conditional govuk-radios__conditional--hidden)
64
+ %w(radios__conditional radios__conditional--hidden).prefix(brand)
62
65
  end
63
66
  end
64
67
  end
@@ -1,12 +1,14 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class Select < Base
4
+ using PrefixableArray
5
+
4
6
  include Traits::Error
5
7
  include Traits::Label
6
8
  include Traits::Hint
7
9
  include Traits::Supplemental
8
10
 
9
- def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, options: {}, html_options: {}, hint_text:, label:, &block)
11
+ def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, options: {}, html_options: {}, hint_text:, label:, caption:, &block)
10
12
  super(builder, object_name, attribute_name, &block)
11
13
 
12
14
  @collection = collection
@@ -15,6 +17,7 @@ module GOVUKDesignSystemFormBuilder
15
17
  @options = options
16
18
  @html_options = html_options
17
19
  @label = label
20
+ @caption = caption
18
21
  @hint_text = hint_text
19
22
  end
20
23
 
@@ -50,8 +53,8 @@ module GOVUKDesignSystemFormBuilder
50
53
  end
51
54
 
52
55
  def select_classes
53
- %w(govuk-select).tap do |classes|
54
- classes.push('govuk-select--error') if has_errors?
56
+ %w(select).prefix(brand).tap do |classes|
57
+ classes.push(%(#{brand}-select--error)) if has_errors?
55
58
  end
56
59
  end
57
60
  end
@@ -1,7 +1,9 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class Submit < Base
4
- def initialize(builder, text, warning:, secondary:, prevent_double_click:, validate:, &block)
4
+ using PrefixableArray
5
+
6
+ def initialize(builder, text, warning:, secondary:, classes:, prevent_double_click:, validate:, &block)
5
7
  fail ArgumentError, 'buttons can be warning or secondary' if warning && secondary
6
8
 
7
9
  @builder = builder
@@ -9,6 +11,7 @@ module GOVUKDesignSystemFormBuilder
9
11
  @prevent_double_click = prevent_double_click
10
12
  @warning = warning
11
13
  @secondary = secondary
14
+ @classes = classes
12
15
  @validate = validate
13
16
  @block_content = capture { block.call } if block_given?
14
17
  end
@@ -18,9 +21,10 @@ module GOVUKDesignSystemFormBuilder
18
21
  [
19
22
  @builder.submit(
20
23
  @text,
21
- class: %w(govuk-button).push(
24
+ class: %w(button).prefix(brand).push(
22
25
  warning_class,
23
26
  secondary_class,
27
+ @classes,
24
28
  padding_class(@block_content.present?)
25
29
  ).compact,
26
30
  **extra_args
@@ -33,23 +37,22 @@ module GOVUKDesignSystemFormBuilder
33
37
  private
34
38
 
35
39
  def warning_class
36
- 'govuk-button--warning' if @warning
40
+ %(#{brand}-button--warning) if @warning
37
41
  end
38
42
 
39
43
  def secondary_class
40
- 'govuk-button--secondary' if @secondary
44
+ %(#{brand}-button--secondary) if @secondary
41
45
  end
42
46
 
43
47
  def padding_class(content_present)
44
- 'govuk-!-margin-right-1' if content_present
48
+ %(#{brand}-!-margin-right-1) if content_present
45
49
  end
46
50
 
47
51
  def extra_args
48
52
  {
49
53
  formnovalidate: !@validate,
50
54
  data: {
51
- module: 'govuk-button',
52
- 'prevent-double-click' => @prevent_double_click
55
+ module: %(#{brand}-button), 'prevent-double-click' => @prevent_double_click
53
56
  }.select { |_k, v| v.present? }
54
57
  }
55
58
  end