Skip to content

Commit f5ffc99

Browse files
miriam-zThemitchell
authored andcommitted
Added support for rendering hints on individual radio button choices, matching the existing checkbox functionality
1 parent d16ec63 commit f5ffc99

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

lung_cancer_screening/nhsuk_forms/jinja2/radios.jinja

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
{% set ns = namespace(items=[]) %}
77
{% for value, text in unbound_field.choices %}
88
{% set conditional_html = field.conditional_html(value) %}
9+
{% set hint_text = field.get_hint_for_choice(value) %}
910
{% set ns.items = ns.items + [{
1011
"id": field.auto_id if loop.first,
1112
"value": value,
1213
"text": text,
1314
"checked": field.value() == value,
15+
"hint": {
16+
"text": hint_text
17+
} if hint_text else undefined,
1418
"conditional": {
1519
"html": conditional_html
1620
} if conditional_html else undefined

lung_cancer_screening/nhsuk_forms/tests/unit/test_choice_field.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.test import TestCase
22
from django.forms import Form
33

4-
from ...choice_field import ChoiceField
4+
from ...choice_field import ChoiceField, MultipleChoiceField
55

66
class TestForm(Form):
77
field = ChoiceField(
@@ -11,6 +11,13 @@ class TestForm(Form):
1111
hint="Pick either one",
1212
)
1313

14+
class TestMultipleChoiceForm(Form):
15+
field = MultipleChoiceField(
16+
label="Select options",
17+
choices=(("a", "Option A"), ("b", "Option B"), ("c", "Option C")),
18+
hint="Select all that apply",
19+
)
20+
1421
class TestChoiceField(TestCase):
1522
def test_renders_nhs_radios(self):
1623
self.assertHTMLEqual(
@@ -74,3 +81,72 @@ class TestForm(Form):
7481
</div>
7582
""",
7683
)
84+
85+
def test_checkbox_field_with_choice_hints(self):
86+
"""Test that choice hints are rendered correctly for checkbox fields"""
87+
form = TestMultipleChoiceForm()
88+
bound_field = form["field"]
89+
90+
# Add hints for specific choices
91+
bound_field.add_hint_for_choice("a", "This is hint for option A")
92+
bound_field.add_hint_for_choice("b", "This is hint for option B")
93+
94+
rendered_html = bound_field.as_field_group()
95+
96+
# Verify the hints are rendered
97+
self.assertIn('This is hint for option A', rendered_html)
98+
self.assertIn('This is hint for option B', rendered_html)
99+
self.assertIn('aria-describedby="id_field_0-item-hint"', rendered_html)
100+
self.assertIn('aria-describedby="id_field_1-item-hint"', rendered_html)
101+
102+
def test_get_hint_for_choice_returns_correct_hint(self):
103+
"""Test that get_hint_for_choice returns the correct hint text"""
104+
form = TestMultipleChoiceForm()
105+
bound_field = form["field"]
106+
107+
# Add a hint
108+
bound_field.add_hint_for_choice("a", "Hint for A")
109+
110+
# Verify the hint can be retrieved
111+
self.assertEqual(bound_field.get_hint_for_choice("a"), "Hint for A")
112+
113+
def test_get_hint_for_choice_returns_none_when_no_hint(self):
114+
"""Test that get_hint_for_choice returns None when no hint is set"""
115+
form = TestMultipleChoiceForm()
116+
bound_field = form["field"]
117+
118+
# No hint added, should return None
119+
self.assertIsNone(bound_field.get_hint_for_choice("a"))
120+
121+
def test_checkbox_field_renders_without_hints_when_none_added(self):
122+
"""Test that checkbox field renders correctly when no hints are added"""
123+
form = TestMultipleChoiceForm()
124+
rendered_html = form["field"].as_field_group()
125+
126+
# Should render without hint elements
127+
self.assertNotIn('nhsuk-checkboxes__hint', rendered_html)
128+
129+
def test_radio_field_with_choice_hints(self):
130+
"""Test that choice hints are rendered correctly for radio fields"""
131+
form = TestForm()
132+
bound_field = form["field"]
133+
134+
# Add hints for specific choices
135+
bound_field.add_hint_for_choice("a", "This is hint for option A")
136+
bound_field.add_hint_for_choice("b", "This is hint for option B")
137+
138+
rendered_html = bound_field.as_field_group()
139+
140+
# Verify the hints are rendered
141+
self.assertIn('This is hint for option A', rendered_html)
142+
self.assertIn('This is hint for option B', rendered_html)
143+
self.assertIn('aria-describedby="id_field-item-hint"', rendered_html)
144+
self.assertIn('aria-describedby="id_field-2-item-hint"', rendered_html)
145+
146+
def test_radio_field_renders_without_hints_when_none_added(self):
147+
"""Test that radio field renders correctly when no hints are added"""
148+
form = TestForm()
149+
rendered_html = form["field"].as_field_group()
150+
151+
# Should render without hint elements for individual items
152+
self.assertNotIn('nhsuk-radios__hint', rendered_html)

0 commit comments

Comments
 (0)