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

Commit

Permalink
feat(tabs): setting md-selected to -1 will allow md-tabs to fun…
Browse files Browse the repository at this point in the history
…ction without forcing a tab to be selected

Closes #3172
  • Loading branch information
Robert Messerle committed Jul 21, 2015
1 parent 9931e2a commit 27783df
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/components/tabs/js/tabsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ function MdTabsController ($scope, $element, $window, $mdConstant, $mdTabInkRipp
* @returns {*}
*/
function getNearestSafeIndex (newIndex) {
if (newIndex === -1) return -1;
var maxOffset = Math.max(ctrl.tabs.length - newIndex, newIndex),
i, tab;
for (i = 0; i <= maxOffset; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/js/tabsDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function MdTabs ($mdTheming, $mdUtil, $compile) {
</div>\
</md-tabs-canvas>\
</md-tabs-wrapper>\
<md-tabs-content-wrapper ng-show="$mdTabsCtrl.hasContent">\
<md-tabs-content-wrapper ng-show="$mdTabsCtrl.hasContent && $mdTabsCtrl.selectedIndex >= 0">\
<md-tab-content\
id="tab-content-{{::tab.id}}"\
role="tabpanel"\
Expand Down
30 changes: 24 additions & 6 deletions src/components/tabs/tabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ describe('<md-tabs>', function () {
});
});

function setup (template) {
function setup (template, scope) {
var el;
inject(function ($compile, $rootScope) {
el = $compile(template)($rootScope.$new());
$rootScope.$apply();
newScope = $rootScope.$new();
for (var key in scope || {}) newScope[key] = scope[key];
el = $compile(template)(newScope);

This comment has been minimized.

Copy link
@ThomasBurleson

ThomasBurleson Jul 21, 2015

Contributor

@robertmesserle - I am pretty certain that here we should be using $mdCompile

This comment has been minimized.

Copy link
@robertmesserle

robertmesserle Jul 21, 2015

Contributor

I'll have to look at $mdCompile, but what would that give me?

This comment has been minimized.

Copy link
@ThomasBurleson

ThomasBurleson Jul 21, 2015

Contributor

Locals, promise-based resolves, async template urls, transformTemplate,
May need enhanced to support the cloned-scope feature you use above.

This comment has been minimized.

Copy link
@robertmesserle

robertmesserle Jul 21, 2015

Contributor

The only thing that would be of use to me here is locals; however, I don't see that in the docs for $mdCompile. But even if it was present, it wouldn't be worth rewriting all of the tests to unnecessarily rely on promises.

I think $mdCompile is only real useful if you have some need for async compiling.

This comment has been minimized.

Copy link
@ThomasBurleson

ThomasBurleson Jul 21, 2015

Contributor

kk.

newScope.$apply();
});
return el;
}
Expand Down Expand Up @@ -287,14 +289,30 @@ describe('<md-tabs>', function () {
</md-tab>\
</md-tabs>\
';
var element = setup(template);
var button = element.find('md-button');
var element = setup(template);
var button = element.find('md-button');

expect(button[0 ].tagName).toBe('MD-BUTTON');
expect(button[ 0 ].tagName).toBe('MD-BUTTON');

button.triggerHandler('click');

expect(element.scope().data).toBe(false);
});
});

describe('no tab selected', function () {
it('should allow cases where no tabs are selected', inject(function () {
var template = '\
<md-tabs md-selected="selectedIndex">\
<md-tab label="a">tab content</md-tab>\
<md-tab label="b">tab content</md-tab>\
</md-tabs>\
';
var element = setup(template, { selectedIndex: -1 });
var scope = element.scope();

expect(scope.selectedIndex).toBe(-1);
expect(element.find('md-tabs-content-wrapper').hasClass('ng-hide')).toBe(true);
}));
});
});

0 comments on commit 27783df

Please sign in to comment.