sdr_view_components 0.16.0 → 0.18.0

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: 27f0afaee126ae7798ad6211bc3b046399a06e6d0aa740ba5c90c8e1b8df5c0c
4
- data.tar.gz: f1dbf12e5241901b8819d994b3d87019fa7366f39d2b0aa682690e44b7b7f64a
3
+ metadata.gz: 5f59b6686400993be3c942c3f2b7c090fdb94c2e3f9aa7f5a6b83ba2818ec8dd
4
+ data.tar.gz: 7dea52d93f22c5aaca0e81dbd98abf50e12185cdffbd8a720b4dfc20801e5774
5
5
  SHA512:
6
- metadata.gz: d90ae68f1ef168082dea45078aea2c4af8803f133ed84054788005b32eb40fbca06f986e3cc21990c42f06949d7486a43a23d8e4a08821b21e72e9baa5dcba1c
7
- data.tar.gz: 292ded03a7947c21657fdc312750827587a4ee1607b07867d6ea9b4a9a7e557bf6916e6d8e66f8e6a026491cf45924f9217d5fa39c2bfcd7bf383c81b28b42f0
6
+ metadata.gz: 7b5e0f8baac5f81b0fac04d9e44473c37e940def8cabe7f37625d84c840c8888d7a9fc7441b74d2c710c7bc621917577d6df96b5030ededaa3133f0671578553
7
+ data.tar.gz: 0d05f092127a49264b7418bb582722c15d246e3e2beb5dd1b95ecff4affc5ca19590aa800f5821a46884a27dac4112f59b2a48ce82f69fafb8dae5eaf2d48c16
data/README.md CHANGED
@@ -127,6 +127,25 @@ At a minimum, each of these components must be provided wih the `form:` and `fie
127
127
  - `help_`: is used to pass arguments to the help text available for the component.
128
128
  - `error_`: is used to pass arguments to the validation output of the component.
129
129
 
130
+ An `input_aria:` hash is merged with the arias the component sets itself (`aria-invalid`, `aria-required`), and its `describedby` is added to the component's error and help text ids rather than replacing them. The component's own arias take precedence, so `input_aria:` can add attributes but can't override the field's error or required state.
131
+
132
+ #### Radio button groups
133
+
134
+ Use `SdrViewComponents::Forms::RadioButtonGroupComponent` for a set of radio buttons that share a field. It renders the radio buttons in a `FieldsetComponent` and renders the field's validation error once for the group, rather than under each radio button. Each radio button gets `aria-invalid` and an `aria-describedby` pointing at that error. Arguments other than `form:` and `field_name:` (`label:`, `tooltip:`, `mark_required:`, `help_text:`, etc.) are passed to the `FieldsetComponent`; arguments to `with_radio_button` are passed to `RadioButtonComponent`.
135
+
136
+ A block given to `with_radio_button` is rendered as that radio button's additional container content, for example a field that applies only when that option is chosen.
137
+
138
+ ```erb
139
+ <%= render SdrViewComponents::Forms::RadioButtonGroupComponent.new(form:, field_name: :delivery_method, label: 'Delivery method') do |group| %>
140
+ <% group.with_radio_button(input_value: 'email', label_text: 'Email') do %>
141
+ <%= render SdrViewComponents::Forms::TextFieldComponent.new(form:, field_name: :email_address, label_text: 'Email address') %>
142
+ <% end %>
143
+ <% group.with_radio_button(input_value: 'mail', label_text: 'Mail') %>
144
+ <% end %>
145
+ ```
146
+
147
+ For layouts the group component doesn't support (for example, showing the error next to one particular radio button), pass `show_errors: false` to each composed component to skip its validation error, and render the error once yourself with `SdrViewComponents::Forms::InvalidFeedbackComponent.new(form:, field_name:)`. The inputs still point their `aria-describedby` at the error's id, so make sure it is rendered.
148
+
130
149
  TODO:
131
150
  - Outline the supported params for each prefix
132
151
  - Provide an example instantiation and output
@@ -209,6 +228,17 @@ See the Lookbook preview for `SdrViewComponents::TabForm::TabListComponent` for
209
228
  <% render SdrViewComponent::....>
210
229
  ```
