Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tabs): detach tab portal when tab hides from view #8486

Merged
merged 1 commit into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand this to change the tab and re-assert the the others are gone?

}));

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

Expand Down