katalyst-govuk-formbuilder 1.28.0 → 1.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '088f97f2c42cd5766eaa77fd9e85f60a2a02ea9b988aa3c6fb4924f107f1ac52'
4
- data.tar.gz: bfc23680580b356b1a4eff07a82cc5770c36740a76819e84fd6cd4a9adf3a239
3
+ metadata.gz: 9d4e88755cce23d5a3cc6744389d5719affe98d3f99da5e7166e1a51237caadb
4
+ data.tar.gz: 193369a6eaaa537f1b66e229ae8ae9a8cdd817d549683e154777328087c52e25
5
5
  SHA512:
6
- metadata.gz: a9c4758793c31b396fa8e75620f20ada27970b4da30f885b18acb5d7e62514fa8565bd5cff4965f76f6dddc4baf6ed28e72a102cd23e67e77bf339b947fe7eed
7
- data.tar.gz: 371302ba7c537c19bb0ec1ab31610b96f05dd1e999abef60aed649123bc96199b63a42667e54109df2b01475eb644414d2bdca0afa015ef170616667654aebc7
6
+ metadata.gz: 8f0de20a24e82ff967e0839dbccf2d0e0d94145880ccb6b44df47159a56bb96da0ec55397b5d5ae9b4821afe3f2231dc064a4fa6659b5a12af459f475f402cd0
7
+ data.tar.gz: 8c576643a92fd031ea13cc9ed5183d31aa09adfe09542ee0613f0fecedc66f50e77a4aed4c3b5773d339498bf88682711a5cb4d38193a6aac126aa49dcd204c0
data/config/ci.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Run using bin/ci
4
+
5
+ CI.run do
6
+ step "Style", "bundle exec rake lint"
7
+ step "Assets: Build", "bundle exec rake build"
8
+ step "Tests: RSpec", "bundle exec rake spec"
9
+ step "Security: Brakeman", "bundle exec rake security"
10
+ end
@@ -27,8 +27,17 @@ module Katalyst
27
27
  end
28
28
  end
29
29
 
30
+ # Mix the extension modules into GOVUKDesignSystemFormBuilder once, after the main
31
+ # autoloader is set up. The modules in app/helpers stay reloadable, but a consuming
32
+ # app only needs them wired once: re-applying on every reload would accumulate stale
33
+ # copies in the (non-reloadable) GOVUKDesignSystemFormBuilder ancestor chain.
34
+ #
35
+ # When developing this gem, spec/dummy re-applies them via to_prepare so that edits
36
+ # to the extension modules are picked up on reload.
30
37
  initializer "katalyst-govuk-formbuilder.extensions" do
31
- GOVUKDesignSystemFormBuilder::Builder.include(FormBuilder::Extensions)
38
+ config.after_initialize do
39
+ FormBuilder.inject_extensions!
40
+ end
32
41
  end
33
42
 
34
43
  initializer "katalyst-govuk-formbuilder.importmap", before: "importmap" do |app|
@@ -6,6 +6,20 @@ require "active_support/rails"
6
6
  module Katalyst
7
7
  module GOVUK
8
8
  module FormBuilder
9
+ # Mix our extension modules into the GOVUKDesignSystemFormBuilder classes.
10
+ #
11
+ # This is called from an after_initialize block (see Engine) or to_prepare in development (see dummy app)
12
+ def inject_extensions!
13
+ GOVUKDesignSystemFormBuilder.include(Config)
14
+ GOVUKDesignSystemFormBuilder::Builder.include(Builder)
15
+ GOVUKDesignSystemFormBuilder::Elements::Label.include(Elements::Label)
16
+ GOVUKDesignSystemFormBuilder::Elements::Legend.include(Elements::Legend)
17
+ GOVUKDesignSystemFormBuilder::Traits::Label.include(Traits::Label)
18
+ GOVUKDesignSystemFormBuilder::Containers::Fieldset.include(Containers::FieldsetContext)
19
+ GOVUKDesignSystemFormBuilder::Containers::CheckBoxesFieldset.include(Containers::FieldsetContext)
20
+ GOVUKDesignSystemFormBuilder::Containers::RadioButtonsFieldset.include(Containers::FieldsetContext)
21
+ end
22
+ module_function :inject_extensions!
9
23
  end
