Skip to content

Commit d4b3396

Browse files
committed
fix: include a visually hidden suffix in repeated labels
The "Describe the specific area" label is repeated for each conditionally shown input. Colin R pointed out that this can cause an accessibility problem when using e.g. Dragon Naturally Speaking to control the browser. If multiple elements on the same page have the same label, it won't know which you mean. When javascript is working there should only be one of them visible on the page, but the fields are still there. Putting the unique part at the end should mean that the user can say the label as it appears visually, "Describe the specific area" and Dragon can prompt which one specifically they mean, e.g. "Describe the specific area: right breast" This should also workaround our tests in CI for some reason not running the javascript that hides the non-selected fields. We may need to look into this further, but it doesn't block this work.
1 parent 40b7e49 commit d4b3396

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

manage_breast_screening/mammograms/forms/symptom_forms.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ def area_radios(symptom_name="symptom"):
9696
)
9797

9898
@staticmethod
99-
def area_description(symptom_name="symptom", hint="For example, the left armpit"):
99+
def area_description(
100+
symptom_name="symptom",
101+
hint="For example, the left armpit",
102+
visually_hidden_label_suffix=None,
103+
):
100104
return CharField(
101105
required=False,
102106
label="Describe the specific area",
@@ -105,6 +109,7 @@ def area_description(symptom_name="symptom", hint="For example, the left armpit"
105109
"required": f"Describe the specific area where the {symptom_name} is located"
106110
},
107111
classes="nhsuk-u-width-two-thirds",
112+
visually_hidden_label_suffix=visually_hidden_label_suffix,
108113
)
109114

110115

@@ -224,9 +229,15 @@ def create(self, appointment, request):
224229

225230
class LumpForm(SymptomForm):
226231
area = CommonFields.area_radios(symptom_name="lump")
227-
area_description_right_breast = CommonFields.area_description("lump")
228-
area_description_left_breast = CommonFields.area_description("lump")
229-
area_description_other = CommonFields.area_description("lump")
232+
area_description_right_breast = CommonFields.area_description(
233+
"lump", visually_hidden_label_suffix="right breast"
234+
)
235+
area_description_left_breast = CommonFields.area_description(
236+
"lump", visually_hidden_label_suffix="left breast"
237+
)
238+
area_description_other = CommonFields.area_description(
239+
"lump", visually_hidden_label_suffix="other"
240+
)
230241
when_started = CommonFields.when_started
231242
specific_date = CommonFields.specific_date
232243
intermittent = CommonFields.intermittent
@@ -255,12 +266,14 @@ def __init__(self, instance=None, **kwargs):
255266
class SwellingOrShapeChangeForm(SymptomForm):
256267
area = CommonFields.area_radios(symptom_name="swelling or shape change")
257268
area_description_right_breast = CommonFields.area_description(
258-
"swelling or shape change"
269+
"swelling or shape change", visually_hidden_label_suffix="right breast"
259270
)
260271
area_description_left_breast = CommonFields.area_description(
261-
"swelling or shape change"
272+
"swelling or shape change", visually_hidden_label_suffix="left breast"
273+
)
274+
area_description_other = CommonFields.area_description(
275+
"swelling or shape change", visually_hidden_label_suffix="other"
262276
)
263-
area_description_other = CommonFields.area_description("swelling or shape change")
264277
when_started = CommonFields.when_started
265278
specific_date = CommonFields.specific_date
266279
intermittent = CommonFields.intermittent
@@ -292,9 +305,15 @@ def __init__(self, instance=None, **kwargs):
292305

