katalyst-govuk-formbuilder 1.9.4 → 1.10.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1653be0dcf19647a3372e5fe35ea0d6dbf6241c13fc057f68f09ab129568c43f
4
- data.tar.gz: da4fbf9bce745fcb6f2c51fe5346b7f1e03526525a907d3951ea7c845ce3ce7e
3
+ metadata.gz: 2e03dce9b294fea3750ea7ec82957580fcb3cf6d28df45e0780dfda313ca7a69
4
+ data.tar.gz: 48faf010853d3cd17b1d49260e78f125d42cbd48eb43eb8206305df786fa6f34
5
5
  SHA512:
6
- metadata.gz: 15555c7d0a9bcb55a5f35a5768be15e365e7fbf0f8e2bffb9ab1217f0bd8b3335aed9a1731544b29f750b5c8db7d6324fb26e3522f129edfb3d57fe116fe1c47
7
- data.tar.gz: 4dee34d0bb82357ebe588593e42af4aab472afe2319aea41d6681d4380048317dc0ba04a86238852e86e5e2f0f7b17ec0f772cfb59302c5580f0778c8bf08fcc
6
+ metadata.gz: 3cd035acfd8a3ff0c55fe5f4754a7615f9f1db578dc0214dfc7f15df2547efc862e0898e4c5e4637f094250ea62a662192483031f6f9879cf4fe2b890cd7c257
7
+ data.tar.gz: 5cd513d0144aab0691f905245f612b0c93d8b053fde24448d74a72806a986fd2ec8a4ba73a9f37b1a40e766bcf487c67579d237a8c2442dd18f7d74daf80e095
data/README.md CHANGED
@@ -23,7 +23,7 @@ Or install it yourself as:
23
23
 
24
24
  Add the stylesheet to your default layout:
25
25
 
26
- ```erbruby
26
+ ```erb
27
27
  <%= stylesheet_link_tag "katalyst/govuk/formbuilder" %>
28
28
  ```
29
29
 
@@ -32,7 +32,7 @@ Some GOVUK components require javascript enhancements
32
32
 
33
33
  You can use the provided helper to load the formbuilder esm from importmaps and enhance your form:
34
34
 
35
- ```erbruby
35
+ ```erb
36
36
  <%= form_with ... %>
37
37
  <%= govuk_formbuilder_init %>
38
38
  ```
@@ -49,11 +49,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/kataly
49
49
 
50
50
  ## Release
51
51
 
52
- Update the version number in `lib/katalyst/govuk/formbuilder/version.rb` and run:
53
-
54
- ```bash
55
- bundle exec rake release
56
- ```
52
+ Tag the release version and push to CI.
57
53
 
58
54
  ## License
59
55
 
