Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

fix(tab): fix tab remove when used with ng-repeat #5689

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 8 additions & 2 deletions src/tabs/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ angular.module('ui.bootstrap.tabs', [])
};

ctrl.removeTab = function removeTab(tab) {
var index = findTabIndex(tab.index);
var index;
for (var i = 0; i < ctrl.tabs.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

forEach?

if (ctrl.tabs[i].tab === tab) {
index = i;
break;
}
}

if (tab.index === ctrl.active) {
if (ctrl.tabs[index].index === ctrl.active) {
var newActiveTabIndex = index === ctrl.tabs.length - 1 ?
index - 1 : index + 1 % ctrl.tabs.length;
ctrl.select(newActiveTabIndex);
Expand Down
25 changes: 25 additions & 0 deletions src/tabs/test/tabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,31 @@ describe('tabs', function() {
expect(contents().eq(1)).toHaveClass('active');
}));

it('should use updated index in tab', inject(function($controller, $compile, $rootScope) {
scope = $rootScope.$new();
elm = $compile('<uib-tabset active="active"><uib-tab index="0" heading="1">Hello</uib-tab><uib-tab index="$index + 1" ng-repeat="i in list" heading="tab {{i}}">content {{i}}</uib-tab></uib-tabset>')(scope);
scope.$apply();

scope.$apply('list = [1,2,3]');
expectTitles(['1', 'tab 1', 'tab 2', 'tab 3']);
expectContents(['Hello', 'content 1', 'content 2', 'content 3']);

// Remove middle "tab 2" tab
scope.$apply('list = [1,3]');
expectTitles(['1', 'tab 1', 'tab 3']);
expectContents(['Hello', 'content 1', 'content 3']);

// Remove last "tab 3" tab
scope.$apply('list = [1]');
expectTitles(['1', 'tab 1']);
expectContents(['Hello', 'content 1']);

// Select first tab
titles().find('> a').eq(0).click();
expect(titles().eq(0)).toHaveClass('active');
expect(contents().eq(0)).toHaveClass('active');
}));

it('should not select tabs when being destroyed', inject(function($controller, $compile, $rootScope) {
var selectList = [],
deselectList = [],
Expand Down