Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/chatty-pants-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Fix incorrect label `for` attribute value when `scope_id_false: false`
3 changes: 2 additions & 1 deletion app/lib/primer/forms/dsl/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def initialize(builder:, form:, **system_arguments)
@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 @@ -102,6 +101,8 @@ def initialize(builder:, form:, **system_arguments)
end
# rubocop:enable Style/IfUnlessModifier

@label_arguments[:for] = @input_arguments[:id]

# Whether or not to wrap the component in a FormControl, which renders a
# label above and validation message beneath the input.
@form_control = @input_arguments.delete(:form_control) { true }
Expand Down
56 changes: 56 additions & 0 deletions test/lib/primer/forms/input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,34 @@ def test_removes_model_scope_from_name_and_id
end

assert_selector "input#ultimate_answer[name=ultimate_answer]"
assert_selector "label[for=ultimate_answer]"
end

def test_removes_model_scope_from_name_and_id_using_namespace
model = DeepThought.new(42)

render_in_view_context do
primer_form_with(model: model, url: "/foo", namespace: "baz") do |f|
render(NoModelScopeForm.new(f))
end
end

assert_selector "input#baz_ultimate_answer[name=ultimate_answer]"
end

def test_label_removes_model_scope_from_name_and_id_using_namespace
# See upstream issue: https://github.com/rails/rails/pull/51237
skip "Broken in Rails versions prior to 8.0.3" unless Rails::VERSION::STRING >= "8.0.3"

model = DeepThought.new(42)

render_in_view_context do
primer_form_with(model: model, url: "/foo", namespace: "baz") do |f|
render(NoModelScopeForm.new(f))
end
end

assert_selector "label[for=baz_ultimate_answer]"
end

def test_uses_given_id
Expand All @@ -116,5 +144,33 @@ def test_uses_given_id
end

assert_selector "input#foobar[name=ultimate_answer]"
assert_selector "label[for=foobar]"
end

def test_uses_given_id_and_namespace
model = DeepThought.new(42)

render_in_view_context do
primer_form_with(model: model, url: "/foo", namespace: "baz") do |f|
render(NoModelScopeForm.new(f, id: "foobar"))
end
end

assert_selector "input#baz_foobar[name=ultimate_answer]"
end

def test_label_uses_given_id_and_namespace
# See upstream issue: https://github.com/rails/rails/pull/51237
skip "Broken in Rails versions prior to 8.0.3" unless Rails::VERSION::STRING >= "8.0.3"

model = DeepThought.new(42)

render_in_view_context do
primer_form_with(model: model, url: "/foo", namespace: "baz") do |f|
render(NoModelScopeForm.new(f, id: "foobar"))
end
end

assert_selector "label[for=baz_foobar]"
end
end