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

Commit

Permalink
feat(tab): add select expressions
Browse files Browse the repository at this point in the history
- Add support for event being available in select/deselect
  callbacks

Closes #5438
  • Loading branch information
wesleycho committed Mar 14, 2016
1 parent 7f93bfc commit bb36e40
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/tabs/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AngularJS version of the tabs directive.

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

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

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

* `template-url`
_(Default: `uib/template/tabs/tab.html`)_ -
Expand Down
14 changes: 9 additions & 5 deletions src/tabs/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ angular.module('ui.bootstrap.tabs', [])
oldIndex;
ctrl.tabs = [];

ctrl.select = function(index) {
ctrl.select = function(index, evt) {
if (!destroyed) {
var previousIndex = findTabIndex(oldIndex);
var previousSelected = ctrl.tabs[previousIndex];
if (previousSelected) {
previousSelected.tab.onDeselect();
previousSelected.tab.onDeselect({
$event: evt
});
previousSelected.tab.active = false;
}

var selected = ctrl.tabs[index];
if (selected) {
selected.tab.onSelect();
selected.tab.onSelect({
$event: evt
});
selected.tab.active = true;
ctrl.active = selected.index;
oldIndex = selected.index;
Expand Down Expand Up @@ -148,7 +152,7 @@ angular.module('ui.bootstrap.tabs', [])
scope.classes = '';
}

scope.select = function() {
scope.select = function(evt) {
if (!scope.disabled) {
var index;
for (var i = 0; i < tabsetCtrl.tabs.length; i++) {
Expand All @@ -158,7 +162,7 @@ angular.module('ui.bootstrap.tabs', [])
}
}

tabsetCtrl.select(index);
tabsetCtrl.select(index, evt);
}
};

Expand Down
10 changes: 8 additions & 2 deletions src/tabs/test/tabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ describe('tabs', function() {
scope.deselectSecond = jasmine.createSpy();
elm = $compile([
'<uib-tabset class="hello" data-pizza="pepperoni" active="active">',
' <uib-tab index="1" heading="First Tab {{first}}" classes="{{firstClass}}" select="selectFirst()" deselect="deselectFirst()">',
' <uib-tab index="1" heading="First Tab {{first}}" classes="{{firstClass}}" select="selectFirst($event)" deselect="deselectFirst($event)">',
' first content is {{first}}',
' </uib-tab>',
' <uib-tab index="2" classes="{{secondClass}}" select="selectSecond()" deselect="deselectSecond()">',
' <uib-tab index="2" classes="{{secondClass}}" select="selectSecond($event)" deselect="deselectSecond($event)">',
' <uib-tab-heading><b>Second</b> Tab {{second}}</uib-tab-heading>',
' second content is {{second}}',
' </uib-tab>',
Expand Down Expand Up @@ -97,19 +97,25 @@ describe('tabs', function() {
});

it('should call select callback on select', function() {
expect(scope.selectFirst.calls.count()).toBe(1);
titles().eq(1).find('> a').click();
expect(scope.selectSecond).toHaveBeenCalled();
expect(scope.selectSecond.calls.argsFor(0)[0].target).toBe(titles().eq(1).find('> a')[0]);
titles().eq(0).find('> a').click();
expect(scope.selectFirst).toHaveBeenCalled();
expect(scope.selectFirst.calls.argsFor(1)[0].target).toBe(titles().eq(0).find('> a')[0]);
});

it('should call deselect callback on deselect', function() {
titles().eq(1).find('> a').click();
expect(scope.deselectFirst).toHaveBeenCalled();
expect(scope.deselectFirst.calls.argsFor(0)[0].target).toBe(titles().eq(1).find('> a')[0]);
titles().eq(0).find('> a').click();
expect(scope.deselectSecond).toHaveBeenCalled();
expect(scope.deselectSecond.calls.argsFor(0)[0].target).toBe(titles().eq(0).find('> a')[0]);
titles().eq(1).find('> a').click();
expect(scope.deselectFirst.calls.count()).toBe(2);
expect(scope.deselectFirst.calls.argsFor(1)[0].target).toBe(titles().eq(1).find('> a')[0]);
});
});

Expand Down
2 changes: 1 addition & 1 deletion template/tabs/tab.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<li ng-class="[{active: active, disabled: disabled}, classes]" class="uib-tab nav-item">
<a href ng-click="select()" class="nav-link" uib-tab-heading-transclude>{{heading}}</a>
<a href ng-click="select($event)" class="nav-link" uib-tab-heading-transclude>{{heading}}</a>
</li>

0 comments on commit bb36e40

Please sign in to comment.