293306
class SkinChangeForm(SymptomForm):
294307
area = CommonFields.area_radios(symptom_name="skin change")
295-
area_description_right_breast = CommonFields.area_description("skin change")
296-
area_description_left_breast = CommonFields.area_description("skin change")
297-
area_description_other = CommonFields.area_description("skin change")
308+
area_description_right_breast = CommonFields.area_description(
309+
"skin change", visually_hidden_label_suffix="right breast"
310+
)
311+
area_description_left_breast = CommonFields.area_description(
312+
"skin change", visually_hidden_label_suffix="left breast"
313+
)
314+
area_description_other = CommonFields.area_description(
315+
"skin change", visually_hidden_label_suffix="other"
316+
)
298317
symptom_sub_type = ChoiceField(
299318
choices=SkinChangeChoices,
300319
label="How has the skin changed?",
@@ -415,9 +434,15 @@ def area_initial(self, area):
415434

416435
class OtherSymptomForm(SymptomForm):
417436
area = CommonFields.area_radios()
418-
area_description_right_breast = CommonFields.area_description()
419-
area_description_left_breast = CommonFields.area_description()
420-
area_description_other = CommonFields.area_description()
437+
area_description_right_breast = CommonFields.area_description(
438+
visually_hidden_label_suffix="right breast"
439+
)
440+
area_description_left_breast = CommonFields.area_description(
441+
visually_hidden_label_suffix="left breast"
442+
)
443+
area_description_other = CommonFields.area_description(
444+
visually_hidden_label_suffix="other"
445+
)
421446
symptom_sub_type_details = CharField(
422447
label="Describe the symptom",
423448
label_classes="nhsuk-label--m",

manage_breast_screening/nhsuk_forms/fields/char_field.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def __init__(
99
hint=None,
1010
label_classes=None,
1111
classes=None,
12+
visually_hidden_label_suffix=None,
1213
**kwargs,
1314
):
1415
widget = kwargs.get("widget")
@@ -22,6 +23,7 @@ def __init__(
2223
self.hint = hint
2324
self.classes = classes
2425
self.label_classes = label_classes
26+
self.visually_hidden_label_suffix = visually_hidden_label_suffix
2527

2628
super().__init__(*args, **kwargs)
2729

manage_breast_screening/nhsuk_forms/jinja2/forms/input.jinja

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
{% from "nhsuk/components/input/macro.jinja" import input %}
22
{% set unbound_field = field.field %}
33
{% set widget = unbound_field.widget %}
4+
{% if unbound_field.visually_hidden_label_suffix %}
5+
{% set label_html %}
6+
{{ field.label }}<span class="nhsuk-u-visually-hidden">: {{ unbound_field.visually_hidden_label_suffix }}</span>
7+
{% endset %}
8+
{% endif %}
49
{% set input_params = {
510
"label": {
611
"text": field.label,
12+
"html": label_html if label_html,
713
"classes": unbound_field.label_classes if unbound_field.label_classes
814
},
915
"hint": {

manage_breast_screening/nhsuk_forms/tests/fields/test_char_field.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class TestForm(Form):
1616
initial="somevalue",
1717
label_classes="nhsuk-u-visually-hidden",
1818
)
19+
field_with_visually_hidden_label_suffix = CharField(
20+
label="Abc",
21+
initial="somevalue",
22+
visually_hidden_label_suffix="some suffix",
23+
)
1924
field_with_hint = CharField(
2025
label="With hint", initial="", hint="ALL UPPERCASE"
2126
)
@@ -73,6 +78,18 @@ def test_renders_nhs_input_with_visually_hidden_label(self, form_class):
7378
""",
7479
)
7580

81+
def test_renders_nhs_input_with_visually_hidden_label_suffix(self, form_class):
82+
assertHTMLEqual(
83+
form_class()["field_with_visually_hidden_label_suffix"].as_field_group(),
84+
"""
85+
<div class="nhsuk-form-group">
86+
<label class="nhsuk-label" for="id_field_with_visually_hidden_label_suffix">
87+
Abc<span class="nhsuk-u-visually-hidden">: some suffix</span>
88+
</label><input class="nhsuk-input" id="id_field_with_visually_hidden_label_suffix" name="field_with_visually_hidden_label_suffix" type="text" value="somevalue">
89+
</div>
90+
""",
91+
)
92+
7693
def test_renders_nhs_input_with_hint(self, form_class):
7794
assertHTMLEqual(
7895
form_class()["field_with_hint"].as_field_group(),

manage_breast_screening/tests/system/clinical/test_recording_symptoms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ def then_i_see_the_add_a_lump_form(self):
9696
self.assert_page_title_contains("Details of the lump")
9797

9898
def when_i_select_right_breast(self):
99-
self.page.get_by_label("Right breast").click()
99+
self.page.get_by_label("Right breast", exact=True).click()
100100

101101
def and_i_enter_the_area(self):
102-
self.page.get_by_label("Describe the specific area").filter(visible=True).fill(
103-
"ldq"
104-
)
102+
self.page.get_by_label("Describe the specific area: right breast").filter(
103+
visible=True
104+
).fill("ldq")
105105

106106
def and_i_select_less_than_three_months(self):
107107
self.page.get_by_label("Less than 3 months").click()

0 commit comments

Comments
 (0)