Skip to content

Commit 6d5a056

Browse files
committed
Added support for rendering hints on individual radio button choices, matching the existing checkbox functionality
1 parent d18dced commit 6d5a056

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(
@@ -38,3 +45,72 @@ def test_renders_nhs_radios(self):
3845
</div>
3946
""",
4047
)
48+
49+
def test_checkbox_field_with_choice_hints(self):
50+
"""Test that choice hints are rendered correctly for checkbox fields"""
51+
form = TestMultipleChoiceForm()
52+
bound_field = form["field"]
53+
54+
# Add hints for specific choices
55+
bound_field.add_hint_for_choice("a", "This is hint for option A")
56+
bound_field.add_hint_for_choice("b", "This is hint for option B")
57+
58+
rendered_html = bound_field.as_field_group()
59+
60+
# Verify the hints are rendered
61+
self.assertIn('This is hint for option A', rendered_html)
62+
self.assertIn('This is hint for option B', rendered_html)
63+
self.assertIn('aria-describedby="id_field_0-item-hint"', rendered_html)
64+
self.assertIn('aria-describedby="id_field_1-item-hint"', rendered_html)
65+
66+
def test_get_hint_for_choice_returns_correct_hint(self):
67+
"""Test that get_hint_for_choice returns the correct hint text"""
68+
form = TestMultipleChoiceForm()
69+
bound_field = form["field"]
70+
71+
# Add a hint
72+
bound_field.add_hint_for_choice("a", "Hint for A")
73+
74+
# Verify the hint can be retrieved
75+
self.assertEqual(bound_field.get_hint_for_choice("a"), "Hint for A")
76+
77+
def test_get_hint_for_choice_returns_none_when_no_hint(self):
78+
"""Test that get_hint_for_choice returns None when no hint is set"""
79+
form = TestMultipleChoiceForm()
80+
bound_field = form["field"]
81+
82+
# No hint added, should return None
83+
self.assertIsNone(bound_field.get_hint_for_choice("a"))
84+
85+
def test_checkbox_field_renders_without_hints_when_none_added(self):
86+
"""Test that checkbox field renders correctly when no hints are added"""
87+
form = TestMultipleChoiceForm()
88+
rendered_html = form["field"].as_field_group()
89+
90+
# Should render without hint elements
91+
self.assertNotIn('nhsuk-checkboxes__hint', rendered_html)
92+
93+
def test_radio_field_with_choice_hints(self):
94+
"""Test that choice hints are rendered correctly for radio fields"""
95+
form = TestForm()
96+
bound_field = form["field"]
97+
98+
# Add hints for specific choices
99+
bound_field.add_hint_for_choice("a", "This is hint for option A")
100+
bound_field.add_hint_for_choice("b", "This is hint for option B")
101+
102+
rendered_html = bound_field.as_field_group()
103+
104+
# Verify the hints are rendered
105+
self.assertIn('This is hint for option A', rendered_html)
106+
self.assertIn('This is hint for option B', rendered_html)
107+
self.assertIn('aria-describedby="id_field-item-hint"', rendered_html)
108+
self.assertIn('aria-describedby="id_field-2-item-hint"', rendered_html)
109+
110+
def test_radio_field_renders_without_hints_when_none_added(self):
111+
"""Test that radio field renders correctly when no hints are added"""
112+
form = TestForm()
113+
rendered_html = form["field"].as_field_group()
114+
115+
# Should render without hint elements for individual items
116+
self.assertNotIn('nhsuk-radios__hint', rendered_html)

0 commit comments

Comments
 (0)