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

Commit 4bc4f62

Browse files
committed
fix(tab): add support for tab deselect prevention
* add ability for user to prevent currently selected tab's deselection by calling `$event.preventDefault()` in tab's `deselect` callback. Fixes #5720 Addresses #2715, #4836, and #5716.
1 parent c83d0a8 commit 4bc4f62

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

Diff for: src/tabs/docs/readme.md

+1-1
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. Supports $event in template for expression.
36+
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.
3737

3838
* `disable`
3939
<small class="badge">$</small>

Diff for: src/tabs/tabs.js

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ angular.module('ui.bootstrap.tabs', [])
1313
previousSelected.tab.onDeselect({
1414
$event: evt
1515
});
16+
if (evt && evt.defaultPrevented) {
17+
return;
18+
}
1619
previousSelected.tab.active = false;
1720
}
1821

Diff for: src/tabs/test/tabs.spec.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ describe('tabs', function() {
3333
scope = $rootScope.$new();
3434
scope.first = '1';
3535
scope.second = '2';
36+
scope.third = '3';
3637
scope.active = 1;
3738
scope.firstClass = 'first-class';
3839
scope.secondClass = 'second-class-1 second-class-2';
3940
scope.selectFirst = jasmine.createSpy();
4041
scope.selectSecond = jasmine.createSpy();
4142
scope.deselectFirst = jasmine.createSpy();
4243
scope.deselectSecond = jasmine.createSpy();
44+
scope.deselectThird = function($event) {
45+
$event.preventDefault();
46+
};
4347
elm = $compile([
4448
'<uib-tabset class="hello" data-pizza="pepperoni" active="active">',
4549
' <uib-tab index="1" heading="First Tab {{first}}" classes="{{firstClass}}" select="selectFirst($event)" deselect="deselectFirst($event)">',
@@ -49,6 +53,10 @@ describe('tabs', function() {
4953
' <uib-tab-heading><b>Second</b> Tab {{second}}</uib-tab-heading>',
5054
' second content is {{second}}',
5155
' </uib-tab>',
56+
' <uib-tab index="3" classes="{{thirdClass}}" deselect="deselectThird($event)">',
57+
' <uib-tab-heading><b>Second</b> Tab {{third}}</uib-tab-heading>',
58+
' third content is {{third}}',
59+
' </uib-tab>',
5260
'</uib-tabset>'
5361
].join('\n'))(scope);
5462
scope.$apply();
@@ -65,15 +73,15 @@ describe('tabs', function() {
6573

6674
it('should create clickable titles', function() {
6775
var t = titles();
68-
expect(t.length).toBe(2);
76+
expect(t.length).toBe(3);
6977
expect(t.find('> a').eq(0).text()).toBe('First Tab 1');
7078
//It should put the uib-tab-heading element into the 'a' title
7179
expect(t.find('> a').eq(1).children().is('uib-tab-heading')).toBe(true);
7280
expect(t.find('> a').eq(1).children().html()).toBe('<b>Second</b> Tab 2');
7381
});
7482

7583
it('should bind tabs content and set first tab active', function() {
76-
expectContents(['first content is 1', 'second content is 2']);
84+
expectContents(['first content is 1', 'second content is 2', 'third content is 3']);
7785
expect(titles().eq(0)).toHaveClass('active');
7886
expect(titles().eq(1)).not.toHaveClass('active');
7987
expect(scope.active).toBe(1);
@@ -117,6 +125,16 @@ describe('tabs', function() {
117125
expect(scope.deselectFirst.calls.count()).toBe(2);
118126
expect(scope.deselectFirst.calls.argsFor(1)[0].target).toBe(titles().eq(1).find('> a')[0]);
119127
});
128+
129+
it('should prevent tab deselection when $event.preventDefault() is called', function() {
130+
spyOn(scope, 'deselectThird');
131+
titles().eq(2).find('> a').click();
132+
expect(scope.active).toBe(3);
133+
titles().eq(1).find('> a').click();
134+
expect(scope.deselectThird).toHaveBeenCalled();
135+
expect(scope.active).not.toBe(1);
136+
expect(scope.active).toBe(2);
137+
});
120138
});
121139

122140
describe('basics with initial active tab', function() {

0 commit comments

Comments
 (0)