Skip to content

Commit cc1ce7a

Browse files
authoredOct 15, 2024··
Fix caption templates (#3153)
1 parent 20ff94e commit cc1ce7a

9 files changed

+36
-11
lines changed
 

‎.changeset/heavy-camels-hear.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': patch
3+
---
4+
5+
Fix problem introduced by #3141 that causes template captions not to render for certain input types if the input is configured with a `value:`.

‎app/forms/caption_template_form.rb

+6
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ class CaptionTemplateForm < ApplicationForm
1818
age_radios.radio_button(value: "young", label: "10-15")
1919
age_radios.radio_button(value: "middle_aged", label: "16-21")
2020
end
21+
22+
name_form.check_box_group(name: "places", label: "Cool places") do |check_group|
23+
check_group.check_box(value: "lopez", label: "Lopez Island")
24+
check_group.check_box(value: "bellevue", label: "Bellevue")
25+
check_group.check_box(value: "seattle", label: "Seattle")
26+
end
2127
end
2228
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span>Bellevue caption</span>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span>Lopez caption</span>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span>Seattle caption</span>

‎app/lib/primer/forms/dsl/check_box_input.rb

+4-8
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,10 @@ def supports_validation?
5050
false
5151
end
5252

53-
private
54-
55-
def caption_template_name
56-
@caption_template_name ||= if @scheme == :array
57-
:"#{name}_#{value}"
58-
else
59-
name.to_sym
60-
end
53+
def values_disambiguate_template_names?
54+
# Check boxes submitted as an array all have the same name, so we return true here
55+
# to ensure different caption templates can be attached to individual check box inputs.
56+
@scheme == :array
6157
end
6258
end
6359
end

‎app/lib/primer/forms/dsl/input.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ def validation_error_icon_target
300300
""
301301
end
302302

303+
# Whether or not the `value:` argument should be used to determine the caption template
304+
# for a given field. This is useful in especially radio button groups where each option
305+
# has the same name but a different value. Check box groups where the values are submitted
306+
# as an array also use this feature, since each check box also has the same name.
307+
def values_disambiguate_template_names?
308+
false
309+
end
310+
303311
private
304312

305313
def input_data
@@ -309,7 +317,7 @@ def input_data
309317
def caption_template_name
310318
return nil unless name
311319

312-
@caption_template_name ||= if respond_to?(:value) && value.present?
320+
@caption_template_name ||= if values_disambiguate_template_names? && respond_to?(:value) && value.present?
313321
:"#{name}_#{value}"
314322
else
315323
name.to_sym

‎app/lib/primer/forms/dsl/radio_button_input.rb

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def type
4242
def supports_validation?
4343
false
4444
end
45+
46+
def values_disambiguate_template_names?
47+
true
48+
end
4549
end
4650
end
4751
end

‎test/lib/primer/forms_test.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ def test_renders_the_caption_template_when_present
6161
assert_selector ".FormControl-caption .color-fg-danger", text: "Check only if you are cool."
6262
assert_selector ".FormControl-caption .color-fg-danger", text: "A young thing."
6363
assert_selector ".FormControl-caption .color-fg-danger", text: "No longer a spring chicken."
64+
assert_selector ".FormControl-caption", text: "Lopez caption"
65+
assert_selector ".FormControl-caption", text: "Bellevue caption"
66+
assert_selector ".FormControl-caption", text: "Seattle caption"
6467
end
6568

66-
def test_the_input_is_described_by_the_caption_when_caption_templates_are_used
67-
num_inputs = 4
69+
def test_inputs_are_described_by_their_captions_when_caption_templates_are_used
70+
num_inputs = 7
6871
render_preview :caption_template_form
6972

7073
caption_ids = page

0 commit comments

Comments
 (0)
Please sign in to comment.