@@ -0,0 +1,197 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module GOVUK
5
+ module Formbuilder
6
+ module Builder
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ # Overwrite GOVUK default to set small to true
11
+ # @see GOVUKDesignSystemFormBuilder::Builder#govuk_radio_buttons_fieldset
12
+ def govuk_radio_buttons_fieldset(attribute_name, hint: {}, legend: {}, caption: {}, inline: false,
13
+ small: true, form_group: {}, **kwargs, &block)
14
+ GOVUKDesignSystemFormBuilder::Containers::RadioButtonsFieldset.new(
15
+ self, object_name, attribute_name,
16
+ hint:, legend:, caption:, inline:, small:, form_group:,
17
+ **kwargs, &block
18
+ ).html
19
+ end
20
+
21
+ # Overwrite GOVUK default to set small to true
22
+ # @see GOVUKDesignSystemFormBuilder::Builder#govuk_collection_check_boxes
23
+ def govuk_collection_check_boxes(attribute_name, collection, value_method, text_method, hint_method = nil,
24
+ hint: {}, legend: {}, caption: {}, small: true, form_group: {},
25
+ include_hidden: config.default_collection_check_boxes_include_hidden,
26
+ **kwargs, &block)
27
+ GOVUKDesignSystemFormBuilder::Elements::CheckBoxes::Collection.new(
28
+ self,
29
+ object_name,
30
+ attribute_name,
31
+ collection,
32
+ value_method:,
33
+ text_method:,
34
+ hint_method:,
35
+ hint:,
36
+ legend:,
37
+ caption:,
38
+ small:,
39
+ form_group:,
40
+ include_hidden:,
41
+ **kwargs,
42
+ &block
43
+ ).html
44
+ end
45
+
46
+ # Overwrite GOVUK default to set small to true
47
+ # @see GOVUKDesignSystemFormBuilder::Builder#govuk_check_boxes_fieldset
48
+ def govuk_check_boxes_fieldset(attribute_name, legend: {}, caption: {}, hint: {}, small: true, form_group: {},
49
+ multiple: true, **kwargs, &block)
50
+ GOVUKDesignSystemFormBuilder::Containers::CheckBoxesFieldset.new(
51
+ self,
52
+ object_name,
53
+ attribute_name,
54
+ hint:,
55
+ legend:,
56
+ caption:,
57
+ small:,
58
+ form_group:,
59
+ multiple:,
60
+ **kwargs,
61
+ &block
62
+ ).html
63
+ end
64
+ end
65
+
66
+ # Generates a check box within a fieldset to be used as a boolean toggle for a single attribute.
67
+ # The values are 1 (toggled on), and 0 (toggled off).
68
+ #
69
+ # @param attribute_name [Symbol] The name of the attribute
70
+ # @param small [Boolean] controls whether small check boxes are used instead of regular-sized ones
71
+ # @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+. When a +Proc+
72
+ # is supplied the hint will be wrapped in a +div+ instead of a +span+
73
+ # @option hint text [String] the hint text
74
+ # @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
75
+ # @param link_errors [Boolean] controls whether this checkbox should be linked to from {#govuk_error_summary}
76
+ # @option label text [String] the label text
77
+ # @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
78
+ # @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
79
+ # @option label hidden [Boolean] control the visibility of the label. Hidden labels will be read by
80
+ # screenreaders
81
+ # @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
82
+ # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element
83
+ # @param block [Block] any HTML passed in will form the contents of the fieldset
84
+ # @return [ActiveSupport::SafeBuffer] HTML output
85
+ #
86
+ # @example A single check box for terms and conditions
87
+ # = f.govuk_check_box_field :terms_agreed,
88
+ # link_errors: true,
89
+ # label: { text: 'Do you agree with our terms and conditions?' },
90
+ # hint: { text: 'You will not be able to proceed unless you do' }
91
+ #
92
+ def govuk_check_box_field(attribute_name, value = 1, unchecked_value = 0,
93
+ small: true, hint: {}, label: {}, link_errors: false, **kwargs, &block)
94
+ govuk_check_boxes_fieldset(attribute_name, legend: nil, multiple: false, small:) do
95
+ fieldset_context.pop # undo push from fieldset extension, labels should be bold unless already nested
96
+ checkbox = govuk_check_box(attribute_name, value, unchecked_value,
97
+ hint:,
98
+ label:,
99
+ link_errors:,
100
+ multiple: false,
101
+ exclusive: false,
102
+ **kwargs, &block)
103
+ fieldset_context.push attribute_name # restore push from fieldset
104
+ checkbox
105
+ end
106
+ end
107
+
108
+ # Overwrite GOVUK default to set small to true
109
+ # @see GOVUKDesignSystemFormBuilder::Builder#govuk_collection_radio_buttons
110
+ def govuk_collection_radio_buttons(attribute_name, collection, value_method, text_method = nil,
111
+ hint_method = nil, hint: {}, legend: {}, caption: {}, inline: false,
112
+ small: true, bold_labels: nil,
113
+ include_hidden: config.default_collection_radio_buttons_include_hidden,
114
+ form_group: {}, **, &)
115
+ GOVUKDesignSystemFormBuilder::Elements::Radios::Collection.new(
116
+ self,
117
+ object_name,
118
+ attribute_name,
119
+ collection,
120
+ value_method:,
121
+ text_method:,
122
+ hint_method:,
123
+ hint:,
124
+ legend:,
125
+ caption:,
126
+ inline:,
127
+ small:,
128
+ bold_labels:,
129
+ form_group:,
130
+ include_hidden:,
131
+ **,
132
+ &
133
+ ).html
134
+ end
135
+
136
+ # Generates a pair of +trix-toolbar+ and +trix-editor+ elements with a label, optional hint.
137
+ # Requires action-text to be correctly setup in the application
138
+ #
139
+ # @param attribute_name [Symbol] The name of the attribute
140
+ # @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+. When a +Proc+
141
+ # is supplied the hint will be wrapped in a +div+ instead of a +span+
142
+ # @option hint text [String] the hint text
143
+ # @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
144
+ # @param label [Hash,Proc] configures or sets the associated label content
145
+ # @option label text [String] the label text
146
+ # @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
147
+ # @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
148
+ # @option label hidden [Boolean] control the visibility of the label. Hidden labels will still be read by screen
149
+ # readers
150
+ # @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
151
+ # @param caption [Hash] configures or sets the caption content which is inserted above the label
152
+ # @option caption text [String] the caption text
153
+ # @option caption size [String] the size of the caption, can be +xl+, +l+ or +m+. Defaults to +m+
154
+ # @option caption kwargs [Hash] additional arguments are applied as attributes on the caption +span+ element
155
+ # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +trix-editor+ element.
156
+ # This is picked up and handled by the action-text gem
157
+ # @param form_group [Hash] configures the form group
158
+ # @option form_group classes [Array,String] sets the form group's classes
159
+ # @option form_group kwargs [Hash] additional attributes added to the form group
160
+ # @param block [Block] arbitrary HTML that will be rendered between the hint and the input
161
+ # @return [ActiveSupport::SafeBuffer] HTML output
162
+ #
163
+ # @example A rich text area with injected content
164
+ # = f.govuk_rich_text_area :description,
165
+ # label: { text: 'Where did the incident take place?' } do
166
+ #
167
+ # p.govuk-inset-text
168
+ # | If you don't know exactly leave this section blank
169
+ #
170
+ # @example A rich text area with the label supplied as a proc
171
+ # = f.govuk_rich_text_area :instructions,
172
+ # label: -> { tag.h3("How do you set it up?") }
173
+ #
174
+ # @example A rich text area with a custom direct upload url and a custom stimulus controller
175
+ # = f.govuk_rich_text_area :description,
176
+ # data: {
177
+ # direct_upload_url: direct_uploads_url,
178
+ # controller: "content--editor--trix",
179
+ # action: "trix-initialize->content--editor--trix#trixInitialize",
180
+ # }
181
+ #
182
+ def govuk_rich_text_area(attribute_name, hint: {}, label: {}, caption: {}, form_group: {}, **, &)
183
+ Elements::RichTextArea.new(
184
+ self, object_name, attribute_name,
185
+ hint:, label:, caption:, form_group:, **, &
186
+ ).html
187
+ end
188
+
189
+ # Keep track of whether we are inside a fieldset
190
+ # This allows labels to default to bold ("s") normally but use the default otherwise
191
+ def fieldset_context
192
+ @fieldset_context ||= []
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module GOVUK
5
+ module Formbuilder
6
+ module Containers
7
+ module FieldsetContext
8
+ def initialize(builder, object_name = nil, attribute_name = nil, &)
9
+ builder.fieldset_context << attribute_name
10
+
11
+ super
12
+
13
+ builder.fieldset_context.pop
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "govuk_design_system_formbuilder"
4
+
5
+ module Katalyst
6
+ module GOVUK
7
+ module Formbuilder
8
+ module Elements
9
+ # Extend Elements::Label to add support for human_attribute_name as a fallback
10
+ module Label
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ def retrieve_text(option_text, hidden)
15
+ text = option_text.presence ||
16
+ localised_text(:label).presence ||
17
+ human_attribute_name.presence ||
18
+ @attribute_name.to_s.humanize.capitalize.presence
19
+
20
+ if hidden
21
+ tag.span(text, class: %(#{brand}-visually-hidden))
22
+ else
23
+ text
24
+ end
25
+ end
26
+ end
27
+
28
+ def human_attribute_name
29
+ return unless @object_name.present? && @attribute_name.present?
30
+ return unless @builder.object&.class.respond_to?(:human_attribute_name)
31
+
32
+ @builder.object.class.human_attribute_name(@attribute_name)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "govuk_design_system_formbuilder"
4
+
5
+ module Katalyst
6
+ module GOVUK
7
+ module Formbuilder
8
+ module Elements
9
+ # Extend Elements::Legend to add support for human_attribute_name as a fallback
10
+ module Legend
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ def retrieve_text(supplied_text)
15
+ supplied_text.presence ||
16
+ localised_text(:legend).presence ||
17
+ human_attribute_name.presence ||
18
+ @attribute_name.to_s.humanize.capitalize.presence
19
+ end
20
+ end
21
+
22
+ def human_attribute_name
23
+ return unless @object_name.present? && @attribute_name.present?
24
+ return unless @builder.object&.class.respond_to?(:human_attribute_name)
25
+
26
+ @builder.object.class.human_attribute_name(@attribute_name)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "govuk_design_system_formbuilder"
4
+
5
+ module Katalyst
6
+ module GOVUK
7
+ module Formbuilder
8
+ module Elements
9
+ class RichTextArea < GOVUKDesignSystemFormBuilder::Base
10
+ using GOVUKDesignSystemFormBuilder::PrefixableArray
11
+
12
+ include GOVUKDesignSystemFormBuilder::Traits::Error
13
+ include GOVUKDesignSystemFormBuilder::Traits::Hint
14
+ include GOVUKDesignSystemFormBuilder::Traits::Label
15
+ include GOVUKDesignSystemFormBuilder::Traits::Supplemental
16
+ include GOVUKDesignSystemFormBuilder::Traits::HTMLAttributes
17
+ include GOVUKDesignSystemFormBuilder::Traits::HTMLClasses
18
+
19
+ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, **kwargs, &)
20
+ super(builder, object_name, attribute_name, &)
21
+
22
+ @label = label
23
+ @caption = caption
24
+ @hint = hint
25
+ @form_group = form_group
26
+ @html_attributes = kwargs
27
+ end
28
+
29
+ def html
30
+ GOVUKDesignSystemFormBuilder::Containers::FormGroup.new(*bound, **@form_group).html do
31
+ safe_join([label_element, supplemental_content, hint_element, error_element, rich_text_area])
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def rich_text_area
38
+ @builder.rich_text_area(@attribute_name, **attributes(@html_attributes))
39
+ end
40
+
41
+ def classes
42
+ build_classes(%(richtextarea), %(richtextarea--error) => has_errors?).prefix(brand)
43
+ end
44
+
45
+ def options
46
+ {
47
+ id: field_id(link_errors: true),
48
+ class: classes,
49
+ aria: { describedby: combine_references(hint_id, error_id, supplemental_id) },
50
+ }
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,10 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/inflector"
4
+ require "govuk_design_system_formbuilder"
5
+
3
6
  module Katalyst
4
7
  module GOVUK
5
8
  module Formbuilder
6
- # Engine is responsible for adding assets to load path
7
9
  class Engine < ::Rails::Engine
10
+ ActiveSupport::Inflector.inflections(:en) do |inflect|
11
+ inflect.acronym "GOVUK"
12
+ end
13
+
14
+ config.eager_load_namespaces << Katalyst::GOVUK::Formbuilder
15
+ config.paths.add("lib", autoload_once: true)
16
+
8
17
  initializer "katalyst-govuk-formbuilder.assets" do
9
18
  config.after_initialize do |app|
10
19
  if app.config.respond_to?(:assets)
@@ -14,6 +23,10 @@ module Katalyst
14
23
  end
15
24
  end
16
25
 
26
+ initializer "katalyst-govuk-formbuilder.extensions" do
27
+ GOVUKDesignSystemFormBuilder::Builder.include(Formbuilder::Extensions)
28
+ end
29
+
17
30
  initializer "katalyst-govuk-formbuilder.importmap", before: "importmap" do |app|
18
31
  if app.config.respond_to?(:importmap)
19
32
  app.config.importmap.paths << root.join("config/importmap.rb")
@@ -6,181 +6,18 @@ module Katalyst
6
6
  module GOVUK
7
7
  module Formbuilder
8
8
  module Extensions
9
- module Fieldset
10
- def initialize(builder, object_name = nil, attribute_name = nil, &)
11
- builder.fieldset_context << attribute_name
12
-
13
- super
14
-
15
- builder.fieldset_context.pop
16
- end
17
- end
18
- end
19
- end
20
- end
21
- end
22
-
23
- module GOVUKDesignSystemFormBuilder
24
- module Builder
25
- # Keep track of whether we are inside a fieldset
26
- # This allows labels to default to bold ("s") normally but use the default otherwise
27
- def fieldset_context
28
- @fieldset_context ||= []
29
- end
30
-
31
- # Overwrite GOVUK default to set small to true
32
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_collection_radio_buttons
33
- def govuk_collection_radio_buttons(attribute_name, collection, value_method, text_method = nil, hint_method = nil,
34
- hint: {}, legend: {}, caption: {}, inline: false, small: true, bold_labels: nil,
35
- include_hidden: config.default_collection_radio_buttons_include_hidden,
36
- form_group: {}, **kwargs, &block)
37
- Elements::Radios::Collection.new(
38
- self,
39
- object_name,
40
- attribute_name,
41
- collection,
42
- value_method:,
43
- text_method:,
44
- hint_method:,
45
- hint:,
46
- legend:,
47
- caption:,
48
- inline:,
49
- small:,
50
- bold_labels:,
51
- form_group:,
52
- include_hidden:,
53
- **kwargs,
54
- &block
55
- ).html
56
- end
57
-
58
- # Overwrite GOVUK default to set small to true
59
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_radio_buttons_fieldset
60
- def govuk_radio_buttons_fieldset(attribute_name, hint: {}, legend: {}, caption: {}, inline: false, small: true,
61
- form_group: {}, **kwargs, &block)
62
- Containers::RadioButtonsFieldset.new(self, object_name, attribute_name,
63
- hint:, legend:, caption:, inline:, small:, form_group:,
64
- **kwargs, &block).html
65
- end
66
-
67
- # Overwrite GOVUK default to set small to true
68
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_collection_check_boxes
69
- def govuk_collection_check_boxes(attribute_name, collection, value_method, text_method, hint_method = nil,
70
- hint: {}, legend: {}, caption: {}, small: true, form_group: {},
71
- include_hidden: config.default_collection_check_boxes_include_hidden,
72
- **kwargs, &block)
73
- Elements::CheckBoxes::Collection.new(
74
- self,
75
- object_name,
76
- attribute_name,
77
- collection,
78
- value_method:,
79
- text_method:,
80
- hint_method:,
81
- hint:,
82
- legend:,
83
- caption:,
84
- small:,
85
- form_group:,
86
- include_hidden:,
87
- **kwargs,
88
- &block
89
- ).html
90
- end
91
-
92
- # Overwrite GOVUK default to set small to true
93
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_check_boxes_fieldset
94
- def govuk_check_boxes_fieldset(attribute_name, legend: {}, caption: {}, hint: {}, small: true, form_group: {},
95
- multiple: true, **kwargs, &block)
96
- Containers::CheckBoxesFieldset.new(
97
- self,
98
- object_name,
99
- attribute_name,
100
- hint:,
101
- legend:,
102
- caption:,
103
- small:,
104
- form_group:,
105
- multiple:,
106
- **kwargs,
107
- &block
108
- ).html
109
- end
110
- end
111
-
112
- # Extend Traits::Label set the default size to small for non-nested labels.
113
- module Traits
114
- module Label
115
- private
116
-
117
- def label_content
118
- default = @builder.fieldset_context.count.positive? ? {} : { size: "s" }
119
-
120
- case @label
121
- when Hash
122
- default.merge(@label)
123
- when Proc
124
- default.merge(content: @label)
125
- else
126
- fail(ArgumentError, %(label must be a Proc or Hash))
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ GOVUKDesignSystemFormBuilder::Builder.include(Builder)
13
+ GOVUKDesignSystemFormBuilder::Elements::Label.include(Elements::Label)
14
+ GOVUKDesignSystemFormBuilder::Elements::Legend.include(Elements::Legend)
15
+ GOVUKDesignSystemFormBuilder::Traits::Label.include(Traits::Label)
16
+ GOVUKDesignSystemFormBuilder::Containers::Fieldset.include(Containers::FieldsetContext)
17
+ GOVUKDesignSystemFormBuilder::Containers::CheckBoxesFieldset.include(Containers::FieldsetContext)
18
+ GOVUKDesignSystemFormBuilder::Containers::RadioButtonsFieldset.include(Containers::FieldsetContext)
127
19
  end
128
20
  end
129
21
  end
130
22
  end
131
-
132
- # Extend Elements::Label to add support for human_attribute_name as a fallback
133
- module Elements
134
- class Label
135
- def retrieve_text(option_text, hidden)
136
- text = option_text.presence ||
137
- localised_text(:label).presence ||
138
- human_attribute_name.presence ||
139
- @attribute_name.to_s.humanize.capitalize.presence
140
-
141
- if hidden
142
- tag.span(text, class: %(#{brand}-visually-hidden))
143
- else
144
- text
145
- end
146
- end
147
-
148
- def human_attribute_name
149
- return unless @object_name.present? && @attribute_name.present?
150
- return unless @builder.object&.class.respond_to?(:human_attribute_name)
151
-
152
- @builder.object.class.human_attribute_name(@attribute_name)
153
- end
154
- end
155
-
156
- class Legend
157
- def retrieve_text(supplied_text)
158
- supplied_text.presence ||
159
- localised_text(:legend).presence ||
160
- human_attribute_name.presence ||
161
- @attribute_name.to_s.humanize.capitalize.presence
162
- end
163
-
164
- def human_attribute_name
165
- return unless @object_name.present? && @attribute_name.present?
166
- return unless @builder.object&.class.respond_to?(:human_attribute_name)
167
-
168
- @builder.object.class.human_attribute_name(@attribute_name)
169
- end
170
- end
171
- end
172
-
173
- module Containers
174
- class Fieldset
175
- include Katalyst::GOVUK::Formbuilder::Extensions::Fieldset
176
- end
177
-
178
- class CheckBoxesFieldset
179
- include Katalyst::GOVUK::Formbuilder::Extensions::Fieldset
180
- end
181
-
182
- class RadioButtonsFieldset
183
- include Katalyst::GOVUK::Formbuilder::Extensions::Fieldset
184
- end
185
- end
186
23
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "govuk_design_system_formbuilder"
4
+
5
+ module Katalyst
6
+ module GOVUK
7
+ module Formbuilder
8
+ module Traits
9
+ # Extend GovukDesignSystemFormBuilder::Traits::Label set the default size to small for non-nested labels.
10
+ module Label
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ private
15
+
16
+ def label_content
17
+ default = @builder.fieldset_context.count.positive? ? {} : { size: "s" }
18
+
19
+ case @label
20
+ when Hash
21
+ default.merge(@label)
22
+ when Proc
23
+ default.merge(content: @label)
24
+ else
25
+ fail(ArgumentError, %(label must be a Proc or Hash))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,16 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "govuk_design_system_formbuilder"
4
- require "govuk_design_system_formbuilder/elements/rich_text_area"
5
- require "govuk_design_system_formbuilder/elements/check_box_field"
6
-
7
- require "katalyst/govuk/formbuilder/engine"
8
- require "katalyst/govuk/formbuilder/extensions"
9
- require "katalyst/govuk/formbuilder/frontend"
10
-
11
3
  module Katalyst
12
4
  module GOVUK
13
5
  module Formbuilder
14
6
  end
15
7
  end
16
8
  end
9
+
10
+ require "katalyst/govuk/formbuilder/engine"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-govuk-formbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.4
4
+ version: 1.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-26 00:00:00.000000000 Z
11
+ date: 2024-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: govuk_design_system_formbuilder
@@ -41,12 +41,16 @@ files:
41
41
  - app/assets/stylesheets/katalyst/govuk/components/richtextarea/_richtextarea.scss
42
42
  - app/assets/stylesheets/katalyst/govuk/formbuilder.scss
43
43
  - config/importmap.rb
44
- - lib/govuk_design_system_formbuilder/elements/check_box_field.rb
45
- - lib/govuk_design_system_formbuilder/elements/rich_text_area.rb
46
44
  - lib/katalyst/govuk/formbuilder.rb
45
+ - lib/katalyst/govuk/formbuilder/builder.rb
46
+ - lib/katalyst/govuk/formbuilder/containers/fieldset_context.rb
47
+ - lib/katalyst/govuk/formbuilder/elements/label.rb
48
+ - lib/katalyst/govuk/formbuilder/elements/legend.rb
49
+ - lib/katalyst/govuk/formbuilder/elements/rich_text_area.rb
47
50
  - lib/katalyst/govuk/formbuilder/engine.rb
48
51
  - lib/katalyst/govuk/formbuilder/extensions.rb
49
52
  - lib/katalyst/govuk/formbuilder/frontend.rb
53
+ - lib/katalyst/govuk/formbuilder/traits/label.rb
50
54
  - node_modules/govuk-frontend/dist/govuk/_base.scss
51
55
  - node_modules/govuk-frontend/dist/govuk/all.scss
52
56
  - node_modules/govuk-frontend/dist/govuk/components/_all.scss
@@ -198,7 +202,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
198
202
  requirements:
199
203
  - - ">="
200
204
  - !ruby/object:Gem::Version
201
- version: '0'
205
+ version: '3.3'
202
206
  required_rubygems_version: !ruby/object:Gem::Requirement
203
207
  requirements:
204
208
  - - ">="
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "govuk_design_system_formbuilder"
4
-
5
- module GOVUKDesignSystemFormBuilder
6
- module Builder
7
- # Generates a check box within a fieldset to be used as a boolean toggle for a single attribute.
8
- # The values are 1 (toggled on), and 0 (toggled off).
9
- #
10
- # @param attribute_name [Symbol] The name of the attribute
11
- # @param small [Boolean] controls whether small check boxes are used instead of regular-sized ones
12
- # @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+. When a +Proc+ is
13
- # supplied the hint will be wrapped in a +div+ instead of a +span+
14
- # @option hint text [String] the hint text
15
- # @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
16
- # @param link_errors [Boolean] controls whether this checkbox should be linked to from {#govuk_error_summary}
17
- # @option label text [String] the label text
18
- # @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
19
- # @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
20
- # @option label hidden [Boolean] control the visibility of the label. Hidden labels will be read by screenreaders
21
- # @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
22
- # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element
23
- # @param block [Block] any HTML passed in will form the contents of the fieldset
24
- # @return [ActiveSupport::SafeBuffer] HTML output
25
- #
26
- # @example A single check box for terms and conditions
27
- # = f.govuk_check_box_field :terms_agreed,
28
- # link_errors: true,
29
- # label: { text: 'Do you agree with our terms and conditions?' },
30
- # hint: { text: 'You will not be able to proceed unless you do' }
31
- #
32
- def govuk_check_box_field(attribute_name, value = 1, unchecked_value = 0,
33
- small: true, hint: {}, label: {}, link_errors: false, **kwargs, &block)
34
- govuk_check_boxes_fieldset(attribute_name, legend: nil, multiple: false, small:) do
35
- fieldset_context.pop # undo push from fieldset extension, labels should be bold unless already nested
36
- checkbox = govuk_check_box(attribute_name, value, unchecked_value,
37
- hint:,
38
- label:,
39
- link_errors:,
40
- multiple: false,
41
- exclusive: false,
42
- **kwargs, &block)
43
- fieldset_context.push attribute_name # restore push from fieldset
44
- checkbox
45
- end
46
- end
47
- end
48
- end
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "govuk_design_system_formbuilder"
4
-
5
- module GOVUKDesignSystemFormBuilder
6
- module Elements
7
- class RichTextArea < Base
8
- using PrefixableArray
9
-
10
- include Traits::Error
11
- include Traits::Hint
12
- include Traits::Label
13
- include Traits::Supplemental
14
- include Traits::HTMLAttributes
15
- include Traits::HTMLClasses
16
-
17
- def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, **kwargs, &block)
18
- super(builder, object_name, attribute_name, &block)
19
-
20
- @label = label
21
- @caption = caption
22
- @hint = hint
23
- @form_group = form_group
24
- @html_attributes = kwargs
25
- end
26
-
27
- def html
28
- Containers::FormGroup.new(*bound, **@form_group).html do
29
- safe_join([label_element, supplemental_content, hint_element, error_element, rich_text_area])
30
- end
31
- end
32
-
33
- private
34
-
35
- def rich_text_area
36
- @builder.rich_text_area(@attribute_name, **attributes(@html_attributes))
37
- end
38
-
39
- def classes
40
- build_classes(%(richtextarea), %(richtextarea--error) => has_errors?).prefix(brand)
41
- end
42
-
43
- def options
44
- {
45
- id: field_id(link_errors: true),
46
- class: classes,
47
- aria: { describedby: combine_references(hint_id, error_id, supplemental_id) },
48
- }
49
- end
50
- end
51
- end
52
- end
53
-
54
- module GOVUKDesignSystemFormBuilder
55
- module Builder
56
- # Generates a pair of +trix-toolbar+ and +trix-editor+ elements with a label, optional hint. Requires action-text to
57
- # be correctly setup in the application
58
- #
59
- # @param attribute_name [Symbol] The name of the attribute
60
- # @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+. When a +Proc+ is
61
- # supplied the hint will be wrapped in a +div+ instead of a +span+
62
- # @option hint text [String] the hint text
63
- # @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
64
- # @param label [Hash,Proc] configures or sets the associated label content
65
- # @option label text [String] the label text
66
- # @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
67
- # @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
68
- # @option label hidden [Boolean] control the visibility of the label. Hidden labels will still be read by screen
69
- # readers
70
- # @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
71
- # @param caption [Hash] configures or sets the caption content which is inserted above the label
72
- # @option caption text [String] the caption text
73
- # @option caption size [String] the size of the caption, can be +xl+, +l+ or +m+. Defaults to +m+
74
- # @option caption kwargs [Hash] additional arguments are applied as attributes on the caption +span+ element
75
- # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +trix-editor+ element. This is
76
- # picked up and handled by the action-text gem
77
- # @param form_group [Hash] configures the form group
78
- # @option form_group classes [Array,String] sets the form group's classes
79
- # @option form_group kwargs [Hash] additional attributes added to the form group
80
- # @param block [Block] arbitrary HTML that will be rendered between the hint and the input
81
- # @return [ActiveSupport::SafeBuffer] HTML output
82
- #
83
- # @example A rich text area with injected content
84
- # = f.govuk_rich_text_area :description,
85
- # label: { text: 'Where did the incident take place?' } do
86
- #
87
- # p.govuk-inset-text
88
- # | If you don't know exactly leave this section blank
89
- #
90
- # @example A rich text area with the label supplied as a proc
91
- # = f.govuk_rich_text_area :instructions,
92
- # label: -> { tag.h3("How do you set it up?") }
93
- #
94
- # @example A rich text area with a custom direct upload url and a custom stimulus controller
95
- # = f.govuk_rich_text_area :description,
96
- # data: {
97
- # direct_upload_url: direct_uploads_url,
98
- # controller: "content--editor--trix",
99
- # action: "trix-initialize->content--editor--trix#trixInitialize",
100
- # }
101
- #
102
- def govuk_rich_text_area(attribute_name, hint: {}, label: {}, caption: {}, form_group: {}, **kwargs, &block)
103
- Elements::RichTextArea.new(self, object_name, attribute_name,
104
- hint:, label:, caption:, form_group:, **kwargs, &block).html
105
- end
106
- end
107
- end