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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33e103d8be5763415d474e06b5ec369e41faa78736769dfdc361da38cfdc0b35
4
- data.tar.gz: 46bb0c7084d3bc259715dc7e3ea94ffc53f3cc2347ad922657ee6f5182f404b5
3
+ metadata.gz: 5d0ec456d05c0f92a86c39b688f0ac2a498160f473002f2fb4004567f4b17496
4
+ data.tar.gz: 0aa054ef3feb33eaf5b402aaea08ab2942e9b9be6708f04de41c1255e455dc86
5
5
  SHA512:
6
- metadata.gz: e6ad6b1bc1d47c5c5510e112a480fff9680d1dc8e64605524a2e63b67a72d1881227614970e61653d37782bff41f7ae1ca052ef67fbb3653ace5e24550e1afdd
7
- data.tar.gz: 92f7169ebf567010c3114946b50677869ef5616e8f96c3542ea11739ea63e3516a1dba70d6fb8b2bfcffeaa2c86a8e5f039f51a0db2e8ac312e2d1185f215d82
6
+ metadata.gz: 8a79e3ac6187857d917f7eb253abfbb414b8ed44a3a4488a29f82052ca234daeba9f1926a0df12697544fa266420ebe8eba8ab26840fe8d371b713d82c2df312
7
+ data.tar.gz: 1334ddb5eb6a36dfbcd5b8f91b50548ecfac38481682e683aec5071d2f91b7577f3cadd532c8dd8c1b1c5dba34fa837479ef55d5f63669b15756159e5b9fb5aa
@@ -14,6 +14,9 @@ module GOVUKDesignSystemFormBuilder
14
14
  # * +:brand+ sets the value used to prefix all classes, used to allow the
15
15
  # builder to be branded for alternative (similar) design systems.
16
16
  #
17
+ # * +:default_caption_size+ controls the default size of caption text.
18
+ # Can be either +xl+, +l+ or +m+.
19
+ #
17
20
  # * +:default_legend_size+ controls the default size of legend text.
18
21
  # Can be either +xl+, +l+, +m+ or +s+.
19
22
  #
@@ -45,6 +48,7 @@ module GOVUKDesignSystemFormBuilder
45
48
 
46
49
  default_legend_size: 'm',
47
50
  default_legend_tag: 'h1',
51
+ default_caption_size: 'm',
48
52
  default_submit_button_text: 'Continue',
49
53
  default_radio_divider_text: 'or',
50
54
  default_error_summary_title: 'There is a problem',
@@ -18,6 +18,11 @@ module GOVUKDesignSystemFormBuilder
18
18
  @block_content = capture { block.call } if block_given?
19
19
  end
20
20
 
21
+ # objects that implement #to_s can be passed directly into #safe_join
22
+ def to_s
23
+ html || ''
24
+ end
25
+
21
26
  private
22
27
 
23
28
  def brand(override = nil)
@@ -312,7 +312,10 @@ module GOVUKDesignSystemFormBuilder
312
312
  # @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
313
313
  # @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
314
314
  # @option label hidden [Boolean] control the visability of the label. Hidden labels will stil be read by screenreaders
315
+ # @param options [Hash] Options hash passed through to Rails' +collection_select+ helper
316
+ # @param html_options [Hash] HTML Options hash passed through to Rails' +collection_select+ helper
315
317
  # @param block [Block] arbitrary HTML that will be rendered between the hint and the input
318
+ # @see https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select Rails collection_select (called by govuk_collection_select)
316
319
  # @return [ActiveSupport::SafeBuffer] HTML output
317
320
  #
318
321
  # @example A select box with hint
@@ -677,14 +680,13 @@ module GOVUKDesignSystemFormBuilder
677
680
  # @param text [String] the button text
678
681
  # @param warning [Boolean] makes the button red ({https://design-system.service.gov.uk/components/button/#warning-buttons warning}) when true