10
24
  end
11
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-govuk-formbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.28.0
4
+ version: 1.29.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
@@ -43,23 +43,12 @@ files:
43
43
  - app/assets/stylesheets/katalyst/govuk/components/richtextarea/_index.scss
44
44
  - app/assets/stylesheets/katalyst/govuk/components/richtextarea/_mixin.scss
45
45
  - app/assets/stylesheets/katalyst/govuk/formbuilder.scss
46
+ - config/ci.rb
46
47
  - config/importmap.rb
47
48
  - lib/katalyst-govuk-formbuilder.rb
48
49
  - lib/katalyst/govuk/form_builder.rb
49
- - lib/katalyst/govuk/form_builder/builder.rb
50
50
  - lib/katalyst/govuk/form_builder/config.rb
51
- - lib/katalyst/govuk/form_builder/containers/fieldset_context.rb
52
- - lib/katalyst/govuk/form_builder/elements/combobox.rb
53
- - lib/katalyst/govuk/form_builder/elements/document.rb
54
- - lib/katalyst/govuk/form_builder/elements/image.rb
55
- - lib/katalyst/govuk/form_builder/elements/label.rb
56
- - lib/katalyst/govuk/form_builder/elements/legend.rb
57
- - lib/katalyst/govuk/form_builder/elements/rich_textarea.rb
58
51
  - lib/katalyst/govuk/form_builder/engine.rb
59
- - lib/katalyst/govuk/form_builder/extensions.rb
60
- - lib/katalyst/govuk/form_builder/frontend.rb
61
- - lib/katalyst/govuk/form_builder/traits/file.rb
62
- - lib/katalyst/govuk/form_builder/traits/label.rb
63
52
  - lib/katalyst/govuk/formbuilder.rb
64
53
  - node_modules/govuk-frontend/dist/govuk/_base.import.scss
65
54
  - node_modules/govuk-frontend/dist/govuk/_base.scss
