govuk_design_system_formbuilder 1.2.0b4 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/lib/govuk_design_system_formbuilder.rb +4 -0
  3. data/lib/govuk_design_system_formbuilder/base.rb +5 -0
  4. data/lib/govuk_design_system_formbuilder/builder.rb +6 -4
  5. data/lib/govuk_design_system_formbuilder/containers/character_count.rb +8 -7
  6. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +8 -1
  7. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +18 -10
  8. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +23 -13
  9. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +18 -10
  10. data/lib/govuk_design_system_formbuilder/containers/radios.rb +8 -1
  11. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +1 -3
  12. data/lib/govuk_design_system_formbuilder/elements/caption.rb +2 -2
  13. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +17 -12
  14. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +17 -15
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +23 -27
  16. data/lib/govuk_design_system_formbuilder/elements/date.rb +44 -41
  17. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +8 -7
  18. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +23 -26
  19. data/lib/govuk_design_system_formbuilder/elements/file.rb +18 -20
  20. data/lib/govuk_design_system_formbuilder/elements/hint.rb +10 -13
  21. data/lib/govuk_design_system_formbuilder/elements/label.rb +33 -22
  22. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +32 -23
  23. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +24 -14
  24. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +18 -19
  25. data/lib/govuk_design_system_formbuilder/elements/select.rb +15 -22
  26. data/lib/govuk_design_system_formbuilder/elements/submit.rb +29 -25
  27. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +37 -39
  28. data/lib/govuk_design_system_formbuilder/traits/caption.rb +5 -6
  29. data/lib/govuk_design_system_formbuilder/traits/input.rb +19 -28
  30. data/lib/govuk_design_system_formbuilder/traits/label.rb +2 -2
  31. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  32. metadata +17 -17
@@ -24,30 +24,40 @@ module GOVUKDesignSystemFormBuilder
24
24
 
25
25
  def html
26
26
  content_tag('div', class: %(#{brand}-radios__item)) do
27
- safe_join(
28
- [
29
- @builder.radio_button(
30
- @attribute_name,
31
- @value,
32
- id: field_id(link_errors: @link_errors),
33
- aria: { describedby: hint_id },
34
- class: %w(radios__input).prefix(brand)
35
- ),
36
- label_element.html,
37
- hint_element.html
38
- ]
39
- )
27
+ safe_join([radio, label_element, hint_element])
40
28
  end
41
29
  end
42
30
 
43
31
  private
44
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
+
45
45
  def hint_element
46
46
  @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, radio: true)
47
47
  end
48
48
 
49
49
  def label_element
50
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, text: @label_text, value: @value, radio: true, size: label_size, link_errors: @link_errors)
50
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_options)
51
+ end
52
+
53
+ def label_options
54
+ {
55
+ text: @label_text,
56
+ value: @value,
57
+ radio: true,
58
+ size: label_size,
59
+ link_errors: @link_errors
60
+ }
51
61
  end
52
62
 
53
63
  def label_size
@@ -23,26 +23,23 @@ module GOVUKDesignSystemFormBuilder
23
23
  end
24
24
 
25
25
  def html
26
- safe_join(
27
- [
28
- content_tag('div', class: %(#{brand}-radios__item)) do
29
- safe_join(
30
- [
31
- input,
32
- label_element.html,
33
- hint_element.html,
34
- ]
35
- )
36
- end,
37
- @conditional_content
38
- ]
39
- )
26
+ safe_join([radio, @conditional_content])
40
27
  end
41
28
 
42
29
  private
43
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
+
44
37
  def label_element
45
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, radio: true, value: @value, link_errors: @link_errors, **label_args)
38
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_content, **label_options)
39
+ end
40
+
41
+ def label_options
42
+ { radio: true, value: @value, link_errors: @link_errors }
46
43
  end
47
44
 
48
45
  def hint_element
