Skip to content

Commit

Permalink
Ensure check box and radio button labels have the correct for attribu…
Browse files Browse the repository at this point in the history
…te when using a custom id
  • Loading branch information
spohlenz committed Dec 4, 2024
1 parent 0e112ec commit b01d143
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/trestle/form/fields/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion lib/trestle/form/fields/radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/trestle/form/fields/check_box_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" } }

Expand Down
12 changes: 12 additions & 0 deletions spec/trestle/form/fields/radio_button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" } }

Expand Down

0 comments on commit b01d143

Please sign in to comment.