Skip to content

Commit

Permalink
Fix caption templates
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron committed Oct 15, 2024
1 parent 9e301d3 commit dc0f933
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 11 deletions.
6 changes: 6 additions & 0 deletions app/forms/caption_template_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ class CaptionTemplateForm < ApplicationForm
age_radios.radio_button(value: "young", label: "10-15")
age_radios.radio_button(value: "middle_aged", label: "16-21")
end

name_form.check_box_group(name: "places", label: "Cool places") do |check_group|
check_group.check_box(value: "lopez", label: "Lopez Island")
check_group.check_box(value: "bellevue", label: "Bellevue")
check_group.check_box(value: "seattle", label: "Seattle")
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span>Bellevue caption</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span>Lopez caption</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span>Seattle caption</span>
12 changes: 4 additions & 8 deletions app/lib/primer/forms/dsl/check_box_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,10 @@ def supports_validation?
false
end

private

def caption_template_name
@caption_template_name ||= if @scheme == :array
:"#{name}_#{value}"
else
name.to_sym
end
def values_disambiguate_template_names?
# Check boxes submitted as an array all have the same name, so we return true here
# to ensure different caption templates can be attached to individual check box inputs.
@scheme == :array
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion app/lib/primer/forms/dsl/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ def validation_error_icon_target
""
end

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

private

def input_data
Expand All @@ -309,7 +317,7 @@ def input_data
def caption_template_name
return nil unless name

@caption_template_name ||= if respond_to?(:value) && value.present?
@caption_template_name ||= if values_disambiguate_template_names? && respond_to?(:value) && value.present?
:"#{name}_#{value}"
else
name.to_sym
Expand Down
4 changes: 4 additions & 0 deletions app/lib/primer/forms/dsl/radio_button_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def type
def supports_validation?
false
end

def values_disambiguate_template_names?
true
end
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions test/lib/primer/forms_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ def test_renders_the_caption_template_when_present
assert_selector ".FormControl-caption .color-fg-danger", text: "Check only if you are cool."
assert_selector ".FormControl-caption .color-fg-danger", text: "A young thing."
assert_selector ".FormControl-caption .color-fg-danger", text: "No longer a spring chicken."
assert_selector ".FormControl-caption", text: "Lopez caption"
assert_selector ".FormControl-caption", text: "Bellevue caption"
assert_selector ".FormControl-caption", text: "Seattle caption"
end

def test_the_input_is_described_by_the_caption_when_caption_templates_are_used
num_inputs = 4
def test_inputs_are_described_by_their_captions_when_caption_templates_are_used
num_inputs = 7
render_preview :caption_template_form

caption_ids = page
Expand Down

0 comments on commit dc0f933

Please sign in to comment.