@@ -1,336 +0,0 @@
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_collection_radio_buttons
12
- def govuk_collection_radio_buttons(attribute_name, collection, value_method, text_method = nil,
13
- hint_method = nil, hint: {}, legend: {}, caption: {}, inline: false,
14
- small: true, bold_labels: nil,
15
- include_hidden: config.default_collection_radio_buttons_include_hidden,
16
- form_group: {}, **, &)
17
- GOVUKDesignSystemFormBuilder::Elements::Radios::Collection.new(
18
- self,
19
- object_name,
20
- attribute_name,
21
- collection,
22
- value_method:,
23
- text_method:,
24
- hint_method:,
25
- hint:,
26
- legend:,
27
- caption:,
28
- inline:,
29
- small:,
30
- bold_labels:,
31
- form_group:,
32
- include_hidden:,
33
- **,
34
- &
35
- ).html
36
- end
37
-
38
- # Overwrite GOVUK default to set small to true
39
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_radio_buttons_fieldset
40
- def govuk_radio_buttons_fieldset(attribute_name, hint: {}, legend: {}, caption: {}, inline: false,
41
- small: true, form_group: {}, **, &)
42
- GOVUKDesignSystemFormBuilder::Containers::RadioButtonsFieldset.new(
43
- self, object_name, attribute_name,
44
- hint:, legend:, caption:, inline:, small:, form_group:,
45
- **, &
46
- ).html
47
- end
48
-
49
- # Overwrite GOVUK default to set small to true
50
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_collection_check_boxes
51
- def govuk_collection_check_boxes(attribute_name, collection, value_method, text_method, hint_method = nil,
52
- hint: {}, legend: {}, caption: {}, small: true, form_group: {},
53
- include_hidden: config.default_collection_check_boxes_include_hidden,
54
- **, &)
55
- GOVUKDesignSystemFormBuilder::Elements::CheckBoxes::Collection.new(
56
- self,
57
- object_name,
58
- attribute_name,
59
- collection,
60
- value_method:,
61
- text_method:,
62
- hint_method:,
63
- hint:,
64
- legend:,
65
- caption:,
66
- small:,
67
- form_group:,
68
- include_hidden:,
69
- **,
70
- &
71
- ).html
72
- end
73
-
74
- # Overwrite GOVUK default to set small to true
75
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_check_boxes_fieldset
76
- def govuk_check_boxes_fieldset(attribute_name, legend: {}, caption: {}, hint: {}, small: true, form_group: {},
77
- multiple: true, **, &)
78
- GOVUKDesignSystemFormBuilder::Containers::CheckBoxesFieldset.new(
79
- self,
80
- object_name,
81
- attribute_name,
82
- hint:,
83
- legend:,
84
- caption:,
85
- small:,
86
- form_group:,
87
- multiple:,
88
- **,
89
- &
90
- ).html
91
- end
92
- end
93
-
94
- # Generates a check box within a fieldset to be used as a boolean toggle for a single attribute.
95
- # The values are 1 (toggled on), and 0 (toggled off).
96
- #
97
- # @param attribute_name [Symbol] The name of the attribute
98
- # @param small [Boolean] controls whether small check boxes are used instead of regular-sized ones
99
- # @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+. When a +Proc+
100
- # is supplied the hint will be wrapped in a +div+ instead of a +span+
101
- # @option hint text [String] the hint text
102
- # @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
103
- # @param link_errors [Boolean] controls whether this checkbox should be linked to from {#govuk_error_summary}
104
- # @option label text [String] the label text
105
- # @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
106
- # @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
107
- # @option label hidden [Boolean] control the visibility of the label. Hidden labels will be read by
108
- # screenreaders
109
- # @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
110
- # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element
111
- # @param block [Block] any HTML passed in will form the contents of the fieldset
112
- # @return [ActiveSupport::SafeBuffer] HTML output
113
- #
114
- # @example A single check box for terms and conditions
115
- # = f.govuk_check_box_field :terms_agreed,
116
- # link_errors: true,
117
- # label: { text: 'Do you agree with our terms and conditions?' },
118
- # hint: { text: 'You will not be able to proceed unless you do' }
119
- #
120
- def govuk_check_box_field(attribute_name, value = 1, unchecked_value = 0,
121
- small: true, hint: {}, label: {}, link_errors: false, **, &)
122
- govuk_check_boxes_fieldset(attribute_name, legend: nil, multiple: false, small:) do
123
- fieldset_context.pop # undo push from fieldset extension, labels should be bold unless already nested
124
- checkbox = govuk_check_box(attribute_name, value, unchecked_value,
125
- hint:,
126
- label:,
127
- link_errors:,
128
- multiple: false,
129
- exclusive: false,
130
- **, &)
131
- fieldset_context.push attribute_name # restore push from fieldset
132
- checkbox
133
- end
134
- end
135
-
136
- # Generates a select for an enum defined in the model.
137
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_collection_select
138
- def govuk_enum_select(attribute_name, **, &)
139
- govuk_collection_select(attribute_name, enum_values(attribute_name),
140
- :itself, enum_labels_for(attribute_name), **, &)
141
- end
142
-
143
- # Generates a checkbox fieldset for an enum defined in the model.
144
- #
145
- # @api internal
146
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_collection_check_boxes
147
- def govuk_enum_check_boxes(attribute_name, **, &)
148
- govuk_collection_check_boxes(attribute_name, enum_values(attribute_name),
149
- :itself, enum_labels_for(attribute_name), **, &)
150
- end
151
-
152
- # Generates a radio buttons fieldset for an enum defined in the model.
153
- # @see GOVUKDesignSystemFormBuilder::Builder#govuk_collection_radio_buttons
154
- def govuk_enum_radio_buttons(attribute_name, **, &)
155
- govuk_collection_radio_buttons(attribute_name, enum_values(attribute_name),
156
- :itself, enum_labels_for(attribute_name), **, &)
157
- end
158
-
159
- # Generates a pair of +trix-toolbar+ and +trix-editor+ elements with a label, optional hint.
160
- # Requires action-text to be correctly setup in the application
161
- #
162
- # @param attribute_name [Symbol] The name of the attribute
163
- # @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+. When a +Proc+
164
- # is supplied the hint will be wrapped in a +div+ instead of a +span+
165
- # @option hint text [String] the hint text
166
- # @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
167
- # @param label [Hash,Proc] configures or sets the associated label content
168
- # @option label text [String] the label text
169
- # @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
170
- # @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
171
- # @option label hidden [Boolean] control the visibility of the label. Hidden labels will still be read by screen
172
- # readers
173
- # @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
174
- # @param caption [Hash] configures or sets the caption content which is inserted above the label
175
- # @option caption text [String] the caption text
176
- # @option caption size [String] the size of the caption, can be +xl+, +l+ or +m+. Defaults to +m+
177
- # @option caption kwargs [Hash] additional arguments are applied as attributes on the caption +span+ element
178
- # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +trix-editor+ element.
179
- # This is picked up and handled by the action-text gem
180
- # @param form_group [Hash] configures the form group
181
- # @option form_group classes [Array,String] sets the form group's classes
182
- # @option form_group kwargs [Hash] additional attributes added to the form group
183
- # @param & [Block] arbitrary HTML that will be rendered between the hint and the input
184
- # @return [ActiveSupport::SafeBuffer] HTML output
185
- #
186
- # @example A rich text area with injected content
187
- # = f.govuk_rich_text_area :description,
188
- # label: { text: 'Where did the incident take place?' } do
189
- #
190
- # p.govuk-inset-text
191
- # | If you don't know exactly leave this section blank
192
- #
193
- # @example A rich text area with the label supplied as a proc
194
- # = f.govuk_rich_text_area :instructions,
195
- # label: -> { tag.h3("How do you set it up?") }
196
- #
197
- def govuk_rich_textarea(attribute_name, hint: {}, label: {}, caption: {}, form_group: {}, **, &)
198
- Elements::RichTextarea.new(
199
- self, object_name, attribute_name,
200
- hint:, label:, caption:, form_group:, **, &
201
- ).html
202
- end
203
- alias_method :govuk_rich_text_area, :govuk_rich_textarea
204
-
205
- # Generates a +combobox+ element that uses Hotwire Combobox to generate a combobox selection element.
206
- # @see https://hotwirecombobox.com
207
- # @see https://github.com/josefarias/hotwire_combobox
208
- #
209
- # @param attribute_name [Symbol] The name of the attribute
210
- # @param options_or_src [Array] The +option+ values or a source path for async combobox
211
- # @param options [Hash] Options hash passed through to the +combobox+ helper
212
- # @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+.
213
- # When a +Proc+ is supplied the hint will be wrapped in a +div+ instead of a +span+
214
- # @option hint text [String] the hint text
215
- # @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
216
- # @param label [Hash,Proc] configures or sets the associated label content
217
- # @option label text [String] the label text
218
- # @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
219
- # @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
220
- # @option label hidden [Boolean] control the visibility of the label.
221
- # Hidden labels will still be read by screenreaders
222
- # @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
223
- # @param form_group [Hash] configures the form group
224
- # @option form_group kwargs [Hash] additional attributes added to the form group
225
- # @param before_input [String,Proc] the content injected before the input. No content will be added if +nil+
226
- # @param after_input [String,Proc] the content injected after the input. No content will be added if +nil+
227
- # @param & [Block] build the contents of the select element manually for exact control
228
- # @see https://hotwirecombobox.com Hotwire Combobox's +combobox+ (called by govuk_combobox)
229
- # @return [ActiveSupport::SafeBuffer] HTML output
230
- #
231
- # @example A combobox that allows the user to choose from a list of states
232
- #
233
- # = f.combobox "state", State.all
234
- #
235
- # @example A combobox that allows the user to choose from an asynchronous states endpoint
236
- #
237
- # = f.combobox "state", states_path
238
- #
239
- # @example A multi-select combobox that allows the user to choose multiple states
240
- #
241
- # = f.combobox "state", State.all, multiselect_chip_src: states_chips_path
242
- #
243
- def govuk_combobox(attribute_name, options_or_src = [], options: {}, label: {}, hint: {}, form_group: {},
244
- caption: {}, before_input: nil, after_input: nil, **, &)
245
- Elements::Combobox.new(
246
- self, object_name, attribute_name, options_or_src,
247
- options:, label:, hint:, form_group:, caption:, before_input:, after_input:, **, &
248
- ).html
249
- end
250
-
251
- # Generates a file input element for uploading documents.
252
- #
253
- # @example A upload field with label as a proc
254
- # = f.govuk_document_field :data, label: -> { tag.h3('Upload your document') }
255
- #
256
- def govuk_document_field(attribute_name,
257
- label: {},
258
- caption: {},
259
- hint: {},
260
- form_group: {},
261
- mime_types: config.document_mime_types,
262
- **,
263
- &)
264
- Elements::Document.new(
265
- self, object_name, attribute_name, label:, caption:, hint:, form_group:, mime_types:, **, &
266
- ).html
267
- end
268
-
269
- # Generates a file input element with a preview for uploading images.
270
- #
271
- # @param attribute_name [Symbol] The name of the attribute
272
- # @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+.
273
- # When a +Proc+ is supplied the hint will be wrapped in a +div+ instead of a +span+
274
- # @option hint text [String] the hint text
275
- # @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
276
- # @param label [Hash,Proc] configures or sets the associated label content
277
- # @option label text [String] the label text
278
- # @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
279
- # @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
280
- # @option label hidden [Boolean] control the visibility of the label. Hidden labels will still be read by screen
281
- # readers
282
- # @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
283
- # @param caption [Hash] configures or sets the caption content which is inserted above the label
284
- # @option caption text [String] the caption text
285
- # @option caption size [String] the size of the caption, can be +xl+, +l+ or +m+. Defaults to +m+
286
- # @option caption kwargs [Hash] additional arguments are applied as attributes on the caption +span+ element
287
- # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element.
288
- # @param form_group [Hash] configures the form group
289
- # @option form_group classes [Array,String] sets the form group's classes
290
- # @option form_group kwargs [Hash] additional attributes added to the form group
291
- # @param & [Block] arbitrary HTML that will be rendered between the hint and the input
292
- # @return [ActiveSupport::SafeBuffer] HTML output
293
- #
294
- # @example An image field with injected content
295
- # = f.govuk_image_field :incident_image,
296
- # label: { text: 'Attach a picture of the incident' } do
297
- #
298
- # p.govuk-inset-text
299
- # | If you don't know exactly leave this section blank
300
- #
301
- # @example A image upload field with label as a proc
302
- # = f.govuk_image_field :image, label: -> { tag.h3('Upload your image') }
303
- #
304
- def govuk_image_field(attribute_name,
305
- label: {},
306
- caption: {},
307
- hint: {},
308
- form_group: {},
309
- mime_types: config.image_mime_types,
310
- **,
311
- &)
312
- Elements::Image.new(
313
- self, object_name, attribute_name, label:, caption:, hint:, form_group:, mime_types:, **, &
314
- ).html
315
- end
316
-
317
- # Keep track of whether we are inside a fieldset
318
- # This allows labels to default to bold ("s") normally but use the default otherwise
319
- def fieldset_context
320
- @fieldset_context ||= []
321
- end
322
-
323
- private
324
-
325
- def enum_values(attribute_name)
326
- object.class.defined_enums[attribute_name.to_s].keys
327
- end
328
-
329
- def enum_labels_for(attribute_name)
330
- model = object.class
331
- ->(value) { model.human_attribute_name("#{attribute_name}.#{value}") }
332
- end
333
- end
334
- end
335
- end
336
- end
@@ -1,19 +0,0 @@
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
@@ -1,85 +0,0 @@
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 Combobox < GOVUKDesignSystemFormBuilder::Base
10
- include GOVUKDesignSystemFormBuilder::Traits::Error
11
- include GOVUKDesignSystemFormBuilder::Traits::Label
12
- include GOVUKDesignSystemFormBuilder::Traits::Hint
13
- include GOVUKDesignSystemFormBuilder::Traits::HTMLAttributes
14
- include GOVUKDesignSystemFormBuilder::Traits::ContentBeforeAndAfter
15
-
16
- def initialize(builder,
17
- object_name,
18
- attribute_name,
19
- options_or_src,
20
- options:,
21
- form_group:,
22
- label:,
23
- hint:,
24
- caption:,
25
- before_input:,
26
- after_input:,
27
- **kwargs,
28
- &block)
29
- # assign the block to a variable rather than passing to super so
30
- # we can send it through to #combobox
31
- super(builder, object_name, attribute_name)
32
- @block = block
33
-
34
- @form_group = form_group
35
- @hint = hint
36
- @label = label
37
- @caption = caption
38
- @options_or_src = options_or_src
39
- @options = options
40
- @html_attributes = kwargs
41
- @before_input = before_input
42
- @after_input = after_input
43
- end
44
-
45
- def html
46
- GOVUKDesignSystemFormBuilder::Containers::FormGroup.new(*bound, **@form_group).html do
47
- safe_join([
48
- label_element,
49
- hint_element,
50
- error_element,
51
- before_input_content,
52
- combobox,
53
- after_input_content,
54
- ])
55
- end
56
- end
57
-
58
- private
59
-
60
- def combobox
61
- attrs = attributes(@html_attributes)
62
- attrs[:class] = attrs.delete(:class).join(" ") # hotwire_combobox does not support flattening classes
63
- @builder.combobox(@attribute_name, @options_or_src, **@options, **attrs, &@block)
64
- end
65
-
66
- def options
67
- {
68
- id: field_id(link_errors: true),
69
- class: classes,
70
- aria: { describedby: combine_references(hint_id, error_id) },
71
- }
72
- end
73
-
74
- def classes
75
- combine_references(%(#{brand}-combobox), error_class)
76
- end
77
-
78
- def error_class
79
- %(#{brand}-combobox--error) if has_errors?
80
- end
81
- end
82
- end
83
- end
84
- end
85
- end
@@ -1,68 +0,0 @@
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 Document < GOVUKDesignSystemFormBuilder::Base
10
- include ActionDispatch::Routing::RouteSet::MountedHelpers
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
- include FormBuilder::Traits::File
20
-
21
- def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, mime_types:,
22
- **kwargs, &)
23
- super(builder, object_name, attribute_name, &)
24
-
25
- @mime_types = mime_types
26
- @label = label
27
- @caption = caption
28
- @hint = hint
29
- @html_attributes = kwargs.merge(file_input_options)
30
- @form_group = form_group
31
- end
32
-
33
- def html
34
- GOVUKDesignSystemFormBuilder::Containers::FormGroup.new(
35
- *bound,
36
- **default_form_group_options(**@form_group),
37
- ).html do
38
- safe_join([label_element, preview, hint_element, error_element, file, destroy_element,
39
- supplemental_content])
40
- end
41
- end
42
-
43
- def preview
44
- options = {}
45
- add_option(options, :data, "#{stimulus_controller}_target", "preview")
46
- add_option(options, :class, "preview-file")
47
- options[:hidden] = "" unless preview?
48
-
49
- tag.div(**options) do
50
- filename = @builder.object.send(@attribute_name).filename.to_s
51
- tag.p(filename, class: "preview-filename") + destroy_element_trigger
52
- end
53
- end
54
-
55
- private
56
-
57
- def stimulus_controller
58
- "govuk-document-field"
59
- end
60
-
61
- def form_group_class
62
- "govuk-document-field"
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Katalyst
4
- module GOVUK
5
- module FormBuilder
6
- module Elements
7
- class Image < GOVUKDesignSystemFormBuilder::Base
8
- include ActionDispatch::Routing::RouteSet::MountedHelpers
9
-
10
- include GOVUKDesignSystemFormBuilder::Traits::Error
11
- include GOVUKDesignSystemFormBuilder::Traits::Hint
12
- include GOVUKDesignSystemFormBuilder::Traits::Label
13
- include GOVUKDesignSystemFormBuilder::Traits::Supplemental
14
- include GOVUKDesignSystemFormBuilder::Traits::HTMLAttributes
15
- include GOVUKDesignSystemFormBuilder::Traits::HTMLClasses
16
-
17
- include FormBuilder::Traits::File
18
-
19
- def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, mime_types:,
20
- **kwargs, &)
21
- super(builder, object_name, attribute_name, &)
22
-
23
- @mime_types = mime_types
24
- @label = label
25
- @caption = caption
26
- @hint = hint
27
- @html_attributes = kwargs.merge(file_input_options)
28
- @form_group = form_group
29
- end
30
-
31
- def html
32
- GOVUKDesignSystemFormBuilder::Containers::FormGroup.new(
33
- *bound,
34
- **default_form_group_options(**@form_group),
35
- ).html do
36
- safe_join([label_element, preview, hint_element, error_element, file, destroy_element,
37
- supplemental_content])
38
- end
39
- end
40
-
41
- def preview
42
- options = {}
43
- add_option(options, :data, "#{stimulus_controller}_target", "preview")
44
- add_option(options, :class, "preview-image")
45
- options[:hidden] = "" unless preview?
46
-
47
- tag.div(**options) do
48
- tag.img(src: preview_url, class: "image-thumbnail") + destroy_element_trigger
49
- end
50
- end
51
-
52
- private
53
-
54
- def stimulus_controller
55
- "govuk-image-field"
56
- end
57
-
58
- def form_group_class
59
- "govuk-image-field"
60
- end
61
- end
62
- end
63
- end
64
- end
65
- end
@@ -1,38 +0,0 @@
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
@@ -1,32 +0,0 @@
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
@@ -1,70 +0,0 @@
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(
20
- builder,
21
- object_name,
22
- attribute_name,
23
- hint:,
24
- label:,
25
- caption:,
26
- form_group:,
27
- **kwargs,
28
- &
29
- )
30
- super(builder, object_name, attribute_name, &)
31
-
32
- @label = label
33
- @caption = caption
34
- @hint = hint
35
- @form_group = form_group
36
- @html_attributes = kwargs
37
- end
38
-
39
- def html
40
- GOVUKDesignSystemFormBuilder::Containers::FormGroup.new(*bound, **@form_group).html do
41
- safe_join([label_element, supplemental_content, hint_element, error_element, rich_textarea])
42
- end
43
- end
44
-
45
- private
46
-
47
- def rich_textarea
48
- @builder.rich_textarea(@attribute_name, **attributes(@html_attributes))
49
- end
50
-
51
- def classes
52
- build_classes(%(rich-textarea), %(rich-textarea--error) => has_errors?).prefix(brand)
53
- end
54
-
55
- def options
56
- {
57
- id: field_id,
58
- class: classes,
59
- aria: { describedby: combine_references(hint_id, error_id) },
60
- }
61
- end
62
-
63
- def label_content
64
- super.merge(link_errors: false)
65
- end
66
- end
67
- end
68
- end
69
- end
70
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "govuk_design_system_formbuilder"
4
-
5
- module Katalyst
6
- module GOVUK
7
- module FormBuilder
8
- module Extensions
9
- extend ActiveSupport::Concern
10
-
11
- included do
12
- GOVUKDesignSystemFormBuilder.include(Config)
13
- GOVUKDesignSystemFormBuilder::Builder.include(Builder)
14
- GOVUKDesignSystemFormBuilder::Elements::Label.include(Elements::Label)
15
- GOVUKDesignSystemFormBuilder::Elements::Legend.include(Elements::Legend)
16
- GOVUKDesignSystemFormBuilder::Traits::Label.include(Traits::Label)
17
- GOVUKDesignSystemFormBuilder::Containers::Fieldset.include(Containers::FieldsetContext)
18
- GOVUKDesignSystemFormBuilder::Containers::CheckBoxesFieldset.include(Containers::FieldsetContext)
19
- GOVUKDesignSystemFormBuilder::Containers::RadioButtonsFieldset.include(Containers::FieldsetContext)
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Katalyst
4
- module GOVUK
5
- module FormBuilder
6
- module Frontend
7
- # rubocop:disable Rails/OutputSafety
8
- def govuk_formbuilder_init
9
- tag.script type: "module", nonce: request.content_security_policy_nonce do
10
- <<~JS.html_safe
11
- document.body.classList.toggle("js-enabled", true);
12
- document.body.classList.toggle("govuk-frontend-supported", ('noModule' in HTMLScriptElement.prototype));
13
- import {initAll} from "@katalyst/govuk-formbuilder";
14
- initAll();
15
- JS
16
- end
17
- end
18
- # rubocop:enable Rails/OutputSafety
19
- end
20
- end
21
- end
22
- end
@@ -1,104 +0,0 @@
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
- module File
10
- extend ActiveSupport::Concern
11
-
12
- protected
13
-
14
- def stimulus_controller_actions
15
- <<~ACTIONS.gsub(/\s+/, " ").freeze
16
- dragover->#{stimulus_controller}#dragover
17
- dragenter->#{stimulus_controller}#dragenter
18
- dragleave->#{stimulus_controller}#dragleave
19
- drop->#{stimulus_controller}#drop
20
- ACTIONS
21
- end
22
-
23
- def file
24
- previous_input = @builder.hidden_field(@attribute_name, id: nil, value: value.signed_id) if attached?
25
- file_input = @builder.file_field(@attribute_name, attributes(@html_attributes))
26
-
27
- safe_join([previous_input, file_input])
28
- end
29
-
30
- def destroy_element
31
- return if @html_attributes[:optional].blank?
32
-
33
- @builder.fields_for(:"#{@attribute_name}_attachment") do |form|
34
- form.hidden_field :_destroy, value: false, data: { "#{stimulus_controller}_target" => "destroy" }
35
- end
36
- end
37
-
38
- def destroy_element_trigger
39
- return if @html_attributes[:optional].blank?
40
-
41
- content_tag(:button, "", class: "file-destroy", data: { action: "#{stimulus_controller}#setDestroy" })
42
- end
43
-
44
- def preview_url
45
- preview? ? main_app.polymorphic_path(value, only_path: true) : ""
46
- end
47
-
48
- def preview?
49
- attached?
50
- end
51
-
52
- def attached?
53
- value&.attached? && value.blob.persisted?
54
- end
55
-
56
- def value
57
- @builder.object.send(@attribute_name)
58
- end
59
-
60
- def file_input_options
61
- default_file_input_options = options
62
-
63
- add_option(default_file_input_options, :accept, @mime_types.join(","))
64
- add_option(default_file_input_options, :data, :action, "change->#{stimulus_controller}#onUpload")
65
-
66
- default_file_input_options
67
- end
68
-
69
- def options
70
- {
71
- id: field_id(link_errors: true),
72
- class: classes,
73
- aria: { describedby: combine_references(hint_id, error_id) },
74
- }
75
- end
76
-
77
- using GOVUKDesignSystemFormBuilder::PrefixableArray
78
-
79
- def classes
80
- build_classes(%(file-upload), %(file-upload--error) => has_errors?).prefix(brand)
81
- end
82
-
83
- def default_form_group_options(**form_group_options)
84
- add_option(form_group_options, :class, "govuk-form-group #{form_group_class}")
85
- add_option(form_group_options, :data, :controller, stimulus_controller)
86
- add_option(form_group_options, :data, :action, stimulus_controller_actions)
87
- add_option(form_group_options, :data, :"#{stimulus_controller}_mime_types_value",
88
- @mime_types.to_json)
89
-
90
- form_group_options
91
- end
92
-
93
- def add_option(options, key, *path)
94
- if path.length > 1
95
- add_option(options[key] ||= {}, *path)
96
- else
97
- options[key] = [options[key], *path].compact.join(" ")
98
- end
99
- end
100
- end
101
- end
102
- end
103
- end
104
- end
@@ -1,33 +0,0 @@
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.any? ? {} : { 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