govuk_design_system_formbuilder 1.2.7 → 2.0.0b4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/lib/govuk_design_system_formbuilder/builder.rb +213 -90
  4. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +21 -10
  5. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +4 -3
  6. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +10 -10
  7. data/lib/govuk_design_system_formbuilder/elements/caption.rb +10 -5
  8. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +12 -12
  9. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +6 -2
  10. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +13 -8
  11. data/lib/govuk_design_system_formbuilder/elements/date.rb +8 -8
  12. data/lib/govuk_design_system_formbuilder/elements/file.rb +8 -8
  13. data/lib/govuk_design_system_formbuilder/elements/hint.rb +35 -8
  14. data/lib/govuk_design_system_formbuilder/elements/label.rb +18 -12
  15. data/lib/govuk_design_system_formbuilder/elements/legend.rb +18 -10
  16. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +14 -14
  17. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +9 -1
  18. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +12 -4
  19. data/lib/govuk_design_system_formbuilder/elements/select.rb +11 -11
  20. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +11 -11
  21. data/lib/govuk_design_system_formbuilder/traits/caption.rb +1 -9
  22. data/lib/govuk_design_system_formbuilder/traits/collection_item.rb +1 -1
  23. data/lib/govuk_design_system_formbuilder/traits/hint.rb +15 -2
  24. data/lib/govuk_design_system_formbuilder/traits/input.rb +49 -13
  25. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  26. metadata +11 -11
@@ -6,25 +6,25 @@ 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:, caption:, inline:, small:, bold_labels:, classes:, form_group_classes:, &block)
9
+ def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint_method:, hint:, legend:, caption:, inline:, small:, bold_labels:, classes:, form_group:, &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
- @inline = inline
17
- @small = small
18
- @legend = legend
19
- @caption = caption
20
- @hint_text = hint_text
21
- @classes = classes
22
- @form_group_classes = form_group_classes
23
- @bold_labels = hint_method.present? || bold_labels
12
+ @collection = collection
13
+ @value_method = value_method
14
+ @text_method = text_method
15
+ @hint_method = hint_method
16
+ @inline = inline
17
+ @small = small
18
+ @legend = legend
19
+ @caption = caption
20
+ @hint = hint
21
+ @classes = classes
22
+ @form_group = form_group
23
+ @bold_labels = hint_method.present? || bold_labels
24
24
  end
25
25
 
26
26
  def html
27
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
27
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
28
28
  Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
29
29
  safe_join([supplemental_content, hint_element, error_element, radios])
30
30
  end
@@ -41,7 +41,15 @@ module GOVUKDesignSystemFormBuilder
41
41
  end
42
42
 
43
43
  def hint_element
44
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, radio: true)
44
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
45
+ end
46
+
47
+ def hint_content
48
+ { text: @hint_text }
49
+ end
50
+
51
+ def hint_options
52
+ { value: @value, radio: true }
45
53
  end
46
54
 
47
55
  def label_element
@@ -8,12 +8,12 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::Hint
9
9
  include Traits::Conditional
10
10
 
11
- def initialize(builder, object_name, attribute_name, value, label:, hint_text:, link_errors:, &block)
11
+ def initialize(builder, object_name, attribute_name, value, label:, hint:, link_errors:, &block)
12
12
  super(builder, object_name, attribute_name)
13
13
 
14
14
  @value = value
15
15
  @label = label
16
- @hint_text = hint_text
16
+ @hint = hint
17
17
  @link_errors = has_errors? && link_errors
18
18
 
19
19
  if block_given?
@@ -34,16 +34,24 @@ module GOVUKDesignSystemFormBuilder
34
34
  end
35
35
  end
36
36
 
37
+ def radio_options
38
+ { radio: true }
39
+ end
40
+
37
41
  def label_element
38
42
  @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_content, **label_options)
39
43
  end
40
44
 
41
45
  def label_options
42
- { radio: true, value: @value, link_errors: @link_errors }
46
+ { value: @value, link_errors: @link_errors }.merge(radio_options)
43
47
  end
44
48
 
45
49
  def hint_element
46
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text, @value, radio: true)
50
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
51
+ end
52
+
53
+ def hint_options
54
+ { value: @value }.merge(radio_options)
47
55
  end
48
56
 
49
57
  def input