211
230
 
231
+ ### Collapsible section component
232
+
233
+ `SdrViewComponents::Elements::CollapsibleSectionComponent` renders a heading that shows / hides the section content using Bootstrap's collapse. The `id:` is used for the collapsed content and must be unique on the page. It is expanded unless `expanded: false` is given.
234
+
235
+ ```erb
236
+ <%= render SdrViewComponents::Elements::CollapsibleSectionComponent.new(id: 'pages', heading: 'Pages (12)', level: :h3,
237
+ heading_classes: 'text-uppercase') do %>
238
+ <p>Section content.</p>
239
+ <% end %>
240
+ ```
241
+
212
242
  ### Navigation components
213
243
 
214
244
  SdrViewComponents provides components for building navigation bar items and dropdown menus:
@@ -176,6 +176,27 @@ fieldset.form-fieldset legend {
176
176
  padding-bottom: 80px;
177
177
  }
178
178
 
179
+ /* Collapsible section */
180
+ .collapsible-section-toggle {
181
+ display: inline-flex;
182
+ align-items: center;
183
+ gap: 0.5rem;
184
+ padding: 0;
185
+ border: 0;
186
+ background: none;
187
+ color: inherit;
188
+ font: inherit;
189
+ text-transform: inherit;
190
+ }
191
+
192
+ .collapsible-section-icon {
193
+ transition: transform 0.2s ease-in-out;
194
+ }
195
+
196
+ .collapsible-section-toggle.collapsed .collapsible-section-icon {
197
+ transform: rotate(180deg);
198
+ }
199
+
179
200
  /* Tables */