679
682
  # @param secondary [Boolean] makes the button grey ({https://design-system.service.gov.uk/components/button/#secondary-buttons secondary}) when true
680
- # @todo The GOV.UK design system also supports {https://design-system.service.gov.uk/components/button/#disabled-buttons disabled buttons}, they
681
- # should probably be supported too
682
683
  # @param classes [String] Classes to add to the submit button
683
684
  # @param prevent_double_click [Boolean] adds JavaScript to safeguard the
684
685
  # form from being submitted more than once
685
686
  # @param validate [Boolean] adds the formnovalidate to the submit button when true, this disables all
686
687
  # client-side validation provided by the browser. This is to provide a more consistent and accessible user
687
688
  # experience
689
+ # @param disabled [Boolean] makes the button disabled when true
688
690
  # @param block [Block] Any supplied HTML will be inserted immediately after
689
691
  # the submit button. It is intended for other buttons directly related to
690
692
  # the form's operation, such as 'Cancel' or 'Safe draft'
@@ -699,8 +701,8 @@ module GOVUKDesignSystemFormBuilder
699
701
  # = f.govuk_submit "Proceed", prevent_double_click: true do
700
702
  # = link_to 'Cancel', some_other_path, class: 'govuk-button__secondary'
701
703
  #
702
- def govuk_submit(text = config.default_submit_button_text, warning: false, secondary: false, classes: nil, prevent_double_click: true, validate: false, &block)
703
- Elements::Submit.new(self, text, warning: warning, secondary: secondary, classes: classes, prevent_double_click: prevent_double_click, validate: validate, &block).html
704
+ def govuk_submit(text = config.default_submit_button_text, warning: false, secondary: false, classes: nil, prevent_double_click: true, validate: false, disabled: false, &block)
705
+ Elements::Submit.new(self, text, warning: warning, secondary: secondary, classes: classes, prevent_double_click: prevent_double_click, validate: validate, disabled: disabled, &block).html
704
706
  end
705
707
 
706
708
  # Generates three inputs for the +day+, +month+ and +year+ components of a date
@@ -14,17 +14,18 @@ module GOVUKDesignSystemFormBuilder
14
14
  def html
15
15
  return yield unless limit?
16
16
 
