Skip to content

Commit

Permalink
Refactor(breadcrumbs)!: expect a mapping from names to URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
mykolaskrynnyk committed Sep 21, 2024
1 parent 67d25ff commit 6839901
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
12 changes: 7 additions & 5 deletions pages/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@

with tab4:
code = """
st_undp.breadcrumb([
{'Home': '/'},
{'Custom components': '/custom'},
{'Breadcrumb': None}
])
st_undp.breadcrumb(
{
"Home": "/",
"Custom components": "/custom",
"Breadcrumb": None,
}
)
"""
eval(code.strip())
with st.expander("Show Code"):
Expand Down
9 changes: 6 additions & 3 deletions st_undp/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,17 @@ def image_card(src: str, summary: str, href: str, width: int = 12) -> None:
st.html(body)


def breadcrumb(items: list[dict[str, str]]) -> None:
def breadcrumb(items: dict[str, str]) -> None:
"""
[Breadcrumbs](https://design.undp.org/?path=/docs/components-navigation-components-breadcrumbs--docs) component.
The breadcrumbs are arranged in the same order as dictionary keys. Last item's value is ignored and can be
set to None.
Parameters
----------
items : list[dict[str, str]
List of items where each item maps a title to a URL.
items : dict[str, str]
Mapping from names to URLs.
Returns
-------
Expand Down
12 changes: 7 additions & 5 deletions st_undp/templates/breadcrumb.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<nav class="breadcrumb">
<ul>
{% for item in items %} {% for label, url in item.items() %} {% if url %}
<li><a href="{{ url }}" aria-label="{{label}}">{{ label }}</a></li>
{% else %}
<li aria-current="{{label}}">{{ label }}</li>
{% endif %} {% endfor %} {% endfor %}
{% for label, url in items.items() %}
{% if not loop.last %}
<li><a href="{{ url }}" aria-label="{{label}}">{{ label }}</a></li>
{% else %}
<li aria-current="{{label}}">{{ label }}</li>
{% endif %}
{% endfor %}
</ul>
</nav>

0 comments on commit 6839901

Please sign in to comment.