180
201
  table.table-h3 caption {
181
202
  padding: .25rem;
@@ -0,0 +1,11 @@
1
+ <%= tag.section class: classes do %>
2
+ <%= render SdrViewComponents::Elements::HeadingComponent.new(level:, classes: heading_classes) do %>
3
+ <%= tag.button type: 'button', class: toggle_classes, data: { bs_toggle: 'collapse', bs_target: "##{id}" }, aria: { expanded:, controls: id } do %>
4
+ <%= helpers.icon(icon_classes: 'bi bi-chevron-up', classes: 'collapsible-section-icon', aria: { hidden: true }) %>
5
+ <%= heading %>
6
+ <% end %>
7
+ <% end %>
8
+ <%= tag.div id:, class: collapse_classes do %>
9
+ <%= content %>
10
+ <% end %>
11
+ <% end %>
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Elements
5
+ # Component for a section whose heading toggles (Bootstrap collapse) the display of its content.
6
+ class CollapsibleSectionComponent < BaseComponent
7
+ def initialize(id:, heading:, level: :h2, expanded: true, classes: [], heading_classes: []) # rubocop:disable Metrics/ParameterLists
8
+ @id = id
9
+ @heading = heading
10
+ @level = level
11
+ @expanded = expanded
12
+ @classes = classes
13
+ @heading_classes = heading_classes
14
+ super()
15
+ end
16
+
17
+ attr_reader :id, :heading, :level, :expanded, :heading_classes
18
+
19
+ def classes
20
+ merge_classes('collapsible-section', @classes)
21
+ end
22
+
23
+ def toggle_classes
24
+ merge_classes('collapsible-section-toggle', expanded ? nil : 'collapsed')
25
+ end
26
+
27
+ def collapse_classes
28
+ merge_classes('collapse', expanded ? 'show' : nil)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -2,7 +2,7 @@
2
2
  <%= render SdrViewComponents::Forms::LabelComponent.new(form:, field_name: label_field_name, **label_args) %>
3
3
  <%= render SdrViewComponents::Forms::HelpTextComponent.new(**help_text_args) unless help_text_below? %>
4
4
  <%= render input_component %>
5
- <%= render SdrViewComponents::Forms::InvalidFeedbackComponent.new(form:, field_name:, **error_args) %>
5
+ <%= render SdrViewComponents::Forms::InvalidFeedbackComponent.new(form:, field_name:, **error_args) if show_errors? %>
6
6
  <%= render SdrViewComponents::Forms::HelpTextComponent.new(**help_text_args) if help_text_below? %>
7
7
 
8
8
  <%= additional_container_content %>
@@ -6,7 +6,10 @@ module SdrViewComponents
6
6
  class FieldComponent < BaseComponent
7
7
  renders_one :additional_container_content
8
8
 
9
- def initialize(form:, field_name:, container_classes: [], variant: :default, **args)
9
+ # @param show_errors [Boolean] Whether to render the field's invalid feedback. Set to false when several
10
+ # inputs share a field (e.g. a radio button group) and the error is rendered once elsewhere. The input
11
+ # still gets aria-invalid and an aria-describedby pointing at the field's error id.
12
+ def initialize(form:, field_name:, container_classes: [], variant: :default, show_errors: true, **args) # rubocop:disable Metrics/ParameterLists
10
13
  @form = form
11
14
  @field_name = field_name
12
15
  @container_classes = container_classes
@@ -15,6 +18,7 @@ module SdrViewComponents
15
18
  # This is necessary for some types of fields such as checkboxes.
16
19
  raise ArgumentError, 'invalid variant' unless %i[default help_text_below].include?(@variant)
17
20
 
21
+ @show_errors = show_errors
18
22
  @args = args
19
23
  super()
20
24
  end
@@ -57,8 +61,13 @@ module SdrViewComponents
57
61
  # Returns a hash of arguments for the input element.
58
62
  # Prefix and argument with 'input_' in order for it to be passed to
59
63
  # the input element.
64
+ # A caller-supplied :input_aria is merged with the field's arias, with
65
+ # describedby values joined. The field's own arias (e.g. invalid, required)
66
+ # take precedence, so a caller can't override the field's error state.
60
67
  def input_args
61
- args_for(args:, prefix: 'input_').merge({ aria: field_aria })
68
+ args_for(args:, prefix: 'input_').then do |input|
69
+ input.merge({ aria: merge_arias(field_aria, input[:aria]) })
70
+ end
62
71
  end
63
72
 
64
73
  # Returns a hash of arguments for the label element.
@@ -80,10 +89,22 @@ module SdrViewComponents
80
89
  def help_text_below?
81
90
  variant == :help_text_below
82
91
  end
92
+
93
+ def show_errors?
94
+ @show_errors
95
+ end
83
96
  ##########################################################################
84
97
 
85
98
  private
86
99
 
100
+ def merge_arias(arias, other_arias)
101
+ return arias if other_arias.blank?
102
+
103
+ arias.merge(other_arias.symbolize_keys) do |key, value, other_value|
104
+ key == :describedby ? merge_actions(value, other_value) : value
105
+ end
106
+ end
107
+
87
108
  def error_aria
88
109
  InvalidFeedbackSupport.arias_for(field_name:, form:).tap do |arias|
89
110
  arias[:describedby] = merge_actions(arias[:describedby], help_text_id) if help_text_args[:text].present?
@@ -0,0 +1,6 @@
1
+ <%= render SdrViewComponents::Forms::FieldsetComponent.new(**fieldset_args) do %>
2
+ <% radio_buttons.each do |radio_button| %>
3
+ <%= radio_button %>
4
+ <% end %>
5
+ <%= render SdrViewComponents::Forms::InvalidFeedbackComponent.new(form:, field_name:) %>
6
+ <% end %>
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ # Component for a group of radio buttons that share a field.
6
+ # The field's error is rendered once for the group rather than under each radio button.
7
+ class RadioButtonGroupComponent < BaseComponent
8
+ # A block given to with_radio_button is rendered as the radio button's additional container content.
9
+ renders_many :radio_buttons, lambda { |**args, &block|
10
+ Forms::RadioButtonComponent.new(form:, field_name:, show_errors: false, **args).tap do |radio_button|
11
+ radio_button.with_additional_container_content(&block) if block
12
+ end
13
+ }
14
+
15
+ # @param fieldset_args [Hash] Passed to FieldsetComponent (e.g. label:, tooltip:, mark_required:, help_text:).
16
+ def initialize(form:, field_name:, **fieldset_args)
17
+ @form = form
18
+ @field_name = field_name
19
+ @fieldset_args = fieldset_args
20
+ super()
21
+ end
22
+
23
+ attr_reader :form, :field_name, :fieldset_args
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SdrViewComponents
4
- VERSION = '0.16.0'
4
+ VERSION = '0.18.0'
5
5
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Elements
5
+ class CollapsibleSectionComponentPreview < ViewComponent::Preview
6
+ def expanded
7
+ render SdrViewComponents::Elements::CollapsibleSectionComponent.new(id: 'expanded-section',
8
+ heading: 'Pages (2)',
9
+ level: :h3,
10
+ heading_classes: 'text-uppercase') do
11
+ 'Section content goes here.'
12
+ end
13
+ end
14
+
15
+ def collapsed
16
+ render SdrViewComponents::Elements::CollapsibleSectionComponent.new(id: 'collapsed-section',
17
+ heading: 'Supplemental files (1)',
18
+ level: :h3,
19
+ expanded: false) do
20
+ 'Section content goes here.'
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ <% model = SdrViewComponents::Forms::RadioButtonGroupComponentPreview::RadioButtonGroupPreviewModel.new(delivery_method: 'email') %>
2
+
3
+ <%= form_with(model: model, url: '/example') do |form| %>
4
+ <%= render SdrViewComponents::Forms::RadioButtonGroupComponent.new(
5
+ form: form,
6
+ field_name: :delivery_method,
7
+ label: 'Delivery method'
8
+ ) do |group| %>
9
+ <% group.with_radio_button(input_value: 'email', label_text: 'Email') %>
10
+ <% group.with_radio_button(input_value: 'mail', label_text: 'Mail') %>
11
+ <% end %>
12
+ <% end %>
@@ -0,0 +1,18 @@
1
+ <% model = SdrViewComponents::Forms::RadioButtonGroupComponentPreview::RadioButtonGroupPreviewModel.new(delivery_method: 'email') %>
2
+
3
+ <%= form_with(model: model, url: '/example') do |form| %>
4
+ <%= render SdrViewComponents::Forms::RadioButtonGroupComponent.new(
5
+ form: form,
6
+ field_name: :delivery_method,
7
+ label: 'Delivery method'
8
+ ) do |group| %>
9
+ <% group.with_radio_button(input_value: 'email', label_text: 'Email') do %>
10
+ <%= render SdrViewComponents::Forms::TextFieldComponent.new(
11
+ form: form,
12
+ field_name: :email_address,
13
+ label_text: 'Email address'
14
+ ) %>
15
+ <% end %>
16
+ <% group.with_radio_button(input_value: 'mail', label_text: 'Mail') %>
17
+ <% end %>
18
+ <% end %>
@@ -0,0 +1,13 @@
1
+ <% model = SdrViewComponents::Forms::RadioButtonGroupComponentPreview::RadioButtonGroupPreviewModel.new(delivery_method: nil) %>
2
+ <% model.valid? %>
3
+
4
+ <%= form_with(model: model, url: '/example') do |form| %>
5
+ <%= render SdrViewComponents::Forms::RadioButtonGroupComponent.new(
6
+ form: form,
7
+ field_name: :delivery_method,
8
+ label: 'Delivery method'
9
+ ) do |group| %>
10
+ <% group.with_radio_button(input_value: 'email', label_text: 'Email') %>
11
+ <% group.with_radio_button(input_value: 'mail', label_text: 'Mail') %>
12
+ <% end %>
13
+ <% end %>
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ class RadioButtonGroupComponentPreview < ViewComponent::Preview
6
+ def default; end
7
+
8
+ def with_validation_error; end
9
+
10
+ def with_nested_content; end
11
+
12
+ class RadioButtonGroupPreviewModel
13
+ include ActiveModel::Model
14
+ include ActiveModel::Attributes
15
+
16
+ attribute :delivery_method, :string, default: nil
17
+ attribute :email_address, :string, default: nil
18
+
19
+ validates :delivery_method, presence: true
20
+ end
21
+ end
22
+ end
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdr_view_components
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Collier
@@ -78,6 +78,8 @@ files:
78
78
  - app/components/sdr_view_components/elements/button_link_component.rb
79
79
  - app/components/sdr_view_components/elements/card_component.html.erb
80
80
  - app/components/sdr_view_components/elements/card_component.rb
81
+ - app/components/sdr_view_components/elements/collapsible_section_component.html.erb
82
+ - app/components/sdr_view_components/elements/collapsible_section_component.rb
81
83
  - app/components/sdr_view_components/elements/heading_component.rb
82
84
  - app/components/sdr_view_components/elements/horizontal_rule_component.rb
83
85
  - app/components/sdr_view_components/elements/icon_button_component.html.erb
@@ -133,6 +135,8 @@ files:
133
135
  - app/components/sdr_view_components/forms/label_component.html.erb
134
136
  - app/components/sdr_view_components/forms/label_component.rb
135
137
  - app/components/sdr_view_components/forms/radio_button_component.rb
138
+ - app/components/sdr_view_components/forms/radio_button_group_component.html.erb
139
+ - app/components/sdr_view_components/forms/radio_button_group_component.rb
136
140
  - app/components/sdr_view_components/forms/select_field_component.rb
137
141
  - app/components/sdr_view_components/forms/submit_component.html.erb
138
142
  - app/components/sdr_view_components/forms/submit_component.rb
@@ -198,6 +202,7 @@ files:
198
202
  - spec/components/previews/sdr_view_components/elements/button_form_component_preview.rb
199
203
  - spec/components/previews/sdr_view_components/elements/button_link_component_preview.rb
200
204
  - spec/components/previews/sdr_view_components/elements/card_component_preview.rb
205
+ - spec/components/previews/sdr_view_components/elements/collapsible_section_component_preview.rb
201
206
  - spec/components/previews/sdr_view_components/elements/heading_component_preview.rb
202
207
  - spec/components/previews/sdr_view_components/elements/horizontal_rule_component_preview.rb
203
208
  - spec/components/previews/sdr_view_components/elements/horizontal_rule_component_preview/default.html.erb
@@ -290,6 +295,10 @@ files:
290
295
  - spec/components/previews/sdr_view_components/forms/radio_button_component_preview/with_help_text_below.html.erb
291
296
  - spec/components/previews/sdr_view_components/forms/radio_button_component_preview/with_label.html.erb
292
297
  - spec/components/previews/sdr_view_components/forms/radio_button_component_preview/with_validation_error.html.erb
298
+ - spec/components/previews/sdr_view_components/forms/radio_button_group_component_preview.rb
299
+ - spec/components/previews/sdr_view_components/forms/radio_button_group_component_preview/default.html.erb
300
+ - spec/components/previews/sdr_view_components/forms/radio_button_group_component_preview/with_nested_content.html.erb
301
+ - spec/components/previews/sdr_view_components/forms/radio_button_group_component_preview/with_validation_error.html.erb
293
302
  - spec/components/previews/sdr_view_components/forms/select_field_component_preview.rb
294
303
  - spec/components/previews/sdr_view_components/forms/select_field_component_preview/default.html.erb
295
304
  - spec/components/previews/sdr_view_components/forms/select_field_component_preview/with_additional_container_content.html.erb