0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 01:22:07 +01:00

Add sublabel support to breadcrumbs

This commit is contained in:
Sage Abdullah 2023-12-14 16:30:37 +00:00 committed by Thibaud Colas
parent 4407c5b5b0
commit 3cbf3b4b6a
5 changed files with 35 additions and 5 deletions

View File

@ -11,5 +11,9 @@
font-size: theme('fontSize.22');
}
}
.w-breadcrumbs__sublabel {
display: inline-block;
}
}
}

View File

@ -3,7 +3,7 @@
The breadcrumb component is reused across most of Wagtail's headers.
Variables this template accepts:
`items` - A list of {"url": Union[str, None], "label": str} dicts
`items` - A list of {"url": Union[str, None], "label": str, "sublabel": Union[str, None]} dicts
`classname` - Modifier classes
`is_expanded` - Whether the breadcrumbs are always expanded or not, if True the breadcrumbs will not be collapsible
{% endcomment %}
@ -48,13 +48,23 @@
data-w-breadcrumbs-target="content"
{% endif %}
>
{% fragment as sublabel %}
{% if item.sublabel %}
<span class="w-sr-only">: </span>
<span class="w-breadcrumbs__sublabel w-font-normal w-hidden w-ml-2.5">
{{ item.sublabel }}
</span>
{% endif %}
{% endfragment %}
{% if item.url is not None %}
<a class="{{ breadcrumb_link_classes }}" href="{{ item.url }}">
{{ item.label }}
{{ sublabel }}
</a>
{% else %}
<div class="w-flex w-justify-start w-items-center">
{{ item.label }}
{{ sublabel }}
</div>
{% endif %}
{% if not forloop.last %}

View File

@ -773,6 +773,7 @@ class TestBreadcrumbs(AdminTemplateTestUtils, WagtailTestUtils, TestCase):
{
"url": "",
"label": "History",
"sublabel": str(self.object),
},
]
self.assertBreadcrumbsItemsRendered(items, response.content)
@ -797,6 +798,7 @@ class TestBreadcrumbs(AdminTemplateTestUtils, WagtailTestUtils, TestCase):
{
"url": "",
"label": "Usage",
"sublabel": str(self.object),
},
]
self.assertBreadcrumbsItemsRendered(items, response.content)
@ -821,6 +823,7 @@ class TestBreadcrumbs(AdminTemplateTestUtils, WagtailTestUtils, TestCase):
{
"url": "",
"label": "Inspect",
"sublabel": str(self.object),
},
]
self.assertBreadcrumbsItemsRendered(items, response.content)

View File

@ -1472,7 +1472,7 @@ class TestBreadcrumbs(AdminTemplateTestUtils, BaseSnippetViewSetTests):
"url": self.get_url("edit", args=(self.object.pk,)),
"label": str(self.object),
},
{"url": "", "label": "History"},
{"url": "", "label": "History", "sublabel": str(self.object)},
]
self.assertBreadcrumbsItemsRendered(items, response.content)
@ -1487,7 +1487,7 @@ class TestBreadcrumbs(AdminTemplateTestUtils, BaseSnippetViewSetTests):
"url": self.get_url("edit", args=(self.object.pk,)),
"label": str(self.object),
},
{"url": "", "label": "Usage"},
{"url": "", "label": "Usage", "sublabel": str(self.object)},
]
self.assertBreadcrumbsItemsRendered(items, response.content)
@ -1502,7 +1502,7 @@ class TestBreadcrumbs(AdminTemplateTestUtils, BaseSnippetViewSetTests):
"url": self.get_url("edit", args=(self.object.pk,)),
"label": str(self.object),
},
{"url": "", "label": "Inspect"},
{"url": "", "label": "Inspect", "sublabel": str(self.object)},
]
self.assertBreadcrumbsItemsRendered(items, response.content)

View File

@ -58,13 +58,26 @@ class AdminTemplateTestUtils:
element,
f"Expected '{item['label']}' breadcrumbs item to be a div",
)
label = element.text.strip()
# Sublabel is optional and the : separator is invisible
label = element.get_text(strip=True)
sublabel = None
if item.get("sublabel"):
label, sublabel = label.split(":", maxsplit=1)
self.assertEqual(
label,
item["label"],
f"Expected '{item['label']}' breadcrumbs item label, found '{label}'",
)
if sublabel:
self.assertEqual(
sublabel,
item["sublabel"],
f"Expected '{item['sublabel']}' breadcrumbs item sublabel, found '{sublabel}'",
)
def assertBreadcrumbsNotRendered(
self: Union[WagtailTestUtils, SimpleTestCase],
html: Union[str, bytes],