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

Commit d1553a4

Browse files
RobJacobsdeeg
authored andcommitted
fix(tab): make active optional
This change makes the active binding added under #5425 optional. Closes #5489
1 parent 8b3e86f commit d1553a4

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/tabs/tabs.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ angular.module('ui.bootstrap.tabs', [])
8888
replace: true,
8989
scope: {},
9090
bindToController: {
91-
active: '=',
91+
active: '=?',
9292
type: '@'
9393
},
9494
controller: 'UibTabsetController',
@@ -101,6 +101,9 @@ angular.module('ui.bootstrap.tabs', [])
101101
scope.$parent.$eval(attrs.vertical) : false;
102102
scope.justified = angular.isDefined(attrs.justified) ?
103103
scope.$parent.$eval(attrs.justified) : false;
104+
if (angular.isUndefined(attrs.active)) {
105+
scope.active = 0;
106+
}
104107
}
105108
};
106109
})
@@ -115,7 +118,7 @@ angular.module('ui.bootstrap.tabs', [])
115118
transclude: true,
116119
scope: {
117120
heading: '@',
118-
index: '=',
121+
index: '=?',
119122
onSelect: '&select', //This callback is called in contentHeadingTransclude
120123
//once it inserts the tab's content into the dom
121124
onDeselect: '&deselect'
@@ -132,6 +135,14 @@ angular.module('ui.bootstrap.tabs', [])
132135
});
133136
}
134137

138+
if (angular.isUndefined(attrs.index)) {
139+
if (tabsetCtrl.tabs && tabsetCtrl.tabs.length) {
140+
scope.index = Math.max.apply(null, tabsetCtrl.tabs.map(function(t) { return t.index; })) + 1;
141+
} else {
142+
scope.index = 0;
143+
}
144+
}
145+
135146
scope.select = function() {
136147
if (!scope.disabled) {
137148
var index;

src/tabs/test/tabs.spec.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,41 @@ describe('tabs', function() {
153153
});
154154
});
155155

156+
describe('without active binding and index attributes', function() {
157+
beforeEach(inject(function($compile, $rootScope) {
158+
scope = $rootScope.$new();
159+
scope.first = '1';
160+
scope.second = '2';
161+
elm = $compile([
162+
'<uib-tabset>',
163+
' <uib-tab heading="First Tab {{first}}">',
164+
' first content is {{first}}',
165+
' </uib-tab>',
166+
' <uib-tab heading="Second Tab {{second}}">',
167+
' second content is {{second}}',
168+
' </uib-tab>',
169+
'</uib-tabset>'
170+
].join('\n'))(scope);
171+
scope.$apply();
172+
return elm;
173+
}));
174+
175+
it('should bind tabs content and set first tab active', function() {
176+
expectContents(['first content is 1', 'second content is 2']);
177+
expect(titles().eq(0)).toHaveClass('active');
178+
expect(titles().eq(1)).not.toHaveClass('active');
179+
expect(elm.controller('uibTabset').active).toBe(0);
180+
});
181+
182+
it('should change active on click', function() {
183+
titles().eq(1).find('> a').click();
184+
expect(contents().eq(1)).toHaveClass('active');
185+
expect(titles().eq(0)).not.toHaveClass('active');
186+
expect(titles().eq(1)).toHaveClass('active');
187+
expect(elm.controller('uibTabset').active).toBe(1);
188+
});
189+
});
190+
156191
describe('tab callback order', function() {
157192
var execOrder;
158193
beforeEach(inject(function($compile, $rootScope) {
@@ -579,7 +614,7 @@ describe('tabs', function() {
579614
describe('remove', function() {
580615
it('should remove title tabs when elements are destroyed and change selection', inject(function($controller, $compile, $rootScope) {
581616
scope = $rootScope.$new();
582-
elm = $compile('<uib-tabset active="active"><uib-tab heading="1">Hello</uib-tab><uib-tab index="$index" ng-repeat="i in list" heading="tab {{i}}">content {{i}}</uib-tab></uib-tabset>')(scope);
617+
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);
583618
scope.$apply();
584619

585620
expectTitles(['1']);

0 commit comments

Comments
 (0)