Skip to content

Commit c335c85

Browse files
committed
feat: Add support for unlimited fieldset tabs items
1 parent af01599 commit c335c85

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/unfold/static/unfold/js/app.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ window.addEventListener("load", (e) => {
1010
warnWithoutSaving();
1111

1212
tabNavigation();
13+
14+
tabNavigation("fieldset-");
1315
});
1416

1517
/*************************************************************
1618
* Move not visible tab items to dropdown
1719
*************************************************************/
18-
function tabNavigation() {
19-
const itemsDropdown = document.getElementById("tabs-dropdown");
20-
const itemsList = document.getElementById("tabs-items");
20+
function tabNavigation(prefix = "") {
21+
const itemsDropdown = document.getElementById(prefix + "tabs-dropdown");
22+
const itemsList = document.getElementById(prefix + "tabs-items");
2123
const widths = [];
2224

2325
if (!itemsDropdown || !itemsList) {
@@ -32,7 +34,7 @@ function tabNavigation() {
3234

3335
function handleTabNavigationResize() {
3436
const contentWidth = document.getElementById("content").offsetWidth;
35-
const tabsWidth = document.getElementById("tabs-wrapper").scrollWidth;
37+
const tabsWidth = document.getElementById(prefix + "tabs-wrapper").scrollWidth;
3638
const availableWidth =
3739
itemsList.parentElement.offsetWidth - itemsList.offsetWidth - 48;
3840

@@ -47,7 +49,7 @@ function tabNavigation() {
4749
// If there is still not enough space, move the last item to the dropdown again
4850
if (
4951
document.getElementById("content").offsetWidth <
50-
document.getElementById("tabs-wrapper").scrollWidth
52+
document.getElementById(prefix + "tabs-wrapper").scrollWidth
5153
) {
5254
handleTabNavigationResize();
5355
}
@@ -70,6 +72,14 @@ function tabNavigation() {
7072
itemsDropdown.parentElement.classList.add("hidden");
7173
} else {
7274
itemsDropdown.parentElement.classList.remove("hidden");
75+
76+
// After adding the dropdown item, check again if we need to move items
77+
if (
78+
document.getElementById("content").offsetWidth <
79+
document.getElementById(prefix + "tabs-wrapper").scrollWidth
80+
) {
81+
handleTabNavigationResize();
82+
}
7383
}
7484
}
7585
}

src/unfold/templates/unfold/helpers/fieldsets_tabs.html

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@
33
{% with tabs=adminform|tabs %}
44
{% if tabs %}
55
<div class="{% fieldset_rows_classes %}" x-data="{openTab: null}" x-show="activeTab == 'general'">
6-
<div class="{% if adminform.model_admin.compressed_fields %}border-b border-base-200 border-dashed dark:border-base-800{% endif %}">
7-
<nav class="bg-base-100 cursor-pointer flex flex-col font-medium gap-1 m-2 p-1 rounded-default text-important dark:border-base-700 md:inline-flex md:flex-row md:w-auto dark:bg-white/[.04] *:flex-inline *:flex-row *:items-center *:px-2.5 *:py-[5px] *:rounded-default *:transition-colors *:hover:bg-base-700/[.04] *:dark:hover:bg-white/[.04] [&>.active]:bg-white [&>.active]:shadow-xs [&>.active]:hover:bg-white [&>.active]:dark:bg-base-900 [&>.active]:dark:hover:bg-base-900">
8-
{% for fieldset in tabs %}
9-
<a x-on:click="openTab = '{{ forloop.counter0 }}-{{ fieldset.name|slugify }}'" x-bind:class="openTab == '{{ forloop.counter0 }}-{{ fieldset.name|slugify }}'{% if forloop.first %} || openTab == null{% endif %} ? 'active' : ''">
10-
{{ fieldset.name }}
11-
</a>
12-
{% endfor %}
13-
</nav>
6+
<div id="fieldset-tabs-wrapper" class="flex items-start flex-col md:flex-row md:items-center {% if adminform.model_admin.compressed_fields %}border-b border-base-200 border-dashed dark:border-base-800{% endif %}">
7+
<div class="flex flex-row grow">
8+
<nav id="fieldset-tabs-items" class="bg-base-100 cursor-pointer flex flex-row font-medium gap-1 m-2 p-1 rounded-default text-important dark:border-base-700 md:w-auto dark:bg-white/[.04] *:flex-inline *:flex-row *:whitespace-nowrap *:items-center *:px-2.5 *:py-[5px] *:rounded-default *:transition-colors *:hover:bg-base-700/[.04] *:dark:hover:bg-white/[.04] [&>.active]:bg-white [&>.active]:shadow-xs [&>.active]:hover:bg-white [&>.active]:dark:bg-base-900 [&>.active]:dark:hover:bg-base-900">
9+
{% for fieldset in tabs %}
10+
<a x-on:click="openTab = '{{ forloop.counter0 }}-{{ fieldset.name|slugify }}'" x-bind:class="openTab == '{{ forloop.counter0 }}-{{ fieldset.name|slugify }}'{% if forloop.first %} || openTab == null{% endif %} ? 'active' : ''">
11+
{{ fieldset.name }}
12+
</a>
13+
{% endfor %}
14+
</nav>
15+
16+
<div class="hidden ml-2 relative" x-data="{ openFieldsetsTabsDropdown: false }">
17+
<div class="group border border-base-200 flex cursor-pointer h-[38px] w-[38px] items-center justify-center rounded-default select-none" x-on:click="openFieldsetsTabsDropdown = !openFieldsetsTabsDropdown">
18+
<span class="material-symbols-outlined text-base-400 group-hover:text-primary-600 dark:group-hover:text-primary-500" x-bind:class="{'text-primary-600 dark:text-primary-500': openFieldsetsTabsDropdown}">more_horiz</span>
19+
</div>
20+
21+
<nav id="fieldset-tabs-dropdown" class="absolute bg-white border border-base-200 flex flex-col -mr-px py-1 right-0 rounded-default shadow-lg top-10 w-52 z-50 dark:bg-base-800 dark:border-base-700 *:max-h-[30px] *:flex *:flex-row *:items-center *:mx-1 *:px-3 *:py-2 *:rounded-default *:text-left *:hover:bg-base-100 *:hover:text-base-700 *:dark:hover:bg-base-700 *:dark:hover:text-base-200" x-show="openFieldsetsTabsDropdown" x-transition></nav>
22+
</div>
23+
</div>
1424
</div>
1525

1626
{% for fieldset in tabs %}

0 commit comments

Comments
 (0)