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

Commit 360cd5c

Browse files
committed
fix(tabs): make tabs added with active=true be selected
1 parent 224bc2f commit 360cd5c

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

src/tabs/tabs.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function TabsetCtrl($scope, $element) {
3030

3131
ctrl.addTab = function addTab(tab) {
3232
tabs.push(tab);
33-
if (tabs.length == 1) {
33+
if (tabs.length == 1 || tab.active) {
3434
ctrl.select(tab);
3535
}
3636
};
@@ -186,16 +186,11 @@ function($parse, $http, $templateCache, $compile) {
186186
compile: function(elm, attrs, transclude) {
187187
return function postLink(scope, elm, attrs, tabsetCtrl) {
188188
var getActive, setActive;
189-
scope.active = false; // default value
190189
if (attrs.active) {
191190
getActive = $parse(attrs.active);
192191
setActive = getActive.assign;
193192
scope.$parent.$watch(getActive, function updateActive(value) {
194-
if ( !!value && scope.disabled ) {
195-
setActive(scope.$parent, false); // Prevent active assignment
196-
} else {
197-
scope.active = !!value;
198-
}
193+
scope.active = !!value;
199194
});
200195
} else {
201196
setActive = getActive = angular.noop;
@@ -226,13 +221,11 @@ function($parse, $http, $templateCache, $compile) {
226221
scope.$on('$destroy', function() {
227222
tabsetCtrl.removeTab(scope);
228223
});
229-
//If the tabset sets this tab to active, set the parent scope's active
230-
//binding too. We do this so the watch for the parent's initial active
231-
//value won't overwrite what is initially set by the tabset
232224
if (scope.active) {
233225
setActive(scope.$parent, true);
234226
}
235227

228+
236229
//We need to transclude later, once the content container is ready.
237230
//when this link happens, we're inside a tab heading.
238231
scope.$transcludeFn = transclude;

src/tabs/test/tabsSpec.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ describe('tabs', function() {
284284
});
285285

286286
describe('tabset controller', function() {
287-
function mockTab() {
288-
return { active: false };
287+
function mockTab(isActive) {
288+
return { active: !!isActive };
289289
}
290290

291291
var ctrl;
@@ -355,6 +355,16 @@ describe('tabs', function() {
355355
ctrl.addTab(tab2);
356356
expect(tab1.active).toBe(true);
357357
});
358+
359+
it('should select a tab added that\'s already active', function() {
360+
var tab1 = mockTab(), tab2 = mockTab(true);
361+
ctrl.addTab(tab1);
362+
expect(tab1.active).toBe(true);
363+
364+
ctrl.addTab(tab2);
365+
expect(tab1.active).toBe(false);
366+
expect(tab2.active).toBe(true);
367+
});
358368
});
359369
});
360370

@@ -434,14 +444,6 @@ describe('tabs', function() {
434444
expectTabActive(scope.tabs[2]);
435445
});
436446

437-
it('should not switch active when setting active=true', function() {
438-
scope.$apply('tabs[2].active = true');
439-
expectTabActive(scope.tabs[2]);
440-
441-
scope.$apply('tabs[3].active = true');
442-
expectTabActive(scope.tabs[2]);
443-
});
444-
445447
it('should toggle between states', function() {
446448
expect(titles().eq(3)).toHaveClass('disabled');
447449
scope.$apply('tabs[3].disabled = false');

0 commit comments

Comments
 (0)