Skip to content

Commit

Permalink
fix(tabs): unnecessarily adding pagination when changing to new list …
Browse files Browse the repository at this point in the history
…of tabs with same labels (angular#16869)

These changes fix an issue where swapping out the list of tabs to one with the same labels causes the header to add a pagination when it doesn't have to. It looks like issue comes down to the fact that we check the element dimensions before they've had the chance to update. This works for the case where the tabs have different text, because we also have a `MutationObserver` that looks at them and it fires a bit later.

Fixes angular#16789.
  • Loading branch information
crisbeto authored Apr 24, 2020
1 parent 403a3d1 commit 6a64130
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/material/tabs/paginated-tab-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export abstract class MatPaginatedTabHeader implements AfterContentChecked, Afte
// On dir change or window resize, realign the ink bar and update the orientation of
// the key manager if the direction has changed.
merge(dirChange, resize, this._items.changes).pipe(takeUntil(this._destroyed)).subscribe(() => {
realign();
// We need to defer this to give the browser some time to recalculate the element dimensions.
Promise.resolve().then(realign);
this._keyManager.withHorizontalOrientation(this._getLayoutDirection());
});

Expand Down
10 changes: 6 additions & 4 deletions src/material/tabs/tab-nav-bar/tab-nav-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,29 @@ describe('MatTabNavBar', () => {
expect(tabLinkElement.classList).toContain('mat-tab-disabled');
});

it('should re-align the ink bar when the direction changes', () => {
it('should re-align the ink bar when the direction changes', fakeAsync(() => {
const inkBar = fixture.componentInstance.tabNavBar._inkBar;

spyOn(inkBar, 'alignToElement');

dirChange.next();
tick();
fixture.detectChanges();

expect(inkBar.alignToElement).toHaveBeenCalled();
});
}));

it('should re-align the ink bar when the tabs list change', () => {
it('should re-align the ink bar when the tabs list change', fakeAsync(() => {
const inkBar = fixture.componentInstance.tabNavBar._inkBar;

spyOn(inkBar, 'alignToElement');

fixture.componentInstance.tabs = [1, 2, 3, 4];
fixture.detectChanges();
tick();

expect(inkBar.alignToElement).toHaveBeenCalled();
});
}));

it('should re-align the ink bar when the tab labels change the width', done => {
const inkBar = fixture.componentInstance.tabNavBar._inkBar;
Expand Down

0 comments on commit 6a64130

Please sign in to comment.