Skip to content

Commit

Permalink
fix: a bug when removing and adding a tab would result in 2 tabs with…
Browse files Browse the repository at this point in the history
… same number

fix devhatt#28
  • Loading branch information
RafaelLimaC committed Mar 12, 2024
1 parent 7c14566 commit 9262997
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
27 changes: 19 additions & 8 deletions src/components/Tabber/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const html = `
<div class="tabber-content" data-select="tabber-content"></div>
</div>
`;
// todo corrigir bug ao remover e depois adicionar aba > elas ficam com o mesmo index

export default function Tabber({ tabs }) {
Component.call(this, { html, events });

Expand Down Expand Up @@ -55,9 +55,7 @@ Tabber.prototype = Object.assign(Tabber.prototype, Component.prototype, {
.querySelectorAll('div');

contentContainer.forEach((content) => content.classList.add('hide'));
tabsContainer.forEach((tab) =>
tab.classList.remove('tabber-button--active'),
);
tabsContainer.forEach((tab) => tab.classList.remove('tabber-button--active'));

tabButton.classList.add('tabber-button--active');
tabContent.classList.remove('hide');
Expand All @@ -74,16 +72,29 @@ Tabber.prototype = Object.assign(Tabber.prototype, Component.prototype, {
const tabsContainer = this.selected.get('tabber-tabs');
const contentContainer = this.selected.get('tabber-content');

const index = tabsContainer.children.length;
let index;
if (tabsContainer.children.length === 0) {
index = 0;
} else {
const lastIndex = parseInt(tabsContainer.lastElementChild.dataset.index, 10);
index = lastIndex + 1;
}

const tabButton = document.createElement('button');
tabButton.textContent = tab.title;
tabButton.dataset.index = index;
tabButton.classList.add('tabber-button');

if (tab.icon) {
const icon = document.createElement('img');
icon.src = tab.icon;
tabButton.appendChild(icon);
fetch(tab.icon)
.then((response) => response.text())
.then((svgContent) => {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = svgContent.trim();
const svgElement = tempDiv.querySelector('svg');

tabButton.appendChild(svgElement);
});
}

tabsContainer.appendChild(tabButton);
Expand Down
4 changes: 3 additions & 1 deletion src/components/Tabber/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ $borderHeight: 0.3rem;
}
}

img {
svg {
max-height: 1.4rem;

fill: currentColor;
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/stories/assets/tabber/birthday.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions src/stories/assets/tabber/home.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9262997

Please sign in to comment.