Skip to content

Commit

Permalink
fix(tabs): detach tab portal when tab hides from view (#8486)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephperrott authored and jelbourn committed Nov 20, 2017
1 parent 0f954a0 commit fbf2987
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/lib/tabs/tab-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,19 @@ export class MatTabBodyPortal extends _MatTabBodyPortalBaseClass implements OnIn
ngOnInit(): void {
if (this._host._isCenterPosition(this._host._position)) {
this.attach(this._host._content);
} else {
this._centeringSub = this._host._beforeCentering.subscribe(() => {
this.attach(this._host._content);
this._centeringSub.unsubscribe();
});
}
this._centeringSub = this._host._beforeCentering.subscribe((isCentering: boolean) => {
if (isCentering) {
if (!this.hasAttached()) {
this.attach(this._host._content);
}
} else {
this.detach();
}
});
}

/** Clean up subscription if necessary. */
/** Clean up centering subscription. */
ngOnDestroy(): void {
if (this._centeringSub && !this._centeringSub.closed) {
this._centeringSub.unsubscribe();
Expand Down Expand Up @@ -136,7 +140,7 @@ export class MatTabBody implements OnInit {
@Output() _onCentering: EventEmitter<number> = new EventEmitter<number>();

/** Event emitted before the centering of the tab begins. */
@Output() _beforeCentering: EventEmitter<number> = new EventEmitter<number>();
@Output() _beforeCentering: EventEmitter<boolean> = new EventEmitter<boolean>();

/** Event emitted when the tab completes its animation towards the center. */
@Output() _onCentered: EventEmitter<void> = new EventEmitter<void>(true);
Expand Down Expand Up @@ -185,8 +189,9 @@ export class MatTabBody implements OnInit {
}

_onTranslateTabStarted(e: AnimationEvent): void {
if (this._isCenterPosition(e.toState)) {
this._beforeCentering.emit();
const isCentering = this._isCenterPosition(e.toState);
this._beforeCentering.emit(isCentering);
if (isCentering) {
this._onCentering.emit(this._elementRef.nativeElement.clientHeight);
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/lib/tabs/tab-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,19 @@ describe('MatTabGroup', () => {
expect(fixture.componentInstance.legumes).toBeTruthy();
});

it('should only have the active tab in the DOM', async(() => {
expect(fixture.nativeElement.textContent).toContain('Pizza, fries');
expect(fixture.nativeElement.textContent).not.toContain('Peanuts');

tabGroup.selectedIndex = 3;
fixture.detectChanges();
// Use whenStable to wait for async observables and change detection to run in content.
fixture.whenStable().then(() => {
expect(fixture.nativeElement.textContent).not.toContain('Pizza, fries');
expect(fixture.nativeElement.textContent).toContain('Peanuts');
});
}));

it('should support setting the header position', () => {
let tabGroupNode = fixture.debugElement.query(By.css('mat-tab-group')).nativeElement;

Expand Down

0 comments on commit fbf2987

Please sign in to comment.