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

Commit

Permalink
fix(tab): fix tab remove when used with ng-repeat
Browse files Browse the repository at this point in the history
- Fix tab remove when tabs are destroyed in the middle using filters with ng-repeat for example
  • Loading branch information
brentahiti committed Mar 24, 2016
1 parent c534cb4 commit c071bb4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
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++) {
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
27 changes: 26 additions & 1 deletion src/tabs/test/tabs.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('tabs', function() {
fdescribe('tabs', function() {
var elm, scope;

beforeEach(module('ui.bootstrap.tabs'));
Expand Down 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

0 comments on commit c071bb4

Please sign in to comment.