Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajoute la possibilité de paramétrer les attributs HTML du bouton de clôture de l'alerte #186

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions dsfr/templates/dsfr/alert.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
</p>
{% endif %}
{% if self.is_collapsible %}
<button class="fr-btn--close fr-btn"
title="{{ hide_message }}"
onclick="const alert = this.parentNode; alert.parentNode.removeChild(alert)">
<button class="fr-btn--close fr-btn" title="{{ hide_message }}" {% for attr, value in self.collapsible_attrs.items %}{{ attr }}="{{ value|safe }}"{% endfor %}>
{{ hide_message }}
</button>
{% endif %}
Expand Down
14 changes: 13 additions & 1 deletion dsfr/templatetags/dsfr_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def dsfr_alert(*args, **kwargs) -> dict:
"type": "Possible values : info, success, error",
"content": "(Optional if median) Content of the accordion item (can include html)",
"heading_tag": "(Optional) Heading tag for the alert title (default: p)",
"is_collapsible" : "(Optional) Boolean, set to true to add a 'close' button for the alert (default: false)",
"is_collapsible" : "(Optional) Boolean, set to true to add a 'close' button for the alert (default: false); set to true if 'collapsible_attrs' is set",
"collapsible_attrs": dict of HTML attribute to set to the 'close' button,
"id": "Unique id of the alert item (Optional, mandatory if collapsible)",
"extra_classes": "(Optional) string with names of extra classes."
}
Expand Down Expand Up @@ -216,9 +217,20 @@ def dsfr_alert(*args, **kwargs) -> dict:
]
tag_data = parse_tag_args(args, kwargs, allowed_keys)

if "collapsible_attrs" in tag_data:
tag_data["is_collapsible"] = True

tag_data.setdefault("title", None)
tag_data.setdefault("id", generate_random_id("alert"))
tag_data.setdefault("is_collapsible", False)
tag_data.setdefault(
"collapsible_attrs",
{
"onclick": (
"const alert = this.parentNode; " "alert.parentNode.removeChild(alert)"
)
},
)

return {"self": tag_data}

Expand Down
19 changes: 19 additions & 0 deletions dsfr/test/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ def test_alert_tag_has_collapse_button(self):
rendered_template,
)

def test_alert_tag_has_custom_attrs(self):
test_data = self.test_data.copy()
test_data.pop("is_collapsible", None)
test_data["collapsible_attrs"] = {
"data-controller": "close",
"data-action": "close#onClick",
}
rendered_template = self.template_to_render.render(
Context({"test_data": test_data})
)
self.assertInHTML(
"""
<button class="fr-btn--close fr-btn" title="Masquer le message" data-controller="close" data-action="close#onClick">
Masquer le message
</button>
""", # noqa
rendered_template,
)


class DsfrBadgeTagTest(SimpleTestCase):
test_data = {
Expand Down
Loading