Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure check box and radio button labels have the correct for attribute when using a custom id #530

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading