Skip to content

Commit 586cef3

Browse files
Jorge Vargasjorgevargas1989
Jorge Vargas
authored andcommitted
fix(tab): Fix mdcTabRouter activation detection for Ivy engine
Fixes #684
1 parent 4bc34b1 commit 586cef3

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

bundle/src/components/tabs/mdc.tab.router.directive.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
import { AfterContentInit, ChangeDetectorRef, ContentChild, ContentChildren, EventEmitter, forwardRef, QueryList, Directive, ElementRef,
2-
HostBinding, HostListener, Input, OnChanges, OnDestroy, Optional, Output, Renderer2, Self } from '@angular/core';
3-
import { Subject } from 'rxjs';
4-
import { takeUntil } from 'rxjs/operators';
5-
import { NgControl } from '@angular/forms';
1+
import { ContentChildren, forwardRef, QueryList, Directive, ElementRef, Optional, Renderer2 } from '@angular/core';
62
import { Router, RouterLink, RouterLinkWithHref } from '@angular/router';
7-
import { MDCTabFoundation } from '@material/tabs';
8-
import { MdcTabAdapter } from './mdc.tab.adapter';
93
import { AbstractMdcTabDirective } from './mdc.tab.directive';
104
import { RouterActiveDetector } from '../utility/router.active.detector';
11-
import { asBoolean } from '../../utils/value.utils';
125
import { MdcEventRegistry } from '../../utils/mdc.event.registry';
136

147
@Directive({
@@ -21,7 +14,8 @@ export class MdcTabRouterDirective extends AbstractMdcTabDirective {
2114
@ContentChildren(RouterLinkWithHref, {descendants: true}) _linksWithHrefs: QueryList<RouterLinkWithHref>;
2215
private routerActive: RouterActiveDetector;
2316

24-
constructor(rndr: Renderer2, root: ElementRef, registry: MdcEventRegistry, private router: Router, private cdr: ChangeDetectorRef) {
17+
constructor(rndr: Renderer2, root: ElementRef, registry: MdcEventRegistry, private router: Router,
18+
@Optional() private link?: RouterLink, @Optional() private linkWithHref?: RouterLinkWithHref) {
2519
super(rndr, root, registry);
2620
}
2721

@@ -33,10 +27,16 @@ export class MdcTabRouterDirective extends AbstractMdcTabDirective {
3327

3428
ngAfterContentInit(): void {
3529
super.ngAfterContentInit();
36-
this.routerActive = new RouterActiveDetector(this, this._links, this._linksWithHrefs, this.router, this.cdr);
30+
this.routerActive = new RouterActiveDetector(this, this._links, this._linksWithHrefs, this.router,
31+
this.link, this.linkWithHref);
3732
this.routerActive.init();
3833
}
3934

35+
ngOnChanges(): void {
36+
if (this.routerActive)
37+
this.routerActive.update();
38+
}
39+
4040
/** @docs-private */
4141
isRouterActive() {
4242
return this._active;

bundle/src/components/utility/router.active.detector.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectorRef, QueryList } from '@angular/core';
1+
import { Optional, QueryList } from '@angular/core';
22
import { Subject } from 'rxjs';
33
import { takeUntil } from 'rxjs/operators';
44
import { NavigationEnd, Router, RouterLink, RouterLinkWithHref } from '@angular/router';
@@ -14,7 +14,8 @@ export class RouterActiveDetector {
1414
private links: QueryList<RouterLink>,
1515
private linksWithHrefs: QueryList<RouterLinkWithHref>,
1616
private router: Router,
17-
private cdr: ChangeDetectorRef) {
17+
@Optional() private link?: RouterLink,
18+
@Optional() private linkWithHref?: RouterLinkWithHref) {
1819
router.events.pipe(takeUntil(this.onDestroy$)).subscribe(s => {
1920
if (s instanceof NavigationEnd) {
2021
this.update();
@@ -37,15 +38,20 @@ export class RouterActiveDetector {
3738

3839
public update(): void {
3940
if (!this.links || !this.linksWithHrefs || !this.router.navigated) return;
40-
const hasActiveLinks = this.hasActiveLinks();
41-
const active = this.component.isRouterActive();
42-
if (active !== hasActiveLinks) {
43-
this.component.setRouterActive(hasActiveLinks);
44-
}
41+
Promise.resolve().then(() => {
42+
const hasActiveLinks = this.hasActiveLinks();
43+
const active = this.component.isRouterActive();
44+
if (active !== hasActiveLinks) {
45+
this.component.setRouterActive(hasActiveLinks);
46+
}
47+
});
4548
}
4649

4750
private hasActiveLinks(): boolean {
48-
return this.links.some(this.isLinkActive(this.router)) || this.linksWithHrefs.some(this.isLinkActive(this.router));
51+
return (this.link && this.isLinkActive(this.router)(this.link)) ||
52+
(this.linkWithHref && this.isLinkActive(this.router)(this.linkWithHref)) ||
53+
this.links.some(this.isLinkActive(this.router)) ||
54+
this.linksWithHrefs.some(this.isLinkActive(this.router));
4955
}
5056

5157
private isLinkActive(router: Router): (link: (RouterLink | RouterLinkWithHref)) => boolean {

0 commit comments

Comments
 (0)