Skip to content

Commit 0bd1519

Browse files
committed
feat(tabs/bottomNav): routing to support programmatic url change #4297
1 parent efbd200 commit 0bd1519

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

projects/igniteui-angular/src/lib/tabbar/tabbar.component.ts

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
HostListener,
1515
Input,
1616
NgModule,
17+
OnDestroy,
1718
Output,
1819
QueryList,
1920
TemplateRef,
@@ -66,7 +67,7 @@ export class IgxTabTemplateDirective {
6667
}
6768
`]
6869
})
69-
export class IgxBottomNavComponent implements AfterViewInit {
70+
export class IgxBottomNavComponent implements AfterViewInit, OnDestroy {
7071

7172
/**
7273
* Gets the `IgxTabComponent` elements in the tab bar component.
@@ -172,32 +173,44 @@ export class IgxBottomNavComponent implements AfterViewInit {
172173
*/
173174
public ngAfterViewInit() {
174175
// initial selection
175-
this._navigationEndSubscription = this.router.events.pipe(
176-
filter(event => event instanceof NavigationEnd)).subscribe((args) => {
177-
const panelsArray = this.panels.toArray();
178-
for (let i = 0; i < panelsArray.length; i++) {
179-
if (panelsArray[i].routerLinkDirective &&
180-
this.router.url.startsWith(panelsArray[i].routerLinkDirective.urlTree.toString())) {
181-
panelsArray[i].selectAndUpdateRoute(false);
182-
break;
183-
}
184-
}
185-
this._navigationEndSubscription.unsubscribe();
186-
}
187-
);
188-
189176
setTimeout(() => {
177+
this.navigationEndHandler(this);
190178
if (this.selectedIndex === -1) {
191179
const selectablePanels = this.panels.filter((p) => !p.disabled);
192180
const panel = selectablePanels[0];
193-
194181
if (panel) {
195182
panel.select();
196183
}
197184
}
185+
this._navigationEndSubscription = this.router.events.pipe(
186+
filter(event => event instanceof NavigationEnd)).subscribe(() => {
187+
this.navigationEndHandler(this);
188+
}
189+
);
198190
}, 0);
199191
}
200192

193+
/**
194+
*@hidden
195+
*/
196+
public navigationEndHandler(bottomNavControl: IgxBottomNavComponent) {
197+
const panelsArray = bottomNavControl.panels.toArray();
198+
for (let i = 0; i < panelsArray.length; i++) {
199+
if (panelsArray[i].routerLinkDirective &&
200+
bottomNavControl.router.url.startsWith(panelsArray[i].routerLinkDirective.urlTree.toString())) {
201+
panelsArray[i]._selectAndEmitEvent();
202+
break;
203+
}
204+
}
205+
}
206+
207+
/**
208+
*@hidden
209+
*/
210+
public ngOnDestroy() {
211+
this._navigationEndSubscription.unsubscribe();
212+
}
213+
201214
/**
202215
*@hidden
203216
*/
@@ -413,20 +426,21 @@ export class IgxTabPanelComponent implements AfterContentInit, AfterViewChecked
413426
* @memberof IgxTabPanelComponent
414427
*/
415428
public select() {
416-
this.selectAndUpdateRoute(true);
429+
if (this.disabled || this._tabBar.selectedIndex === this.index) {
430+
return;
431+
}
432+
if (this.routerLinkDirective) {
433+
this.routerLinkDirective.onClick();
434+
} else {
435+
this._selectAndEmitEvent();
436+
}
417437
}
418438

419439
/**
420440
*@hidden
421441
*/
422-
public selectAndUpdateRoute(updateRoute: boolean) {
423-
if (this.disabled || this._tabBar.selectedIndex === this.index) {
424-
return;
425-
}
442+
public _selectAndEmitEvent() {
426443
this.isSelected = true;
427-
if (updateRoute && this.routerLinkDirective) {
428-
this.routerLinkDirective.onClick();
429-
}
430444
this._tabBar.onTabSelected.emit({ tab: this._tabBar.tabs.toArray()[this.index], panel: this });
431445
}
432446
}

0 commit comments

Comments
 (0)