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

Commit 7474c47

Browse files
jroxendalbekos
authored andcommitted
fix(tabs): fire deselect before select callback
Closes #1557 Closes #1566
1 parent a33bac9 commit 7474c47

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

src/tabs/tabs.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@ angular.module('ui.bootstrap.tabs', [])
1313
var ctrl = this,
1414
tabs = ctrl.tabs = $scope.tabs = [];
1515

16-
ctrl.select = function(tab) {
16+
ctrl.select = function(selectedTab) {
1717
angular.forEach(tabs, function(tab) {
18-
tab.active = false;
18+
if (tab.active && tab !== selectedTab) {
19+
tab.active = false;
20+
tab.onDeselect();
21+
}
1922
});
20-
tab.active = true;
23+
selectedTab.active = true;
24+
selectedTab.onSelect();
2125
};
2226

2327
ctrl.addTab = function addTab(tab) {
2428
tabs.push(tab);
25-
if (tabs.length === 1 || tab.active) {
29+
// we can't run the select function on the first tab
30+
// since that would select it twice
31+
if (tabs.length === 1) {
32+
tab.active = true;
33+
} else if (tab.active) {
2634
ctrl.select(tab);
2735
}
2836
};
@@ -206,9 +214,6 @@ angular.module('ui.bootstrap.tabs', [])
206214
setActive(scope.$parent, active);
207215
if (active) {
208216
tabsetCtrl.select(scope);
209-
scope.onSelect();
210-
} else {
211-
scope.onDeselect();
212217
}
213218
});
214219

src/tabs/test/tabs.spec.js

+48-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('tabs', function() {
7373
expect(titles().eq(0)).toHaveClass('active');
7474
expect(titles().eq(1)).not.toHaveClass('active');
7575
expect(scope.actives.one).toBe(true);
76-
expect(scope.actives.two).toBe(false);
76+
expect(scope.actives.two).toBeFalsy();
7777
});
7878

7979
it('should change active on click', function() {
@@ -99,7 +99,6 @@ describe('tabs', function() {
9999
titles().eq(1).find('a').click();
100100
expect(scope.deselectFirst).toHaveBeenCalled();
101101
});
102-
103102
});
104103

105104
describe('basics with initial active tab', function() {
@@ -153,6 +152,48 @@ describe('tabs', function() {
153152
});
154153
});
155154

155+
describe('tab callback order', function() {
156+
var execOrder;
157+
beforeEach(inject(function($compile, $rootScope) {
158+
scope = $rootScope.$new();
159+
execOrder = [];
160+
scope.actives = {};
161+
162+
scope.execute = function(id) {
163+
execOrder.push(id);
164+
};
165+
166+
elm = $compile([
167+
'<div>',
168+
' <tabset class="hello" data-pizza="pepperoni">',
169+
' <tab heading="First Tab" active="actives.one" select="execute(\'select1\')" deselect="execute(\'deselect1\')"></tab>',
170+
' <tab select="execute(\'select2\')" deselect="execute(\'deselect2\')"></tab>',
171+
' </tabset>',
172+
'</div>'
173+
].join('\n'))(scope);
174+
scope.$apply();
175+
return elm;
176+
}));
177+
178+
it('should call select for the first tab', function() {
179+
expect(execOrder).toEqual([ 'select1' ]);
180+
});
181+
182+
it('should call deselect, then select', function() {
183+
execOrder = [];
184+
185+
// Select second tab
186+
titles().eq(1).find('a').click();
187+
expect(execOrder).toEqual([ 'deselect1', 'select2' ]);
188+
189+
execOrder = [];
190+
191+
// Select again first tab
192+
titles().eq(0).find('a').click();
193+
expect(execOrder).toEqual([ 'deselect2', 'select1' ]);
194+
});
195+
});
196+
156197
describe('ng-repeat', function() {
157198

158199
beforeEach(inject(function($compile, $rootScope) {
@@ -346,7 +387,11 @@ describe('tabs', function() {
346387

347388
describe('tabset controller', function() {
348389
function mockTab(isActive) {
349-
return { active: !!isActive };
390+
return {
391+
active: !!isActive,
392+
onSelect : angular.noop,
393+
onDeselect : angular.noop
394+
};
350395
}
351396

352397
var ctrl;

0 commit comments

Comments
 (0)