@@ -50,14 +47,16 @@ module GOVUKDesignSystemFormBuilder
50
47
  end
51
48
 
52
49
  def input
53
- @builder.radio_button(
54
- @attribute_name,
55
- @value,
50
+ @builder.radio_button(@attribute_name, @value, **input_options)
51
+ end
52
+
53
+ def input_options
54
+ {
56
55
  id: field_id(link_errors: @link_errors),
57
56
  aria: { describedby: hint_id },
58
57
  data: { 'aria-controls' => @conditional_id },
59
58
  class: %w(radios__input).prefix(brand)
60
- )
59
+ }
61
60
  end
62
61
 
63
62
  def conditional_classes
@@ -1,8 +1,6 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class Select < Base
4
- using PrefixableArray
5
-
6
4
  include Traits::Error
7
5
  include Traits::Label
8
6
  include Traits::Hint
@@ -23,28 +21,17 @@ module GOVUKDesignSystemFormBuilder
23
21
 
24
22
  def html
25
23
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
26
- safe_join(
27
- [
28
- label_element.html,
29
- supplemental_content.html,
30
- hint_element.html,
31
- error_element.html,
32
- @builder.collection_select(
33
- @attribute_name,
34
- @collection,
35
- @value_method,
36
- @text_method,
37
- @options,
38
- build_html_options
39
- )
40
- ]
41
- )
24
+ safe_join([label_element, supplemental_content, hint_element, error_element, select])
42
25
  end
43
26
  end
44
27
 
45
28
  private
46
29
 
