Skip to content

Commit

Permalink
fix(tabs): react to changes within fw-tabs
Browse files Browse the repository at this point in the history
This adds a mutation Observer to tabs component so that if there is a change in fw-tab, the entire
  • Loading branch information
Asif Ahmed committed May 22, 2020
1 parent 6dd1010 commit c1fe804
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class Tabs {

@Element()
el!: HTMLElement;
private mutationO?: MutationObserver;

/**
* Child Elements/Tab Items
Expand All @@ -31,6 +32,11 @@ export class Tabs {
@State()
activeChildClass = '';

init() {
this.tabs = Array.from(this.el.querySelectorAll('fw-tab'));
this.displayTab(0);
}

displayTab(index: number) {
this.activeTabIndex = index;
this.tabs = this.tabs?.map((tab, i) => {
Expand All @@ -40,8 +46,21 @@ export class Tabs {
}

componentWillLoad() {
this.tabs = Array.from(this.el.querySelectorAll('fw-tab'));
this.displayTab(0);
this.init();
}

connectedCallback() {
this.mutationO = new MutationObserver(() => {
this.init();
});
this.mutationO.observe(this.el, { childList: true });
}

disconnectedCallback() {
if (this.mutationO) {
this.mutationO.disconnect();
this.mutationO = undefined;
}
}

render() {
Expand Down

0 comments on commit c1fe804

Please sign in to comment.