Skip to content

Commit

Permalink
Use TeleportController to re-render slim header's buttons on results …
Browse files Browse the repository at this point in the history
…refresh

Fixes wagtail#11726
  • Loading branch information
laymonage authored and lb- committed Mar 9, 2024
1 parent 2003e14 commit 574a9d4
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Changelog
* Fix: Resolve issue with unwanted padding in chooser modal listings (Sage Abdullah)
* Fix: Ensure form builder emails that have date or datetime fields correctly localize dates based on the configured `LANGUAGE_CODE` (Mark Niehues)
* Fix: Ensure the Stimulus `UnsavedController` checks for nested removal/additions of inputs so that the unsaved warning shows in more valid cases when editing a page (Karthik Ayangar)
* Fix: Ensure `get_add_url()` is always used to re-render the add button when the listing is refreshed in viewsets (Sage Abdullah)
* Docs: Add contributing development documentation on how to work with a fork of Wagtail (Nix Asteri, Dan Braghis)
* Docs: Make sure the settings panel is listed in tabbed interface examples (Tibor Leupold)
* Docs: Update content and page names to their US spelling instead of UK spelling (Victoria Poromon)
Expand Down
1 change: 1 addition & 0 deletions docs/releases/6.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ depth: 1
* Resolve issue with unwanted padding in chooser modal listings (Sage Abdullah)
* Ensure form builder emails that have date or datetime fields correctly localize dates based on the configured `LANGUAGE_CODE` (Mark Niehues)
* Ensure the Stimulus `UnsavedController` checks for nested removal/additions of inputs so that the unsaved warning shows in more valid cases when editing a page (Karthik Ayangar)
* Ensure `get_add_url()` is always used to re-render the add button when the listing is refreshed in viewsets (Sage Abdullah)


### Documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
</template>
{% endif %}

{% if render_buttons_fragment %}
<template data-controller="w-teleport" data-w-teleport-target-value="#w-slim-header-buttons" data-w-teleport-reset-value="true">
{% for button in header_buttons %}
{% component button %}
{% endfor %}
</template>
{% endif %}

{% if is_searching and view.show_other_searches %}
<div class="nice-padding">
{% search_other %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h1 class="w-sr-only">
{% if actions or block_actions %}
{# Actions divider #}
<div class="w-w-px w-h-[30px] w-ml-auto sm:w-ml-0 w-bg-border-furniture"></div>
<div class="w-flex w-items-center w-ml-3">
<div id="w-slim-header-buttons" class="w-flex w-items-center w-ml-3">
{% firstof actions block_actions %}
</div>
{% endif %}
Expand Down
3 changes: 3 additions & 0 deletions wagtail/admin/views/generic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,8 @@ def get_context_data(self, *args, **kwargs):
and self.filters
and self.results_only
)
context["render_buttons_fragment"] = (
context.get("header_buttons") and self.results_only
)

return context
9 changes: 0 additions & 9 deletions wagtail/admin/views/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ def header_buttons(self):
self.add_item_label,
url=self.add_url,
icon_name="plus",
attrs={"data-w-link-reflect-keys-value": '["locale"]'},
)
)
return buttons
Expand All @@ -349,10 +348,6 @@ def header_more_buttons(self):
url=self.xlsx_export_url,
icon_name="download",
priority=90,
attrs={
"data-controller": "w-link",
"data-w-link-preserve-keys-value": '["export"]',
},
)
)
buttons.append(
Expand All @@ -361,10 +356,6 @@ def header_more_buttons(self):
url=self.csv_export_url,
icon_name="download",
priority=100,
attrs={
"data-controller": "w-link",
"data-w-link-preserve-keys-value": '["export"]',
},
)
)

Expand Down
3 changes: 1 addition & 2 deletions wagtail/admin/widgets/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ def __init__(
attrs = attrs.copy()
attrs.update(
{
"data-controller": "w-tooltip w-link",
"data-controller": "w-tooltip",
"data-w-tooltip-content-value": label,
"data-action": "w-swap:reflect@document->w-link#setParams",
"aria-label": label,
}
)
Expand Down
32 changes: 32 additions & 0 deletions wagtail/snippets/tests/test_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1536,3 +1536,35 @@ def test_index_view_get_add_url_is_respected_with_i18n(self):
soup = self.get_soup(response.content)
links = soup.find_all("a", attrs={"href": add_url})
self.assertEqual(len(links), 1)

def test_index_results_view_get_add_url_teleports_to_header(self):
response = self.client.get(self.get_url("list_results"))
add_url = self.get_url("add") + "?customised=param"
soup = self.get_soup(response.content)
template = soup.find(
"template",
{
"data-controller": "w-teleport",
"data-w-teleport-target-value": "#w-slim-header-buttons",
},
)
self.assertIsNotNone(template)
links = template.find_all("a", attrs={"href": add_url})
self.assertEqual(len(links), 1)

@override_settings(WAGTAIL_I18N_ENABLED=True)
def test_index_results_view_get_add_url_teleports_to_header_with_i18n(self):
Locale.objects.create(language_code="fr")
response = self.client.get(self.get_url("list_results") + "?locale=fr")
add_url = self.get_url("add") + "?locale=fr&customised=param"
soup = self.get_soup(response.content)
template = soup.find(
"template",
{
"data-controller": "w-teleport",
"data-w-teleport-target-value": "#w-slim-header-buttons",
},
)
self.assertIsNotNone(template)
links = template.find_all("a", attrs={"href": add_url})
self.assertEqual(len(links), 1)

0 comments on commit 574a9d4

Please sign in to comment.