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 tab 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, which allows the user to directed to the selected tab
after some work is performed.

- Closes #5820
  • Loading branch information
ksieburg committed Apr 19, 2016
1 parent f0e661c commit ef58dd3
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 `selectedTabIndex` in template for expression. You may call `$event.preventDefault()` in this event handler to prevent a tab change from occurring. The `selectedTabIndex` 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,
selectedTabIndex: 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, selectedTabIndex)">',
' 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, selectedTabIndex)">',
' <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, selectedTabIndex)">',
' <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 ef58dd3

Please sign in to comment.