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

Commit bb36e40

Browse files
committed
feat(tab): add select expressions
- Add support for event being available in select/deselect callbacks Closes #5438
1 parent 7f93bfc commit bb36e40

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

src/tabs/docs/readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ AngularJS version of the tabs directive.
3333

3434
* `deselect()`
3535
<small class="badge">$</small> -
36-
An optional expression called when tab is deactivated.
36+
An optional expression called when tab is deactivated. Supports $event in template for expression.
3737

3838
* `disable`
3939
<small class="badge">$</small>
@@ -49,7 +49,7 @@ AngularJS version of the tabs directive.
4949

5050
* `select()`
5151
<small class="badge">$</small> -
52-
An optional expression called when tab is activated.
52+
An optional expression called when tab is activated. Supports $event in template for expression.
5353

5454
* `template-url`
5555
_(Default: `uib/template/tabs/tab.html`)_ -

src/tabs/tabs.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ angular.module('ui.bootstrap.tabs', [])
55
oldIndex;
66
ctrl.tabs = [];
77

8-
ctrl.select = function(index) {
8+
ctrl.select = function(index, evt) {
99
if (!destroyed) {
1010
var previousIndex = findTabIndex(oldIndex);
1111
var previousSelected = ctrl.tabs[previousIndex];
1212
if (previousSelected) {
13-
previousSelected.tab.onDeselect();
13+
previousSelected.tab.onDeselect({
14+
$event: evt
15+
});
1416
previousSelected.tab.active = false;
1517
}
1618

1719
var selected = ctrl.tabs[index];
1820
if (selected) {
19-
selected.tab.onSelect();
21+
selected.tab.onSelect({
22+
$event: evt
23+
});
2024
selected.tab.active = true;
2125
ctrl.active = selected.index;
2226
oldIndex = selected.index;
@@ -148,7 +152,7 @@ angular.module('ui.bootstrap.tabs', [])
148152
scope.classes = '';
149153
}
150154

151-
scope.select = function() {
155+
scope.select = function(evt) {
152156
if (!scope.disabled) {
153157
var index;
154158
for (var i = 0; i < tabsetCtrl.tabs.length; i++) {
@@ -158,7 +162,7 @@ angular.module('ui.bootstrap.tabs', [])
158162
}
159163
}
160164

161-
tabsetCtrl.select(index);
165+
tabsetCtrl.select(index, evt);
162166
}
163167
};
164168

src/tabs/test/tabs.spec.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ describe('tabs', function() {
4242
scope.deselectSecond = jasmine.createSpy();
4343
elm = $compile([
4444
'<uib-tabset class="hello" data-pizza="pepperoni" active="active">',
45-
' <uib-tab index="1" heading="First Tab {{first}}" classes="{{firstClass}}" select="selectFirst()" deselect="deselectFirst()">',
45+
' <uib-tab index="1" heading="First Tab {{first}}" classes="{{firstClass}}" select="selectFirst($event)" deselect="deselectFirst($event)">',
4646
' first content is {{first}}',
4747
' </uib-tab>',
48-
' <uib-tab index="2" classes="{{secondClass}}" select="selectSecond()" deselect="deselectSecond()">',
48+
' <uib-tab index="2" classes="{{secondClass}}" select="selectSecond($event)" deselect="deselectSecond($event)">',
4949
' <uib-tab-heading><b>Second</b> Tab {{second}}</uib-tab-heading>',
5050
' second content is {{second}}',
5151
' </uib-tab>',
@@ -97,19 +97,25 @@ describe('tabs', function() {
9797
});
9898

9999
it('should call select callback on select', function() {
100+
expect(scope.selectFirst.calls.count()).toBe(1);
100101
titles().eq(1).find('> a').click();
101102
expect(scope.selectSecond).toHaveBeenCalled();
103+
expect(scope.selectSecond.calls.argsFor(0)[0].target).toBe(titles().eq(1).find('> a')[0]);
102104
titles().eq(0).find('> a').click();
103105
expect(scope.selectFirst).toHaveBeenCalled();
106+
expect(scope.selectFirst.calls.argsFor(1)[0].target).toBe(titles().eq(0).find('> a')[0]);
104107
});
105108

106109
it('should call deselect callback on deselect', function() {
107110
titles().eq(1).find('> a').click();
108111
expect(scope.deselectFirst).toHaveBeenCalled();
112+
expect(scope.deselectFirst.calls.argsFor(0)[0].target).toBe(titles().eq(1).find('> a')[0]);
109113
titles().eq(0).find('> a').click();
110114
expect(scope.deselectSecond).toHaveBeenCalled();
115+
expect(scope.deselectSecond.calls.argsFor(0)[0].target).toBe(titles().eq(0).find('> a')[0]);
111116
titles().eq(1).find('> a').click();
112117
expect(scope.deselectFirst.calls.count()).toBe(2);
118+
expect(scope.deselectFirst.calls.argsFor(1)[0].target).toBe(titles().eq(1).find('> a')[0]);
113119
});
114120
});
115121

template/tabs/tab.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<li ng-class="[{active: active, disabled: disabled}, classes]" class="uib-tab nav-item">
2-
<a href ng-click="select()" class="nav-link" uib-tab-heading-transclude>{{heading}}</a>
2+
<a href ng-click="select($event)" class="nav-link" uib-tab-heading-transclude>{{heading}}</a>
33
</li>

0 commit comments

Comments
 (0)