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

Improve Show As Tabs by Default Class #357

Closed
wants to merge 6 commits into from
Closed
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: 4 additions & 0 deletions src/unfold/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def each_context(self, request: HttpRequest) -> Dict[str, Any]:
"colors": self._process_colors(
get_config(self.settings_name)["COLORS"]
),
"fieldset_classes": get_config(self.settings_name).get(
"FIELDSET_CLASSES"
)
or [],
"tab_list": self.get_tabs_list(request),
"styles": [
self._get_value(style, request)
Expand Down
2 changes: 1 addition & 1 deletion src/unfold/templates/admin/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@

{% block field_sets %}
{% for fieldset in adminform %}
{% if "tab" not in fieldset.classes %}
{% if "tab" not in fieldset.classes|default:fieldset_classes %}
{% include 'admin/includes/fieldset.html' %}
{% endif %}
{% endfor %}
Expand Down
9 changes: 5 additions & 4 deletions src/unfold/templates/admin/includes/fieldset.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% load unfold %}
{% load i18n unfold %}

<fieldset class="module{% if fieldset.classes %} {{ fieldset.classes }}{% endif %}">
{% if fieldset.name and "tab" not in fieldset.classes %}
<fieldset class="module{% if fieldset.classes %} {{ fieldset.classes }}{% elif fieldset_classes %} {{ fieldset_classes|join:" " }}{% endif %}">
{% if "tab" not in fieldset.classes|default:fieldset_classes %}
<h2 class="bg-gray-100 border border-transparent font-semibold mb-6 px-4 py-3 rounded-md text-gray-700 text-sm lg:-mx-4 dark:bg-white/[.02] dark:border dark:border-gray-800 dark:text-gray-200">
{{ fieldset.name }}
{% trans 'General' as general %}
{{ fieldset.name|default_if_none:general }}
</h2>
{% endif %}

Expand Down
15 changes: 8 additions & 7 deletions src/unfold/templates/unfold/helpers/fieldsets_tabs.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
{% load unfold %}
{% load i18n unfold %}

{% with tabs=adminform|tabs %}
{% with tabs=adminform|tabs:fieldset_classes %}
{% if tabs %}
{% trans 'General' as general %}
<div x-data="{openTab: null}">
<ul class="bg-gray-100 border border-transparent flex gap-10 mb-6 px-4 py-3 rounded-md text-gray-400 text-sm lg:-mx-4 dark:bg-white/[.02] dark:border dark:border-gray-800 dark:text-gray-400">
{% for fieldset in tabs %}
<li>
<a class="cursor-pointer font-semibold hover:text-gray-700 dark:hover:text-white"
x-on:click="openTab = '{{ fieldset.name|slugify }}'"
x-bind:class="openTab == '{{ fieldset.name|slugify }}'{% if forloop.first %} || openTab == null{% endif %} ? 'text-gray-700 dark:text-white' : ''">
{{ fieldset.name }}
x-on:click="openTab = '{{ fieldset.name|default_if_none:general|slugify }}'"
x-bind:class="openTab == '{{ fieldset.name|default_if_none:general|slugify }}'{% if forloop.first %} || openTab == null{% endif %} ? 'text-gray-700 dark:text-white' : ''">
{{ fieldset.name|default_if_none:general }}
</a>
</li>
{% endfor %}
</ul>

{% for fieldset in tabs %}
<div class="tab-wrapper{% if fieldset.name %} fieldset-{{ fieldset.name|slugify }}{% endif %}"
x-show="openTab == '{{ fieldset.name|slugify }}'{% if forloop.first %} || openTab == null{% endif %}">
<div class="tab-wrapper{% if fieldset.name %} fieldset-{{ fieldset.name|default_if_none:general|slugify }}{% endif %}"
x-show="openTab == '{{ fieldset.name|default_if_none:general|slugify }}'{% if forloop.first %} || openTab == null{% endif %}">
{% include 'admin/includes/fieldset.html' %}
</div>
{% endfor %}
Expand Down
6 changes: 4 additions & 2 deletions src/unfold/templatetags/unfold.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ def index(indexable: Mapping[int, Any], i: int) -> Any:


@register.filter
def tabs(adminform: AdminForm) -> List[Fieldset]:
def tabs(
adminform: AdminForm, fieldset_classes: Optional[List[str]] = None
) -> List[Fieldset]:
result = []

for fieldset in adminform:
if "tab" in fieldset.classes and fieldset.name:
if "tab" in (fieldset_classes or fieldset.classes):
result.append(fieldset)

return result
Expand Down