47
- def build_html_options
30
+ def select
31
+ @builder.collection_select(@attribute_name, @collection, @value_method, @text_method, @options, **select_options)
32
+ end
33
+
34
+ def select_options
48
35
  @html_options.deep_merge(
49
36
  id: field_id(link_errors: true),
50
37
  class: select_classes,
@@ -53,9 +40,15 @@ module GOVUKDesignSystemFormBuilder
53
40
  end
54
41
 
55
42
  def select_classes
56
- %w(select).prefix(brand).tap do |classes|
57
- classes.push(%(#{brand}-select--error)) if has_errors?
58
- end
43
+ [%(#{brand}-select), select_error_class, select_custom_classes].flatten.compact
44
+ end
45
+
46
+ def select_error_class
47
+ %(#{brand}-select--error) if has_errors?
48
+ end
49
+
50
+ def select_custom_classes
51
+ @html_options.dig(:class)
59
52
  end
60
53
  end
61
54
  end
@@ -3,7 +3,7 @@ module GOVUKDesignSystemFormBuilder
3
3
  class Submit < Base
4
4
  using PrefixableArray
5
5
 
6
- def initialize(builder, text, warning:, secondary:, classes:, prevent_double_click:, validate:, &block)
6
+ def initialize(builder, text, warning:, secondary:, classes:, prevent_double_click:, validate:, disabled:, &block)
7
7
  fail ArgumentError, 'buttons can be warning or secondary' if warning && secondary
8
8
 
9
9
  @builder = builder
@@ -13,29 +13,38 @@ module GOVUKDesignSystemFormBuilder
13
13
  @secondary = secondary
14
14
  @classes = classes
15
15
  @validate = validate
16
+ @disabled = disabled
16
17
  @block_content = capture { block.call } if block_given?
17
18
  end
18
19
 
19
20
  def html
20
- safe_join(
21
- [
22
- @builder.submit(
23
- @text,
24
- class: %w(button).prefix(brand).push(
25
- warning_class,
26
- secondary_class,
27
- @classes,
28
- padding_class(@block_content.present?)
29
- ).compact,
30
- **extra_args
31
- ),
32
- @block_content
33
- ]
34
- )
21
+ safe_join([submit, @block_content])
35
22
  end
36
23
 
37
24
  private
38
25
 
26
+ def submit
27
+ @builder.submit(@text, class: submit_classes, **submit_options)
28
+ end
29
+
30
+ def submit_classes
31
+ %w(button)
32
+ .prefix(brand)
33
+ .push(warning_class, secondary_class, disabled_class, padding_class, @classes)
34
+ .compact
35
+ end
36
+
37
+ def submit_options
38
+ {
39
+ formnovalidate: !@validate,
40
+ disabled: @disabled,
41
+ data: {
42
+ module: %(#{brand}-button),
43
+ 'prevent-double-click': @prevent_double_click
44
+ }.select { |_k, v| v.present? }
45
+ }
46
+ end
47
+
39
48
  def warning_class
40
49
  %(#{brand}-button--warning) if @warning
41
50
  end
@@ -44,17 +53,12 @@ module GOVUKDesignSystemFormBuilder
44
53
  %(#{brand}-button--secondary) if @secondary
45
54
  end
46
55
 
47
- def padding_class(content_present)
48
- %(#{brand}-!-margin-right-1) if content_present
56
+ def padding_class
57
+ %(#{brand}-!-margin-right-1) if @block_content
49
58
  end
50
59
 
51
- def extra_args
52
- {
53
- formnovalidate: !@validate,
54
- data: {
55
- module: %(#{brand}-button), 'prevent-double-click' => @prevent_double_click
56
- }.select { |_k, v| v.present? }
57
- }
60
+ def disabled_class
61
+ %(#{brand}-button--disabled) if @disabled
58
62
  end
59
63
  end
60
64
  end
@@ -8,55 +8,53 @@ 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:, 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)
12
12
  super(builder, object_name, attribute_name, &block)
13
13
 
14
- @label = label
15
- @caption = caption
16
- @hint_text = hint_text
17
- @extra_args = extra_args
18
- @max_words = max_words
19
- @max_chars = max_chars
20
- @threshold = threshold
21
- @rows = rows
14
+ @label = label
15
+ @caption = caption
16
+ @hint_text = hint_text
17
+ @max_words = max_words
18
+ @max_chars = max_chars
19
+ @threshold = threshold
20
+ @rows = rows
21
+ @html_attributes = kwargs
22
22
  end
23
23
 
24
24
  def html
25
- Containers::CharacterCount.new(@builder, max_words: @max_words, max_chars: @max_chars, threshold: @threshold).html do
25
+ Containers::CharacterCount.new(@builder, **character_count_options).html do
26
26
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
27
- safe_join(
28
- [
29
- [
30
- label_element,
31
- supplemental_content,
32
- hint_element,
33
- error_element
34
- ].map(&:html),
35
- @builder.text_area(
36
- @attribute_name,
37
- id: field_id(link_errors: true),
38
- class: govuk_textarea_classes,
39
- aria: {
40
- describedby: described_by(hint_id, error_id, supplemental_id, limit_description_id)
41
- },
42
- **@extra_args.merge(rows: @rows)
43
- ),
44
- limit_description
45
- ].flatten.compact
46
- )
27
+ safe_join([label_element, supplemental_content, hint_element, error_element, text_area, limit_description])
47
28
  end
48
29
  end
49
30
  end
50
31
 
51
32
  private
52
33
 
53
- def govuk_textarea_classes
34
+ def character_count_options
35
+ { max_words: @max_words, max_chars: @max_chars, threshold: @threshold }
36
+ end
37
+
38
+ def text_area
39
+ @builder.text_area(@attribute_name, **text_area_options, **@html_attributes)
40
+ end
41
+
42
+ def text_area_classes
54
43
  %w(textarea).prefix(brand).tap do |classes|
55
44
  classes.push(%(#{brand}-textarea--error)) if has_errors?
56
45
  classes.push(%(#{brand}-js-character-count)) if limit?
57
46
  end
58
47
  end
59
48
 
49
+ def text_area_options
50
+ {
51
+ id: field_id(link_errors: true),
52
+ class: text_area_classes,
53
+ rows: @rows,
54
+ aria: { describedby: described_by(hint_id, error_id, supplemental_id, limit_description_id) },
55
+ }
56
+ end
57
+
60
58
  # Provides an id for use by the textual description of character and word limits.
61
59
  #
62
60
  # @note In order for the GOV.UK Frontend JavaScript to pick up this associated field
@@ -66,9 +64,17 @@ module GOVUKDesignSystemFormBuilder
66
64
  end
67
65
 
68
66
  def limit?
67
+ limit_quantity.present?
68
+ end
69
+
70
+ def limit_quantity
69
71
  @max_words || @max_chars
70
72
  end
71
73
 
74
+ def limit_type
75
+ @max_words.present? ? 'words' : 'characters'
76
+ end
77
+
72
78
  def limit_description
73
79
  return nil unless limit?
74
80
 
@@ -81,14 +87,6 @@ module GOVUKDesignSystemFormBuilder
81
87
  %w(hint character-count__message).prefix(brand)
82
88
  end
83
89
 
84
- def limit_quantity
85
- @max_words || @max_chars
86
- end
87
-
88
- def limit_type
89
- @max_words.present? ? 'words' : 'characters'
90
- end
91
-
92
90
  def limit_description_id
93
91
  return nil unless limit?
94
92
 
@@ -4,12 +4,11 @@ module GOVUKDesignSystemFormBuilder
4
4
  private
5
5
 
6
6
  def caption_element
7
- @caption_element ||= Elements::Caption.new(
8
- @builder,
9
- @object_name,
10
- @attribute_name,
11
- **{ text: nil }.merge({ text: caption_text, size: caption_size }.compact)
12
- )
7
+ @caption_element ||= Elements::Caption.new(@builder, @object_name, @attribute_name, **caption_options)
8
+ end
9
+
10
+ def caption_options
11
+ { text: nil }.merge({ text: caption_text, size: caption_size }.compact)
13
12
  end
14
13
 
15
14
  def caption_text
@@ -1,45 +1,36 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Traits
3
3
  module Input
4
- def initialize(builder, object_name, attribute_name, hint_text:, label:, caption:, width:, **extra_args, &block)
4
+ def initialize(builder, object_name, attribute_name, hint_text:, label:, caption:, width:, **kwargs, &block)
5
5
  super(builder, object_name, attribute_name, &block)
6
6
 
7
- @width = width
8
- @extra_args = extra_args
9
- @label = label
10
- @caption = caption
11
- @hint_text = hint_text
7
+ @width = width
8
+ @label = label
9
+ @caption = caption
10
+ @hint_text = hint_text
11
+ @html_attributes = kwargs
12
12
  end
13
13
 
14
14
  def html
15
15
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
16
- safe_join(
17
- [
18
- label_element.html,
19
- supplemental_content.html,
20
- hint_element.html,
21
- error_element.html,
22
- @builder.send(
23
- builder_method,
24
- @attribute_name,
25
- id: field_id(link_errors: true),
26
- class: input_classes,
27
- aria: {
28
- describedby: described_by(
29
- hint_id,
30
- error_id,
31
- supplemental_id
32
- )
33
- },
34
- **@extra_args
35
- )
36
- ]
37
- )
16
+ safe_join([label_element, supplemental_content, hint_element, error_element, input])
38
17
  end
39
18
  end
40
19
 
41
20
  private
42
21
 
22
+ def input
23
+ @builder.send(builder_method, @attribute_name, **input_options, **@html_attributes)
24
+ end
25
+
26
+ def input_options
27
+ {
28
+ id: field_id(link_errors: true),
29
+ class: input_classes,
30
+ aria: { describedby: described_by(hint_id, error_id, supplemental_id) }
31
+ }
32
+ end
33
+
43
34
  def input_classes
44
35
  [%(#{brand}-input)].push(width_classes, error_classes).compact
45
36
  end