govuk_design_system_formbuilder 1.1.11 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) 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 +17 -0
  5. data/lib/govuk_design_system_formbuilder/builder.rb +205 -77
  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 +12 -11
  9. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +37 -20
  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 +12 -11
  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 +11 -13
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +20 -16
  16. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +24 -29
  17. data/lib/govuk_design_system_formbuilder/elements/check_boxes/label.rb +3 -1
  18. data/lib/govuk_design_system_formbuilder/elements/date.rb +31 -31
  19. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +11 -8
  20. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +26 -29
  21. data/lib/govuk_design_system_formbuilder/elements/file.rb +22 -21
  22. data/lib/govuk_design_system_formbuilder/elements/hint.rb +6 -7
  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 +39 -26
  30. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +26 -24
  31. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +16 -14
  32. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +19 -21
  33. data/lib/govuk_design_system_formbuilder/elements/select.rb +12 -20
  34. data/lib/govuk_design_system_formbuilder/elements/submit.rb +29 -27
  35. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +27 -27
  36. data/lib/govuk_design_system_formbuilder/traits/caption.rb +23 -0
  37. data/lib/govuk_design_system_formbuilder/traits/input.rb +33 -41
  38. data/lib/govuk_design_system_formbuilder/traits/label.rb +12 -1
  39. data/lib/govuk_design_system_formbuilder/traits/localisation.rb +2 -0
  40. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  41. metadata +19 -17
@@ -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,38 +24,39 @@ 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
- safe_join(
28
- [
29
- supplemental_content.html,
30
- hint_element.html,
31
- error_element.html,
32
- Containers::Radios.new(@builder, inline: @inline, small: @small, classes: @classes).html do
33
- safe_join(build_collection)
34
- end
35
- ]
36
- )
27
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, caption: @caption, described_by: [error_id, hint_id, supplemental_id]).html do
28
+ safe_join([supplemental_content, hint_element, error_element, radios])
37
29
  end
38
30
  end
39
31
  end
40
32
 
41
33
  private
42
34
 
43
- def build_collection
35
+ def radios
36
+ Containers::Radios.new(@builder, inline: @inline, small: @small, classes: @classes).html do
37
+ safe_join(collection)
38
+ end
39
+ end
40
+
41
+ def collection
44
42
  @collection.map.with_index do |item, i|
45
- Elements::Radios::CollectionRadioButton.new(
46
- @builder,
47
- @object_name,
48
- @attribute_name,
49
- item,
50
- value_method: @value_method,
51
- text_method: @text_method,
52
- hint_method: @hint_method,
53
- link_errors: has_errors? && i.zero?,
54
- bold_labels: @bold_labels
55
- ).html
43
+ Elements::Radios::CollectionRadioButton.new(@builder, @object_name, @attribute_name, item, **collection_options(i)).html
56
44
  end
57
45
  end
46
+
47
+ def collection_options(index)
48
+ {
49
+ value_method: @value_method,
50
+ text_method: @text_method,
51
+ hint_method: @hint_method,
52
+ link_errors: link_errors?(index),
53
+ bold_labels: @bold_labels
54
+ }
55
+ end
56
+
57
+ def link_errors?(index)
58
+ has_errors? && index.zero?
59
+ end
58
60
  end
59
61
  end
60
62
  end
@@ -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,25 +23,25 @@ module GOVUKDesignSystemFormBuilder
21
23
  end
22
24
 
23
25
  def html
