Skip to content

fix(tabs): don't fire change event when amount of tabs changes #12097

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

Merged
merged 1 commit into from
Jul 11, 2018
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
14 changes: 14 additions & 0 deletions src/lib/tabs/tab-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,20 @@ describe('MatTabGroup', () => {
expect(component._tabs.toArray()[1].isActive).toBe(true);
});

it('should not fire `selectedTabChange` when the amount of tabs changes', fakeAsync(() => {
fixture.detectChanges();
fixture.componentInstance.selectedIndex = 1;
fixture.detectChanges();

// Add a new tab at the beginning.
spyOn(fixture.componentInstance, 'handleSelection');
fixture.componentInstance.tabs.unshift({label: 'New tab', content: 'at the start'});
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(fixture.componentInstance.handleSelection).not.toHaveBeenCalled();
}));

});

Expand Down
5 changes: 4 additions & 1 deletion src/lib/tabs/tab-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn
// Maintain the previously-selected tab if a new tab is added or removed.
for (let i = 0; i < tabs.length; i++) {
if (tabs[i].isActive) {
this._indexToSelect = i;
// Assign both to the `_indexToSelect` and `_selectedIndex` so we don't fire a changed
// event, otherwise the consumer may end up in an infinite loop in some edge cases like
// adding a tab within the `selectedIndexChange` event.
this._indexToSelect = this._selectedIndex = i;
break;
}
}
Expand Down