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

Commit

Permalink
feat(tabs): pass the selected index to onDeselect
Browse files Browse the repository at this point in the history
-  Add the index of the tab the user attempted to open to the onDeselect call
Closes #5820
Closes #5821
  • Loading branch information
ksieburg authored and wesleycho committed Apr 19, 2016
1 parent f0e661c commit 241fea8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion 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. Supports $event in template for expression. You may call `$event.preventDefault()` in this event handler to prevent a tab change from occurring.
An optional expression called when tab is deactivated. Supports `$event` and `$selectedIndex` in template for expression. You may call `$event.preventDefault()` in this event handler to prevent a tab change from occurring. The `$selectedIndex` can be used to determine which tab was attempted to be opened.

* `disable`
<small class="badge">$</small>
Expand Down
3 changes: 2 additions & 1 deletion src/tabs/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ angular.module('ui.bootstrap.tabs', [])
var previousSelected = ctrl.tabs[previousIndex];
if (previousSelected) {
previousSelected.tab.onDeselect({
$event: evt
$event: evt,
$selectedIndex: index
});
if (evt && evt.isDefaultPrevented()) {
return;
Expand Down
9 changes: 6 additions & 3 deletions src/tabs/test/tabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ describe('tabs', function() {
};
elm = $compile([
'<uib-tabset class="hello" data-pizza="pepperoni" active="active">',
' <uib-tab index="1" heading="First Tab {{first}}" classes="{{firstClass}}" select="selectFirst($event)" deselect="deselectFirst($event)">',
' <uib-tab index="1" heading="First Tab {{first}}" classes="{{firstClass}}" select="selectFirst($event)" deselect="deselectFirst($event, $selectedIndex)">',
' first content is {{first}}',
' </uib-tab>',
' <uib-tab index="2" classes="{{secondClass}}" select="selectSecond($event)" deselect="deselectSecond($event)">',
' <uib-tab index="2" classes="{{secondClass}}" select="selectSecond($event)" deselect="deselectSecond($event, $selectedIndex)">',
' <uib-tab-heading><b>Second</b> Tab {{second}}</uib-tab-heading>',
' second content is {{second}}',
' </uib-tab>',
' <uib-tab index="3" classes="{{thirdClass}}" deselect="deselectThird($event)">',
' <uib-tab index="3" classes="{{thirdClass}}" deselect="deselectThird($event, $selectedIndex)">',
' <uib-tab-heading><b>Second</b> Tab {{third}}</uib-tab-heading>',
' third content is {{third}}',
' </uib-tab>',
Expand Down Expand Up @@ -118,12 +118,15 @@ describe('tabs', 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]);
expect(scope.deselectFirst.calls.argsFor(0)[1]).toBe(1);
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]);
expect(scope.deselectSecond.calls.argsFor(0)[1]).toBe(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]);
expect(scope.deselectFirst.calls.argsFor(1)[1]).toBe(1);
});

it('should prevent tab deselection when $event.preventDefault() is called', function() {
Expand Down

0 comments on commit 241fea8

Please sign in to comment.