24
- content_tag('div', class: 'govuk-radios__item') do
25
- safe_join(
26
- [
27
- @builder.radio_button(
28
- @attribute_name,
29
- @value,
30
- id: field_id(link_errors: @link_errors),
31
- aria: { describedby: hint_id },
32
- class: %w(govuk-radios__input)
33
- ),
34
- label_element.html,
35
- hint_element.html
36
- ]
37
- )
26
+ content_tag('div', class: %(#{brand}-radios__item)) do
27
+ safe_join([radio, label_element, hint_element])
38
28
  end
39
29
  end
40
30
 
41
31
  private
42
32
 
33
+ def radio
34
+ @builder.radio_button(@attribute_name, @value, **radio_options)
35
+ end
36
+
37
+ def radio_options
38
+ {
39
+ id: field_id(link_errors: @link_errors),
40
+ aria: { describedby: hint_id },
41
+ class: %w(radios__input).prefix(brand)
42
+ }
43
+ end
44
+
43
45
  def hint_element
44
46
  @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, radio: true)
45
47
  end
@@ -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
 
@@ -20,26 +23,19 @@ module GOVUKDesignSystemFormBuilder
20
23
  end
21
24
 
22
25
  def html
23
- safe_join(
24
- [
25
- content_tag('div', class: 'govuk-radios__item') do
26
- safe_join(
27
- [
28
- input,
29
- label_element.html,
30
- hint_element.html,
31
- ]
32
- )
33
- end,
34
- @conditional_content
35
- ]
36
- )
26
+ safe_join([radio, @conditional_content])
37
27
  end
38
28
 
39
29
  private
40
30
 
31
+ def radio
32
+ content_tag('div', class: %(#{brand}-radios__item)) do
33
+ safe_join([input, label_element, hint_element])
34
+ end
35
+ end
36
+
41
37
  def label_element
42
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, radio: true, value: @value, **@label, link_errors: @link_errors)
38
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, radio: true, value: @value, link_errors: @link_errors, **label_options)
43
39
  end
44
40
 
45
41
  def hint_element
@@ -47,18 +43,20 @@ module GOVUKDesignSystemFormBuilder
47
43
  end
48
44
 
49
45
  def input
50
- @builder.radio_button(
51
- @attribute_name,
52
- @value,
46
+ @builder.radio_button(@attribute_name, @value, input_options)
47
+ end
48
+
49
+ def input_options
50
+ {
53
51
  id: field_id(link_errors: @link_errors),
54
52
  aria: { describedby: hint_id },
55
53
  data: { 'aria-controls' => @conditional_id },
56
- class: %w(govuk-radios__input)
57
- )
54
+ class: %w(radios__input).prefix(brand)
55
+ }
58
56
  end
59
57
 
60
58
  def conditional_classes
61
- %w(govuk-radios__conditional govuk-radios__conditional--hidden)
59
+ %w(radios__conditional radios__conditional--hidden).prefix(brand)
62
60
  end
63
61
  end
64
62
  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,33 +17,23 @@ 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
 
21
24
  def html
22
25
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
23
- safe_join(
24
- [
25
- label_element.html,
26
- supplemental_content.html,
27
- hint_element.html,
28
- error_element.html,
29
- @builder.collection_select(
30
- @attribute_name,
31
- @collection,
32
- @value_method,
33
- @text_method,
34
- @options,
35
- build_html_options
36
- )
37
- ]
38
- )
26
+ safe_join([label_element, supplemental_content, hint_element, error_element, select])
39
27
  end
40
28
  end
41
29
 
42
30
  private
43
31
 
44
- def build_html_options
32
+ def select
33
+ @builder.collection_select(@attribute_name, @collection, @value_method, @text_method, @options, select_options)
34
+ end
35
+
36
+ def select_options
45
37
  @html_options.deep_merge(
46
38
  id: field_id(link_errors: true),
47
39
  class: select_classes,
@@ -50,8 +42,8 @@ module GOVUKDesignSystemFormBuilder
50
42
  end
51
43
 
52
44
  def select_classes
53
- %w(govuk-select).tap do |classes|
54
- classes.push('govuk-select--error') if has_errors?
45
+ %w(select).prefix(brand).tap do |classes|
46
+ classes.push(%(#{brand}-select--error)) if has_errors?
55
47
  end
56
48
  end
57
49
  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:, classes:, prevent_double_click:, validate:, &block)
4
+ using PrefixableArray
5
+
6
+ def initialize(builder, text, warning:, secondary:, classes:, prevent_double_click:, validate:, disabled:, &block)
5
7
  fail ArgumentError, 'buttons can be warning or secondary' if warning && secondary
6
8
 
7
9
  @builder = builder
@@ -11,50 +13,50 @@ module GOVUKDesignSystemFormBuilder
11
13
  @secondary = secondary
12
14
  @classes = classes
13
15
  @validate = validate
16
+ @disabled = disabled
14
17
  @block_content = capture { block.call } if block_given?
15
18
  end
16
19
 
17
20
  def html
18
- safe_join(
19
- [
20
- @builder.submit(
21
- @text,
22
- class: %w(govuk-button).push(
23
- warning_class,
24
- secondary_class,
25
- @classes,
26
- padding_class(@block_content.present?)
27
- ).compact,
28
- **extra_args
29
- ),
30
- @block_content
31
- ]
32
- )
21
+ safe_join([submit, @block_content])
33
22
  end
34
23
 
35
24
  private
36
25
 
37
- def warning_class
38
- 'govuk-button--warning' if @warning
26
+ def submit
27
+ @builder.submit(@text, class: submit_classes, **submit_options)
39
28
  end
40
29
 
41
- def secondary_class
42
- 'govuk-button--secondary' if @secondary
30
+ def submit_classes
31
+ %w(button).prefix(brand).push(warning_class, secondary_class, disabled_class, @classes, padding_class(@block_content.present?))
43
32
  end
44
33
 
45
- def padding_class(content_present)
46
- 'govuk-!-margin-right-1' if content_present
47
- end
48
-
49
- def extra_args
34
+ def submit_options
50
35
  {
51
36
  formnovalidate: !@validate,
37
+ disabled: @disabled,
52
38
  data: {
53
- module: 'govuk-button',
54
- 'prevent-double-click' => @prevent_double_click
39
+ module: %(#{brand}-button),
40
+ 'prevent-double-click': @prevent_double_click
55
41
  }.select { |_k, v| v.present? }
56
42
  }
57
43
  end
44
+
45
+ def warning_class
46
+ %(#{brand}-button--warning) if @warning
47
+ end
48
+
49
+ def secondary_class
50
+ %(#{brand}-button--secondary) if @secondary
51
+ end
52
+
53
+ def padding_class(content_present)
54
+ %(#{brand}-!-margin-right-1) if content_present
55
+ end
56
+
57
+ def disabled_class
58
+ %(#{brand}-button--disabled) if @disabled
59
+ end
58
60
  end
59
61
  end
60
62
  end
@@ -1,17 +1,20 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class TextArea < 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:, rows:, max_words:, max_chars:, threshold:, **extra_args, &block)
11
+ def initialize(builder, object_name, attribute_name, hint_text:, label:, caption:, rows:, max_words:, max_chars:, threshold:, **kwargs, &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
- @extra_args = extra_args
17
+ @attributes = kwargs
15
18
  @max_words = max_words
16
19
  @max_chars = max_chars
17
20
  @threshold = threshold
@@ -21,39 +24,32 @@ module GOVUKDesignSystemFormBuilder
21
24
  def html
22
25
  Containers::CharacterCount.new(@builder, max_words: @max_words, max_chars: @max_chars, threshold: @threshold).html do
23
26
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
24
- safe_join(
25
- [
26
- [
27
- label_element,
28
- supplemental_content,
29
- hint_element,
30
- error_element
31
- ].map(&:html),
32
- @builder.text_area(
33
- @attribute_name,
34
- id: field_id(link_errors: true),
35
- class: govuk_textarea_classes,
36
- aria: {
37
- describedby: described_by(hint_id, error_id, supplemental_id, limit_description_id)
38
- },
39
- **@extra_args.merge(rows: @rows)
40
- ),
41
- limit_description
42
- ].flatten.compact
43
- )
27
+ safe_join([label_element, supplemental_content, hint_element, error_element, text_area, limit_description])
44
28
  end
45
29
  end
46
30
  end
47
31
 
48
32
  private
49
33
 
50
- def govuk_textarea_classes
51
- %w(govuk-textarea).tap do |classes|
52
- classes.push('govuk-textarea--error') if has_errors?
53
- classes.push('govuk-js-character-count') if limit?
34
+ def text_area
35
+ @builder.text_area(@attribute_name, **text_area_options, **@attributes.merge(rows: @rows))
36
+ end
37
+
38
+ def text_area_classes
39
+ %w(textarea).prefix(brand).tap do |classes|
40
+ classes.push(%(#{brand}-textarea--error)) if has_errors?
41
+ classes.push(%(#{brand}-js-character-count)) if limit?
54
42
  end
55
43
  end
56
44
 
45
+ def text_area_options
46
+ {
47
+ id: field_id(link_errors: true),
48
+ class: text_area_classes,
49
+ aria: { describedby: described_by(hint_id, error_id, supplemental_id, limit_description_id) },
50
+ }
51
+ end
52
+
57
53
  # Provides an id for use by the textual description of character and word limits.
58
54
  #
59
55
  # @note In order for the GOV.UK Frontend JavaScript to pick up this associated field
@@ -69,11 +65,15 @@ module GOVUKDesignSystemFormBuilder
69
65
  def limit_description
70
66
  return nil unless limit?
71
67
 
72
- content_tag('span', id: limit_id, class: %w(govuk-hint govuk-character-count__message), aria: { live: 'polite' }) do
68
+ content_tag('span', id: limit_id, class: limit_description_classes, aria: { live: 'polite' }) do
73
69
  "You can enter up to #{limit_quantity} #{limit_type}"
74
70
  end
75
71
  end
76
72
 
73
+ def limit_description_classes
74
+ %w(hint character-count__message).prefix(brand)
75
+ end
76
+
77
77
  def limit_quantity
78
78
  @max_words || @max_chars
79
79
  end