From b01d143e5d53dade0b386a479049801c53a0d585 Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Wed, 4 Dec 2024 19:49:48 +1030 Subject: [PATCH] Ensure check box and radio button labels have the correct for attribute when using a custom id --- lib/trestle/form/fields/check_box.rb | 14 +++++++++++++- lib/trestle/form/fields/radio_button.rb | 14 +++++++++++++- spec/trestle/form/fields/check_box_spec.rb | 12 ++++++++++++ spec/trestle/form/fields/radio_button_spec.rb | 12 ++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/trestle/form/fields/check_box.rb b/lib/trestle/form/fields/check_box.rb index 134b7990..988cea23 100644 --- a/lib/trestle/form/fields/check_box.rb +++ b/lib/trestle/form/fields/check_box.rb @@ -22,11 +22,23 @@ def field tag.div(class: wrapper_class) do safe_join([ builder.raw_check_box(name, options.merge(class: input_class), checked_value, unchecked_value), - builder.label(name, options[:label] || admin.human_attribute_name(name), class: label_class, value: (checked_value if options[:multiple])) + builder.label(name, label, label_options) ]) end end + def label + options[:label] || admin.human_attribute_name(name) + end + + def label_options + { + class: label_class, + value: (checked_value if options[:multiple]), + for: options[:id] + }.compact + end + def extract_wrapper_options! # Intentional no-op end diff --git a/lib/trestle/form/fields/radio_button.rb b/lib/trestle/form/fields/radio_button.rb index e8e402c1..ad8a5331 100644 --- a/lib/trestle/form/fields/radio_button.rb +++ b/lib/trestle/form/fields/radio_button.rb @@ -23,11 +23,23 @@ def field tag.div(class: wrapper_class) do safe_join([ builder.raw_radio_button(name, tag_value, options.merge(class: input_class)), - builder.label(name, options[:label] || tag_value.to_s.humanize, value: tag_value, class: label_class) + builder.label(name, label, label_options) ]) end end + def label + options[:label] || tag_value.to_s.humanize + end + + def label_options + { + class: label_class, + value: tag_value, + for: options[:id] + }.compact + end + def extract_wrapper_options! # Intentional no-op end diff --git a/spec/trestle/form/fields/check_box_spec.rb b/spec/trestle/form/fields/check_box_spec.rb index dc0f6ccd..ed325be5 100644 --- a/spec/trestle/form/fields/check_box_spec.rb +++ b/spec/trestle/form/fields/check_box_spec.rb @@ -39,6 +39,18 @@ end end + context "when options[:id] is specified" do + let(:options) { { id: "custom-id" } } + + it "overrides the id attribute on the input element" do + expect(subject).to have_tag("input.form-check-input", with: { id: "custom-id" }) + end + + it "overrides the for attribute on the label element" do + expect(subject).to have_tag("label", with: { for: "custom-id" }) + end + end + context "when options[:label] is specified" do let(:options) { { label: "Custom Label" } } diff --git a/spec/trestle/form/fields/radio_button_spec.rb b/spec/trestle/form/fields/radio_button_spec.rb index f0c139bb..3a767984 100644 --- a/spec/trestle/form/fields/radio_button_spec.rb +++ b/spec/trestle/form/fields/radio_button_spec.rb @@ -25,6 +25,18 @@ end end + context "when options[:id] is specified" do + let(:options) { { id: "custom-id" } } + + it "overrides the id attribute on the input element" do + expect(subject).to have_tag("input.form-check-input", with: { id: "custom-id" }) + end + + it "overrides the for attribute on the label element" do + expect(subject).to have_tag("label", with: { for: "custom-id" }) + end + end + context "when options[:label] is specified" do let(:options) { { label: "Custom Label" } }