Skip to content

Commit

Permalink
Make sure labels and form inputs are properly associated when a custo…
Browse files Browse the repository at this point in the history
…m ID is provided (#2666)
  • Loading branch information
camertron authored Mar 8, 2024
1 parent 66aa978 commit 53a3390
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-singers-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/view-components": patch
---

Make sure labels and form inputs are properly associated when a custom ID is provided
6 changes: 6 additions & 0 deletions lib/primer/forms/dsl/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def initialize(builder:, form:, **system_arguments)
@form = form

@input_arguments = system_arguments
@input_arguments.delete(:id) unless @input_arguments[:id].present?
@label_arguments = @input_arguments.delete(:label_arguments) || {}
@label_arguments[:for] = id if id.present?

@label_arguments[:class] = class_names(
@label_arguments[:class],
Expand Down Expand Up @@ -240,6 +242,10 @@ def autofocus!
input_arguments[:autofocus] = true
end

def id
@input_arguments[:id]
end

# :nocov:
def name
raise_for_abstract_method!(__method__)
Expand Down
13 changes: 13 additions & 0 deletions test/lib/primer/forms/form_control_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ def test_auto_check_generates_validation_elements
# there are validation-related elements, but they're hidden
assert_selector(".FormControl-inlineValidation", visible: false)
end

def test_labels_use_custom_ids_when_provided
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render_inline_form(f) do |test_form|
test_form.text_field(name: :foobar, id: "bazboo", label: "Foos and bars y'all")
end
end
end

assert_selector "input[id=bazboo]"
assert_selector "label[for=bazboo]"
end
end

0 comments on commit 53a3390

Please sign in to comment.