Skip to content

Commit b1d9f70

Browse files
committed
Use role="group" instead of fieldset
1 parent c6c46b3 commit b1d9f70

11 files changed

Lines changed: 93 additions & 88 deletions

File tree

.yarnrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# yarn lockfile v1
33

44

5-
lastUpdateCheck 1763690055251
5+
lastUpdateCheck 1763854595198

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ This generates the following HTML:
120120
</form>
121121
```
122122

123-
Note: All examples in this README are generated with the configuration option `fieldset_around_collections` set to `true`. See the [Configuration](#configuration) section.
123+
Note: All examples in this README are generated with the configuration option `group_around_collections` set to `true`. See the [Configuration](#configuration) section.
124124

125125
### bootstrap_form_tag
126126

@@ -235,7 +235,7 @@ The current configuration options are:
235235
| Option | Default value | Description |
236236
|---------------------------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
237237
| `default_form_attributes` | {} | `bootstrap_form` versions 3 and 4 added a role="form" attribute to all forms. The W3C validator will raise a **warning** on forms with a role="form" attribute. `bootstrap_form` version 5 drops this attribute by default. Set this option to `{ role: "form" }` to make forms non-compliant with W3C, but generate the `role="form"` attribute like `bootstrap_form` versions 3 and 4. |
238-
| `fieldset_around_collections` | false | Historically, `bootstrap_form` generated a wrapper around `collection_checkboxes` and `collection_radio_buttons` using the same `form_group` as individual controls used. This markup caused accessibility problems. Setting `fieldset_around_collections = true` will generate collections of checkboxes and radio buttons wrapper in a `<fieldset>` with the text as a `<legend>` (https://www.w3.org/WAI/tutorials/forms/grouping/). This _will_ make visible changes to pages that use the collection methods.<br/><br/>The default for this option will be changed to `true` in a future version. |
238+
| `group_around_collections` | false | Historically, `bootstrap_form` generated a wrapper around `collection_checkboxes` and `collection_radio_buttons` using the same `form_group` as individual controls used. This markup caused accessibility problems. Setting `group_around_collections = true` will generate collections of checkboxes and radio buttons wrapper in a `<fieldset>` with the text as a `<legend>` (https://www.w3.org/WAI/tutorials/forms/grouping/). This _will_ make visible changes to pages that use the collection methods.<br/><br/>The default for this option will be changed to `true` in a future version. |
239239

240240
Example:
241241

@@ -784,8 +784,8 @@ This generates:
784784
This generates:
785785

786786
```html
787-
<fieldset class="mb-3">
788-
<legend class="form-label">Skill level</legend>
787+
<div aria-labelledby="user_misc_label" class="mb-3" role="group">
788+
<div class="form-label" id="user_misc_label">Skill level</div>
789789
<div class="form-check">
790790
<input class="form-check-input" id="user_skill_level_1" name="user[skill_level]" type="radio" value="1">
791791
<label class="form-check-label" for="user_skill_level_1">Mind reading</label>
@@ -794,10 +794,10 @@ This generates:
794794
<input class="form-check-input" id="user_skill_level_2" name="user[skill_level]" type="radio" value="2">
795795
<label class="form-check-label" for="user_skill_level_2">Farming</label>
796796
</div>
797-
</fieldset>
797+
</div>
798798
<input id="user_skills" name="user[skills][]" type="hidden" value="">
799-
<fieldset class="mb-3">
800-
<legend class="form-label">Skills</legend>
799+
<div aria-labelledby="user_misc_label" class="mb-3" role="group">
800+
<div class="form-label" id="user_misc_label">Skills</div>
801801
<div class="form-check">
802802
<input class="form-check-input" id="user_skills_1" name="user[skills][]" type="checkbox" value="1">
803803
<label class="form-check-label" for="user_skills_1">Mind reading</label>
@@ -806,7 +806,7 @@ This generates:
806806
<input class="form-check-input" id="user_skills_2" name="user[skills][]" type="checkbox" value="2">
807807
<label class="form-check-label" for="user_skills_2">Farming</label>
808808
</div>
809-
</fieldset>
809+
</div>
810810
```
811811

812812
NOTE: These helpers do not currently support a block, unlike their equivalent Rails helpers. See issue [#477](https://github.com/bootstrap-ruby/bootstrap_form/issues/477). If you need to use the block syntax, use `collection_check_boxes_without_bootstrap` or `collection_radio_buttons_without_bootstrap` for now.
@@ -832,8 +832,8 @@ To add `data-` attributes to a collection of radio buttons, map your models to a
832832
This generates:
833833

834834
```html
835-
<fieldset class="mb-3">
836-
<legend class="form-label">Misc</legend>
835+
<div aria-labelledby="user_misc_label" class="mb-3" role="group">
836+
<div class="form-label" id="user_misc_label">Misc</div>
837837
<div class="form-check">
838838
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1">
839839
<label class="form-check-label" for="user_misc_1">Foo</label>
@@ -842,7 +842,7 @@ This generates:
842842
<input class="form-check-input" id="user_misc_2" name="user[misc]" type="radio" value="2">
843843
<label class="form-check-label" for="user_misc_2">Bar</label>
844844
</div>
845-
</fieldset>
845+
</div>
846846
```
847847

848848
## Range Controls
@@ -1496,8 +1496,8 @@ Generated HTML:
14961496
<input class="form-control is-invalid" id="user_email" name="user[email]" required="required" type="email" value="steve.example.com">
14971497
<div class="invalid-feedback">is invalid</div>
14981498
</div>
1499-
<fieldset class="mb-3">
1500-
<legend class="form-label">Misc</legend>
1499+
<div aria-labelledby="user_misc_label" class="mb-3" role="group">
1500+
<div class="form-label" id="user_misc_label">Misc</div>
15011501
<div class="form-check">
15021502
<input checked class="form-check-input is-invalid" id="user_misc_1" name="user[misc]" type="radio" value="1">
15031503
<label class="form-check-label" for="user_misc_1">Mind reading</label>
@@ -1507,10 +1507,10 @@ Generated HTML:
15071507
<label class="form-check-label" for="user_misc_2">Farming</label>
15081508
<div class="invalid-feedback">is invalid</div>
15091509
</div>
1510-
</fieldset>
1510+
</div>
15111511
<input id="user_preferences" name="user[preferences][]" type="hidden" value="">
1512-
<fieldset class="mb-3">
1513-
<legend class="form-label">Preferences</legend>
1512+
<div aria-labelledby="user_misc_label" class="mb-3" role="group">
1513+
<div class="form-label" id="user_misc_label">Preferences</div>
15141514
<div class="form-check">
15151515
<input checked class="form-check-input is-invalid" id="user_preferences_1" name="user[preferences][]" type="checkbox" value="1">
15161516
<label class="form-check-label" for="user_preferences_1">Good</label>
@@ -1520,7 +1520,7 @@ Generated HTML:
15201520
<label class="form-check-label" for="user_preferences_2">Bad</label>
15211521
<div class="invalid-feedback">is invalid</div>
15221522
</div>
1523-
</fieldset>
1523+
</div>
15241524
<div class="mb-3">
15251525
<label class="form-label" for="user_address_attributes_street">Street</label>
15261526
<input class="form-control is-invalid" id="user_address_attributes_street" name="user[address_attributes][street]" type="text" value="Bar">
-1.02 KB
Loading
-452 Bytes
Loading
-1.01 KB
Loading
-452 Bytes
Loading
-1.34 KB
Loading

demo/test/system/bootstrap_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
class BootstrapTest < ApplicationSystemTestCase
77
setup do
88
screenshot_section :bootstrap
9-
Rails.application.config.bootstrap_form.fieldset_around_collections = true
9+
Rails.application.config.bootstrap_form.group_around_collections = true
1010
end
1111

1212
teardown do
13-
Rails.application.config.bootstrap_form.fieldset_around_collections = false
13+
Rails.application.config.bootstrap_form.group_around_collections = false
1414
end
1515

1616
test "visiting the index" do

lib/bootstrap_form/engine.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ class Engine < Rails::Engine
99

1010
config.bootstrap_form = BootstrapForm.config
1111
config.bootstrap_form.default_form_attributes ||= {}
12-
if config.bootstrap_form.fieldset_around_collections.nil?
13-
config.bootstrap_form.fieldset_around_collections = Rails.env.development?
14-
end
12+
config.bootstrap_form.group_around_collections = Rails.env.development? if config.bootstrap_form.group_around_collections.nil?
1513

1614
initializer "bootstrap_form.configure" do |app|
1715
BootstrapForm.config = app.config.bootstrap_form

lib/bootstrap_form/inputs/inputs_collection.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module InputsCollection
88
private
99

1010
def inputs_collection(name, collection, value, text, options={}, &)
11-
if BootstrapForm.config.fieldset_around_collections
11+
if BootstrapForm.config.group_around_collections
1212
return fieldset_inputs_collection(name, collection, value, text, options, &)
1313
end
1414

@@ -93,27 +93,34 @@ def fieldset_builder(method, options, html_options=nil, &)
9393
if no_wrapper
9494
yield
9595
else
96-
fieldset(method, form_group_options, &)
96+
field_group(method, form_group_options, &)
9797
end
9898
end
9999

100-
def fieldset(name, options, &)
100+
def field_group(name, options, &)
101101
options[:class] = form_group_classes(options)
102102

103-
tag.fieldset(**options.except(
104-
:add_control_col_class, :append, :control_col, :floating, :help, :icon, :id,
105-
:input_group_class, :label, :label_col, :layout, :prepend
106-
)) do
103+
tag.div(
104+
**options.except(
105+
:add_control_col_class, :append, :control_col, :floating, :help, :icon, :id,
106+
:input_group_class, :label, :label_col, :layout, :prepend
107+
),
108+
aria: { labelledby: :user_misc_label },
109+
role: :group
110+
) do
107111
legend = generate_legend(name, options)
108112
prepare_label_options(options[:id], name, options[:label], options[:label_col], options[:layout])
109113
form_group_content(legend, generate_help(name, options[:help]), options, &)
110114
end
111115
end
112116

113117
def generate_legend(name, options)
114-
legend_class = group_label_class(options[:layout])
118+
legend_class = group_label_class(field_layout(options)) || "form-label"
115119

116-
tag.legend(**{ class: legend_class }.compact) { label_text(name, options.dig(:label, :text)) }
120+
tag.div(
121+
**{ class: legend_class }.compact,
122+
id: "user_misc_label" # FIXME: This can't be hard-coded, obviously.
123+
) { label_text(name, options.dig(:label, :text)) }
117124
end
118125
end
119126
end

0 commit comments

Comments
 (0)