17
- content_tag(
18
- 'div',
19
- class: %(#{brand}-character-count),
20
- data: { module: %(#{brand}-character-count) }.merge(**limit, **threshold).compact
21
- ) do
22
- yield
23
- end
17
+ content_tag('div', **character_count_options) { yield }
24
18
  end
25
19
 
26
20
  private
27
21
 
22
+ def character_count_options
23
+ {
24
+ class: %(#{brand}-character-count),
25
+ data: { module: %(#{brand}-character-count) }.merge(**limit, **threshold).compact
26
+ }
27
+ end
28
+
28
29
  def limit
29
30
  if @max_words
30
31
  { maxwords: @max_words }
@@ -10,13 +10,20 @@ module GOVUKDesignSystemFormBuilder
10
10
  end
11
11
 
12
12
  def html
13
- content_tag('div', class: check_boxes_classes, data: { module: %(#{brand}-checkboxes) }) do
13
+ content_tag('div', **check_boxes_options) do
14
14
  yield
15
15
  end
16
16
  end
17
17
 
18
18
  private
19
19
 
20
+ def check_boxes_options
21
+ {
22
+ class: check_boxes_classes,
23
+ data: { module: %(#{brand}-checkboxes) }
24
+ }
25
+ end
26
+
20
27
  def check_boxes_classes
21
28
  %w(checkboxes).prefix(brand).tap do |c|
22
29
  c.push(%(#{brand}-checkboxes--small)) if @small
@@ -17,19 +17,27 @@ module GOVUKDesignSystemFormBuilder
17
17
 
18
18
  def html
19
19
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
20
- Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, caption: @caption, described_by: [error_element.error_id, hint_element.hint_id]).html do
21
- safe_join(
22
- [
23
- hint_element.html,
24
- error_element.html,
25
- Containers::CheckBoxes.new(@builder, small: @small, classes: @classes).html do
26
- @block_content
27
- end
28
- ]
29
- )
20
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
21
+ safe_join([hint_element, error_element, checkboxes])
30
22
  end
31
23
  end
32
24
  end
25
+
26
+ private
27
+
28
+ def fieldset_options
29
+ {
30
+ legend: @legend,
31
+ caption: @caption,
32
+ described_by: [error_element.error_id, hint_element.hint_id]
33
+ }
34
+ end
35
+
36
+ def checkboxes
37
+ Containers::CheckBoxes.new(@builder, small: @small, classes: @classes).html do
38
+ @block_content
39
+ end
40
+ end
33
41
  end
34
42
  end
35
43
  end
@@ -26,40 +26,50 @@ module GOVUKDesignSystemFormBuilder
26
26
  end
27
27
 
28
28
  def html
29
- content_tag('fieldset', class: fieldset_classes, aria: { describedby: @described_by }) do
30
- safe_join([legend_content, (@block_content || yield)])
29
+ content_tag('fieldset', **fieldset_options) do
30
+ safe_join([legend, (@block_content || yield)])
31
31
  end
32
32
  end
33
33
 
34
34
  private
35
35
 
36
- def legend_content
37
- @legend_raw || build_legend
36
+ def fieldset_options
37
+ {
38
+ class: fieldset_classes,
39
+ aria: { describedby: @described_by }
40
+ }
41
+ end
42
+
43
+ def fieldset_classes
44
+ %w(fieldset).prefix(brand)
38
45
  end
39
46
 
40
- def build_legend
47
+ def legend
48
+ @legend_raw || legend_content
49
+ end
50
+
51
+ def legend_content
41
52
  if legend_text.present?
42
53
  content_tag('legend', class: legend_classes) do
43
54
  content_tag(@legend_options.dig(:tag), class: legend_heading_classes) do
44
- safe_join([caption_element.html, legend_text])
55
+ safe_join([caption_element, legend_text])
45
56
  end
46
57
  end
47
58
  end
48
59
  end
49
60
 
50
61
  def legend_text
51
- [@legend_options.dig(:text), localised_text(:legend)].compact.first
62
+ @legend_options.dig(:text) || localised_text(:legend)
52
63
  end
53
64
 
54
- def fieldset_classes
55
- %w(fieldset).prefix(brand)
65
+ def legend_size
66
+ @legend_options.dig(:size).tap do |size|
67
+ fail "invalid size '#{size}', must be #{LEGEND_SIZES.join(', ')}" unless size.in?(LEGEND_SIZES)
68
+ end
56
69
  end
57
70
 
58
71
  def legend_classes
59
- size = @legend_options.dig(:size)
60
- fail "invalid size '#{size}', must be #{LEGEND_SIZES.join(', ')}" unless size.in?(LEGEND_SIZES)
61
-
62
- [%(fieldset__legend), %(fieldset__legend--#{size})].prefix(brand).tap do |classes|
72
+ [%(fieldset__legend), %(fieldset__legend--#{legend_size})].prefix(brand).tap do |classes|
63
73
  classes.push(%(#{brand}-visually-hidden)) if @legend_options.dig(:hidden)
64
74
  end
65
75
  end
@@ -18,19 +18,27 @@ module GOVUKDesignSystemFormBuilder
18
18
 
19
19
  def html
20
20
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
21
- Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, caption: @caption, described_by: [error_element.error_id, hint_element.hint_id]).html do
22
- safe_join(
23
- [
24
- hint_element.html,
25
- error_element.html,
26
- Containers::Radios.new(@builder, inline: @inline, small: @small, classes: @classes).html do
27
- @block_content
28
- end
29
- ]
30
- )
21
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
22
+ safe_join([hint_element, error_element, radios])
31
23
  end
32
24
  end
33
25
  end
26
+
27
+ private
28
+
29
+ def fieldset_options
30
+ {
31
+ legend: @legend,
32
+ caption: @caption,
33
+ described_by: [error_element.error_id, hint_element.hint_id]
34
+ }
35
+ end
36
+
37
+ def radios
38
+ Containers::Radios.new(@builder, inline: @inline, small: @small, classes: @classes).html do
39
+ @block_content
40
+ end
41
+ end
34
42
  end
35
43
  end
36
44
  end
@@ -13,13 +13,20 @@ module GOVUKDesignSystemFormBuilder
13
13
  end
14
14
 
15
15
  def html
16
- content_tag('div', class: radios_classes, data: { module: %(#{brand}-radios) }) do
16
+ content_tag('div', **radios_options) do
17
17
  yield
18
18
  end
19
19
  end
20
20
 
21
21
  private
22
22
 
23
+ def radios_options
24
+ {
25
+ class: radios_classes,
26
+ data: { module: %(#{brand}-radios) }
27
+ }
28
+ end
29
+
23
30
  def radios_classes
24
31
  %w(radios).prefix(brand).tap do |c|
25
32
  c.push(%(#{brand}-radios--inline)) if @inline
@@ -10,9 +10,7 @@ module GOVUKDesignSystemFormBuilder
10
10
  def html
11
11
  return nil if @content.blank?
12
12
 
13
- content_tag('div', id: supplemental_id) do
14
- @content
15
- end
13
+ content_tag('div', id: supplemental_id) { @content }
16
14
  end
17
15
 
18
16
  def supplemental_id
@@ -3,7 +3,7 @@ module GOVUKDesignSystemFormBuilder
3
3
  class Caption < Base
4
4
  include Traits::Localisation
5
5
 
6
- def initialize(builder, object_name, attribute_name, text:, size: 'm')
6
+ def initialize(builder, object_name, attribute_name, text:, size: nil)
7
7
  super(builder, object_name, attribute_name)
8
8
 
9
9
  @text = caption_text(text)
@@ -21,7 +21,7 @@ module GOVUKDesignSystemFormBuilder
21
21
  end
22
22
 
23
23
  def caption_size_class(size)
24
- case size
24
+ case size || config.default_caption_size
25
25
  when 'xl' then %(#{brand}-caption-xl)
26
26
  when 'l' then %(#{brand}-caption-l)
27
27
  when 'm' then %(#{brand}-caption-m)
@@ -22,23 +22,28 @@ module GOVUKDesignSystemFormBuilder
22
22
 
23
23
  def html
24
24
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
25
- Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, caption: @caption, described_by: [error_id, hint_id, supplemental_id]).html do
26
- safe_join(
27
- [
28
- supplemental_content.html,
29
- hint_element.html,
30
- error_element.html,
31
- Containers::CheckBoxes.new(@builder, small: @small, classes: @classes).html do
32
- build_collection
33
- end
34
- ]
35
- )
25
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
26
+ safe_join([supplemental_content, hint_element, error_element, check_boxes])
36
27
  end
37
28
  end
38
29
  end
39
30
 
40
31
  private
41
32
 
33
+ def fieldset_options
34
+ {
35
+ legend: @legend,
36
+ caption: @caption,
37
+ described_by: [error_id, hint_id, supplemental_id]
38
+ }
39
+ end
40
+
41
+ def check_boxes
42
+ Containers::CheckBoxes.new(@builder, small: @small, classes: @classes).html do
43
+ collection
44
+ end
45
+ end
46
+
42
47
  # Builds a collection of check {Elements::CheckBoxes::CheckBox}
43
48
  # @return [ActiveSupport::SafeBuffer] HTML output
44
49
  #
@@ -47,7 +52,7 @@ module GOVUKDesignSystemFormBuilder
47
52
  # be rendered when it happens we need to work on the chance that it might, so
48
53
  # the +link_errors+ variable is set to +true+ if this attribute has errors and
49
54
  # always set back to +false+ after the first checkbox has been rendered
50
- def build_collection
55
+ def collection
51
56
  link_errors = has_errors?
52
57
 
53
58
  @builder.collection_check_boxes(@attribute_name, @collection, @value_method, @text_method) do |check_box|
@@ -10,31 +10,33 @@ module GOVUKDesignSystemFormBuilder
10
10
  def initialize(builder, object_name, attribute_name, checkbox, hint_method = nil, link_errors: false)
11
11
  super(builder, object_name, attribute_name)
12
12
 
13
- @checkbox = checkbox
14
- @item = checkbox.object
15
- @value = checkbox.value
16
- @hint_text = retrieve(@item, hint_method)
13
+ @checkbox = checkbox
14
+ @item = checkbox.object
15
+ @value = checkbox.value
16
+ @hint_text = retrieve(@item, hint_method)
17
17
  @link_errors = link_errors
18
18
  end
19
19
 
20
20
  def html
21
21
  content_tag('div', class: %(#{brand}-checkboxes__item)) do
22
- safe_join(
23
- [
24
- @checkbox.check_box(
25
- id: field_id(link_errors: @link_errors),
26
- class: %(#{brand}-checkboxes__input),
27
- aria: { describedby: hint_id }
28
- ),
29
- label_element.html,
30
- hint_element.html
31
- ]
32
- )
22
+ safe_join([check_box, label_element, hint_element])
33
23
  end
34
24
  end
35
25
 
36
26
  private
37
27
 
28
+ def check_box
29
+ @checkbox.check_box(**check_box_options)
30
+ end
31
+
32
+ def check_box_options
33
+ {
34
+ id: field_id(link_errors: @link_errors),
35
+ class: %(#{brand}-checkboxes__input),
36
+ aria: { describedby: hint_id }
37
+ }
38
+ end
39
+
38
40
  def label_element
39
41
  @label_element ||= Label.new(@builder, @object_name, @attribute_name, @checkbox, value: @value, link_errors: @link_errors)
40
42
  end
@@ -24,41 +24,37 @@ module GOVUKDesignSystemFormBuilder
24
24
  end
25
25
 
26
26
  def html
27
- safe_join(
28
- [
29
- content_tag('div', class: %(#{brand}-checkboxes__item)) do
30
- safe_join(
31
- [
32
- input,
33
- label_element.html,
34
- hint_element.html,
35
- ]
36
- )
37
- end,
38
- @conditional_content
39
- ]
40
- )
27
+ safe_join([check_box, @conditional_content])
41
28
  end
42
29
 
43
30
  private
44
31
 
32
+ def check_box
33
+ content_tag('div', class: %(#{brand}-checkboxes__item)) do
34
+ safe_join([input, label_element, hint_element])
35
+ end
36
+ end
37
+
45
38
  def input
46
- @builder.check_box(
47
- @attribute_name,
48
- {
49
- id: field_id(link_errors: @link_errors),
50
- class: check_box_classes,
51
- multiple: @multiple,
52
- aria: { describedby: hint_id },
53
- data: { 'aria-controls' => @conditional_id }
54
- },
55
- @value,
56
- false
57
- )
39
+ @builder.check_box(@attribute_name, input_options, @value, false)
40
+ end
41
+
42
+ def input_options
43
+ {
44
+ id: field_id(link_errors: @link_errors),
45
+ class: check_box_classes,
46
+ multiple: @multiple,
47
+ aria: { describedby: hint_id },
48
+ data: { 'aria-controls' => @conditional_id }
49
+ }
58
50
  end
59
51
 
60
52
  def label_element
61
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, checkbox: true, value: @value, link_errors: @link_errors, **label_args)
53
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_content, **label_options)
54
+ end
55
+
56
+ def label_options
57
+ { checkbox: true, value: @value, link_errors: @link_errors }
62
58
  end
63
59
 
64
60
  def hint_element