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

Commit 03e6047

Browse files
brentahitiwesleycho
authored andcommitted
fix(tab): correctly identify index on removal
- Fix tab remove when tabs are destroyed in the middle Closes #5689
1 parent 449c0d1 commit 03e6047

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/tabs/tabs.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ angular.module('ui.bootstrap.tabs', [])
5555
};
5656

5757
ctrl.removeTab = function removeTab(tab) {
58-
var index = findTabIndex(tab.index);
58+
var index;
59+
for (var i = 0; i < ctrl.tabs.length; i++) {
60+
if (ctrl.tabs[i].tab === tab) {
61+
index = i;
62+
break;
63+
}
64+
}
5965

60-
if (tab.index === ctrl.active) {
66+
if (ctrl.tabs[index].index === ctrl.active) {
6167
var newActiveTabIndex = index === ctrl.tabs.length - 1 ?
6268
index - 1 : index + 1 % ctrl.tabs.length;
6369
ctrl.select(newActiveTabIndex);

src/tabs/test/tabs.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,31 @@ describe('tabs', function() {
670670
expect(contents().eq(1)).toHaveClass('active');
671671
}));
672672

673+
it('should use updated index in tab', inject(function($controller, $compile, $rootScope) {
674+
scope = $rootScope.$new();
675+
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);
676+
scope.$apply();
677+
678+
scope.$apply('list = [1,2,3]');
679+
expectTitles(['1', 'tab 1', 'tab 2', 'tab 3']);
680+
expectContents(['Hello', 'content 1', 'content 2', 'content 3']);
681+
682+
// Remove middle "tab 2" tab
683+
scope.$apply('list = [1,3]');
684+
expectTitles(['1', 'tab 1', 'tab 3']);
685+
expectContents(['Hello', 'content 1', 'content 3']);
686+
687+
// Remove last "tab 3" tab
688+
scope.$apply('list = [1]');
689+
expectTitles(['1', 'tab 1']);
690+
expectContents(['Hello', 'content 1']);
691+
692+
// Select first tab
693+
titles().find('> a').eq(0).click();
694+
expect(titles().eq(0)).toHaveClass('active');
695+
expect(contents().eq(0)).toHaveClass('active');
696+
}));
697+
673698
it('should not select tabs when being destroyed', inject(function($controller, $compile, $rootScope) {
674699
var selectList = [],
675700
deselectList = [],

0 commit comments

Comments
 (0)