@@ -6,22 +6,22 @@ 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:, options: {}, html_options: {}, hint_text:, label:, caption:, form_group_classes:, &block)
9
+ def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, options: {}, html_options: {}, hint:, label:, caption:, form_group:, &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
- @options = options
16
- @html_options = html_options
17
- @label = label
18
- @caption = caption
19
- @hint_text = hint_text
20
- @form_group_classes = form_group_classes
12
+ @collection = collection
13
+ @value_method = value_method
14
+ @text_method = text_method
15
+ @options = options
16
+ @html_options = html_options
17
+ @label = label
18
+ @caption = caption
19
+ @hint = hint
20
+ @form_group = form_group
21
21
  end
22
22
 
23
23
  def html
24
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
24
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
25
25
  safe_join([label_element, supplemental_content, hint_element, error_element, select])
26
26
  end
27
27
  end
@@ -8,23 +8,23 @@ 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:, form_group_classes:, **kwargs, &block)
11
+ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, rows:, max_words:, max_chars:, threshold:, form_group:, **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
- @max_words = max_words
18
- @max_chars = max_chars
19
- @threshold = threshold
20
- @rows = rows
21
- @form_group_classes = form_group_classes
22
- @html_attributes = kwargs
14
+ @label = label
15
+ @caption = caption
16
+ @hint = hint
17
+ @max_words = max_words
18
+ @max_chars = max_chars
19
+ @threshold = threshold
20
+ @rows = rows
21
+ @form_group = form_group
22
+ @html_attributes = kwargs
23
23
  end
24
24
 
25
25
  def html
26
26
  Containers::CharacterCount.new(@builder, **character_count_options).html do
27
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
27
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
28
28
  safe_join([label_element, supplemental_content, hint_element, error_element, text_area, limit_description])
29
29
  end
30
30
  end
@@ -8,15 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  end
9
9
 
10
10
  def caption_options
11
- { text: nil }.merge({ text: caption_text, size: caption_size }.compact)
12
- end
13
-
14
- def caption_text
15
- @caption&.dig(:text)
16
- end
17
-
18
- def caption_size
19
- @caption&.dig(:size)
11
+ @caption
20
12
  end
21
13
  end
22
14
  end
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  when Symbol, String
9
9
  item.send(method)
10
10
  when Proc
11
- method.call(item)
11
+ capture { method.call(item) }
12
12
  end
13
13
  end
14
14
  end
@@ -2,7 +2,7 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Traits
3
3
  module Hint
4
4
  def hint_id
5
- return nil if @hint_text.blank?
5
+ return nil unless hint_element.active?
6
6
 
7
7
  build_id('hint')
8
8
  end
@@ -10,7 +10,20 @@ module GOVUKDesignSystemFormBuilder
10
10
  private
11
11
 
12
12
  def hint_element
13
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text)
13
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_content)
14
+ end
15
+
16
+ def hint_content
17
+ case @hint
18
+ when NilClass
19
+ {}
20
+ when Hash
21
+ @hint
22
+ when Proc
23
+ { content: @hint }
24
+ else
25
+ fail(ArgumentError, %(hint must be a Proc or Hash))
26
+ end
14
27
  end
15
28
  end
16
29
  end
@@ -1,38 +1,58 @@
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:, form_group_classes:, **kwargs, &block)
4
+ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, prefix_text:, suffix_text:, width:, form_group:, **kwargs, &block)
5
5
  super(builder, object_name, attribute_name, &block)
6
6
 
7
- @width = width
8
- @label = label
9
- @caption = caption
10
- @hint_text = hint_text
11
- @html_attributes = kwargs
12
- @form_group_classes = form_group_classes
7
+ @width = width
8
+ @label = label
9
+ @caption = caption
10
+ @hint = hint
11
+ @prefix_text = prefix_text
12
+ @suffix_text = suffix_text
13
+ @html_attributes = kwargs
14
+ @form_group = form_group
13
15
  end
14
16
 
15
17
  def html
16
- Containers::FormGroup.new(@builder, @object_name, @attribute_name, classes: @form_group_classes).html do
17
- safe_join([label_element, supplemental_content, hint_element, error_element, input])
18
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
19
+ safe_join([label_element, supplemental_content, hint_element, error_element, content])
18
20
  end
19
21
  end
20
22
 
21
23
  private
22
24
 
