Skip to content

Commit bbbb21b

Browse files
committed
feat(tabs): adding routing feature and test for tabs control #4297
1 parent 48cef4b commit bbbb21b

File tree

5 files changed

+51
-39
lines changed

5 files changed

+51
-39
lines changed

projects/igniteui-angular/src/lib/tabs/tabs-group.component.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,21 @@ export class IgxTabsGroupComponent implements IgxTabsGroupBase, AfterContentInit
169169
*```
170170
* @param focusDelay A number representing the expected delay.
171171
*/
172-
public select(focusDelay = 200, navigateToRoute = true): void {
172+
public select(focusDelay = 200): void {
173173
if (this.disabled || this.isSelected) {
174174
return;
175175
}
176+
if (this.routerLinkDirective) {
177+
this.routerLinkDirective.onClick();
178+
} else {
179+
this._selectAndEmitEvent(focusDelay);
180+
}
181+
}
176182

183+
/**
184+
* @hidden
185+
*/
186+
public _selectAndEmitEvent(focusDelay = 200) {
177187
this.isSelected = true;
178188
this.relatedTab.tabindex = 0;
179189

@@ -183,15 +193,9 @@ export class IgxTabsGroupComponent implements IgxTabsGroupBase, AfterContentInit
183193
}, focusDelay);
184194
}
185195
this.handleSelection();
186-
if (navigateToRoute && this.routerLinkDirective) {
187-
this.routerLinkDirective.onClick();
188-
}
189196
this._tabs.onTabItemSelected.emit({ tab: this._tabs.tabs.toArray()[this.index], group: this });
190197
}
191198

192-
public _selectAndEmitEvent() {
193-
}
194-
195199
private handleSelection(): void {
196200
const tabElement = this.relatedTab.nativeTabItem.nativeElement;
197201

projects/igniteui-angular/src/lib/tabs/tabs.component.spec.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -413,71 +413,67 @@ describe('IgxTabs', () => {
413413
expect(indicator.nativeElement.style.width).toBe('90px');
414414
}));
415415

416-
fit('should navigate to the correct URL when clicking on tab buttons', fakeAsync(() => {
417-
416+
it('should navigate to the correct URL when clicking on tab buttons', fakeAsync(() => {
418417
const router = TestBed.get(Router);
419418
const location = TestBed.get(Location);
420419
const fixture = TestBed.createComponent(TabsRoutingTestComponent);
421420
const tabsComp = fixture.componentInstance.tabsComp;
422421
fixture.detectChanges();
423422

424423
fixture.ngZone.run(() => { router.initialNavigation(); });
425-
426-
tick();
424+
tick(200);
427425
expect(location.path()).toBe('/view1');
428426

429427
fixture.ngZone.run(() => { tabsComp.tabs.toArray()[2].select(); });
430-
tick();
428+
tick(200);
431429
expect(location.path()).toBe('/view3');
432430

433431
fixture.ngZone.run(() => { tabsComp.tabs.toArray()[1].select(); });
434-
tick();
432+
tick(200);
435433
expect(location.path()).toBe('/view2');
436434

437435
fixture.ngZone.run(() => { tabsComp.tabs.toArray()[0].select(); });
438-
tick();
436+
tick(200);
439437
expect(location.path()).toBe('/view1');
440438
}));
441439

442-
fit('should select the correct tab button/panel when navigating an URL', fakeAsync(() => {
443-
440+
it('should select the correct tab button/panel when navigating an URL', fakeAsync(() => {
444441
const router = TestBed.get(Router);
445442
const location = TestBed.get(Location);
446443
const fixture = TestBed.createComponent(TabsRoutingTestComponent);
447444
const tabsComp = fixture.componentInstance.tabsComp;
448445
fixture.detectChanges();
449446

450447
fixture.ngZone.run(() => { router.initialNavigation(); });
451-
tick();
448+
tick(300);
452449
expect(location.path()).toBe('/view1');
453450
expect(tabsComp.selectedIndex).toBe(0);
454451
expect(tabsComp.groups.toArray()[0].isSelected).toBe(true);
455452
expect(tabsComp.tabs.toArray()[0].isSelected).toBe(true);
456453

457454
fixture.ngZone.run(() => { router.navigate(['/view3']); });
458-
tick();
455+
tick(300);
459456
expect(location.path()).toBe('/view3');
460457
fixture.detectChanges();
461458
expect(tabsComp.selectedIndex).toBe(2);
462459
expect(tabsComp.groups.toArray()[2].isSelected).toBe(true);
463460
expect(tabsComp.tabs.toArray()[2].isSelected).toBe(true);
464461

465462
fixture.ngZone.run(() => { router.navigate(['/view2']); });
466-
tick();
463+
tick(300);
467464
expect(location.path()).toBe('/view2');
468465
fixture.detectChanges();
469466
expect(tabsComp.selectedIndex).toBe(1);
470467
expect(tabsComp.groups.toArray()[1].isSelected).toBe(true);
471468
expect(tabsComp.tabs.toArray()[1].isSelected).toBe(true);
472469

473470
fixture.ngZone.run(() => { router.navigate(['/view1']); });
474-
tick();
471+
tick(300);
475472
expect(location.path()).toBe('/view1');
476473
fixture.detectChanges();
477474
expect(tabsComp.selectedIndex).toBe(0);
478475
expect(tabsComp.groups.toArray()[0].isSelected).toBe(true);
479476
expect(tabsComp.tabs.toArray()[0].isSelected).toBe(true);
480-
481477
}));
482478
});
483479

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

+9-17
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ export class IgxTabsComponent implements IgxTabsBase, AfterViewInit, OnDestroy {
280280
*/
281281
public ngAfterViewInit() {
282282
// initially do not navigate to the route set on the tabs-groups to keep the url set by the user
283-
this.setSelectedGroup(false);
283+
this.navigationEndHandler(this);
284+
this.setSelectedGroup();
284285
this._groupChanges$ = this.groups.changes.subscribe(() => {
285286
this.resetSelectionOnCollectionChanged();
286287
});
@@ -312,27 +313,18 @@ export class IgxTabsComponent implements IgxTabsBase, AfterViewInit, OnDestroy {
312313
if (this._groupChanges$) {
313314
this._groupChanges$.unsubscribe();
314315
}
316+
if (this._navigationEndSubscription) {
317+
this._navigationEndSubscription.unsubscribe();
318+
}
315319
}
316320

317-
private setSelectedGroup(navigateToRoute = true): void {
321+
private setSelectedGroup(): void {
318322
requestAnimationFrame(() => {
319-
if (!navigateToRoute) {
320-
navigateToRoute = true;
321-
const groupsArray = this.groups.toArray();
322-
for (let i = 0; i < groupsArray.length; i++) {
323-
if (groupsArray[i].routerLinkDirective &&
324-
this.router.url.startsWith(groupsArray[i].routerLinkDirective.urlTree.toString())) {
325-
this._selectedIndex = i;
326-
navigateToRoute = false;
327-
break;
328-
}
329-
}
330-
}
331323
if (this.selectedIndex <= 0 || this.selectedIndex >= this.groups.length) {
332324
// if nothing is selected - select the first tabs group
333-
this.selectGroupByIndex(0, navigateToRoute);
325+
this.selectGroupByIndex(0);
334326
} else {
335-
this.selectGroupByIndex(this.selectedIndex, navigateToRoute);
327+
this.selectGroupByIndex(this.selectedIndex);
336328
}
337329
});
338330
}
@@ -356,7 +348,7 @@ export class IgxTabsComponent implements IgxTabsBase, AfterViewInit, OnDestroy {
356348
const group = selectableGroups[selectedIndex];
357349

358350
if (group) {
359-
group.select(0, navigateToRoute);
351+
group.select(0);
360352
}
361353
}
362354

src/app/tabs-routing/tabs-routing.sample.html

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<div>
2424
<router-outlet></router-outlet>
2525
</div>
26+
Programmatically change URL:<br>
27+
<button (click)="navigateUrl1()">/tabs-routing/view1</button><br>
28+
<button (click)="navigateUrl2()">/tabs-routing/view2</button><br>
29+
<button (click)="navigateUrl3()">/tabs-routing/view3</button>
2630
</div>
2731
</div>
2832
</div>
+17-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
import { Component } from '@angular/core';
2-
2+
import { Router } from '@angular/router';
33
@Component({
44
selector: 'app-tabs-routing-sample',
55
styleUrls: ['tabs-routing.sample.css'],
66
templateUrl: 'tabs-routing.sample.html'
77
})
88
export class TabsRoutingSampleComponent {
9+
10+
constructor(private router: Router) {
11+
}
12+
13+
public navigateUrl1() {
14+
this.router.navigateByUrl('/tabs-routing/view1');
15+
}
16+
17+
public navigateUrl2() {
18+
this.router.navigateByUrl('/tabs-routing/view2');
19+
}
20+
21+
public navigateUrl3() {
22+
this.router.navigateByUrl('/tabs-routing/view3');
23+
}
24+
925
}

0 commit comments

Comments
 (0)