25
+ def content
26
+ if affixed?
27
+ affixed_input
28
+ else
29
+ input
30
+ end
31
+ end
32
+
33
+ def affixed_input
34
+ content_tag('div', class: %(#{brand}-input__wrapper)) do
35
+ safe_join([prefix, input, suffix])
36
+ end
37
+ end
38
+
23
39
  def input
24
- @builder.send(builder_method, @attribute_name, **input_options, **@html_attributes)
40
+ @builder.send(builder_method, @attribute_name, **options, **@html_attributes)
41
+ end
42
+
43
+ def affixed?
44
+ [@prefix_text, @suffix_text].any?
25
45
  end
26
46
 
27
- def input_options
47
+ def options
28
48
  {
29
49
  id: field_id(link_errors: true),
30
- class: input_classes,
50
+ class: classes,
31
51
  aria: { describedby: described_by(hint_id, error_id, supplemental_id) }
32
52
  }
33
53
  end
34
54
 
35
- def input_classes
55
+ def classes
36
56
  [%(#{brand}-input)].push(width_classes, error_classes).compact
37
57
  end
38
58
 
@@ -64,6 +84,22 @@ module GOVUKDesignSystemFormBuilder
64
84
  else fail(ArgumentError, "invalid width '#{@width}'")
65
85
  end
66
86
  end
87
+
88
+ def prefix
89
+ return nil if @prefix_text.blank?
90
+
91
+ tag.span(@prefix_text, class: %(#{brand}-input__prefix), **affix_options)
92
+ end
93
+
94
+ def suffix
95
+ return nil if @suffix_text.blank?
96
+
97
+ tag.span(@suffix_text, class: %(#{brand}-input__suffix), **affix_options)
98
+ end
99
+
100
+ def affix_options
101
+ { aria: { hidden: true } }
102
+ end
67
103
  end
68
104
  end
69
105
  end
@@ -1,3 +1,3 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
- VERSION = '1.2.7'.freeze
2
+ VERSION = '2.0.0b4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_design_system_formbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.7
4
+ version: 2.0.0b4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Yates
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-14 00:00:00.000000000 Z
11
+ date: 2020-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -134,14 +134,14 @@ dependencies:
134
134
  requirements:
135
135
  - - "~>"
136
136
  - !ruby/object:Gem::Version
137
- version: 0.18.5
137
+ version: 0.17.1
138
138
  type: :development
139
139
  prerelease: false
140
140
  version_requirements: !ruby/object:Gem::Requirement
141
141
  requirements:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
- version: 0.18.5
144
+ version: 0.17.1
145
145
  - !ruby/object:Gem::Dependency
146
146
  name: htmlbeautifier
147
147
  requirement: !ruby/object:Gem::Requirement
@@ -176,14 +176,14 @@ dependencies:
176
176
  requirements:
177
177
  - - "~>"
178
178
  - !ruby/object:Gem::Version
179
- version: 3.21.0
179
+ version: 3.22.0
180
180
  type: :development
181
181
  prerelease: false
182
182
  version_requirements: !ruby/object:Gem::Requirement
183
183
  requirements:
184
184
  - - "~>"
185
185
  - !ruby/object:Gem::Version
186
- version: 3.21.0
186
+ version: 3.22.0
187
187
  - !ruby/object:Gem::Dependency
188
188
  name: rubypants
189
189
  requirement: !ruby/object:Gem::Requirement
@@ -303,7 +303,7 @@ metadata:
303
303
  documentation_uri: https://www.rubydoc.info/gems/govuk_design_system_formbuilder/GOVUKDesignSystemFormBuilder/Builder
304
304
  homepage_uri: https://govuk-form-builder.netlify.app
305
305
  source_code_uri: https://github.com/DFE-Digital/govuk_design_system_formbuilder
306
- post_install_message:
306
+ post_install_message:
307
307
  rdoc_options: []
308
308
  require_paths:
309
309
  - lib
@@ -314,12 +314,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
314
314
  version: '0'
315
315
  required_rubygems_version: !ruby/object:Gem::Requirement
316
316
  requirements:
317
- - - ">="
317
+ - - ">"
318
318
  - !ruby/object:Gem::Version
319
- version: '0'
319
+ version: 1.3.1
320
320
  requirements: []
321
321
  rubygems_version: 3.1.2
322
- signing_key:
322
+ signing_key:
323
323
  specification_version: 4
324
324
  summary: GOV.UK-compliant Rails form builder
325
325
  test_files: []