mirror of
https://github.com/google/material-design-lite.git
synced 2024-08-16 15:50:34 +03:00
Add public method setTab to set the active tab programmatically
This commit is contained in:
parent
2ae138280a
commit
20b941bb30
@ -110,6 +110,24 @@
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the active tab.
|
||||
*
|
||||
* @public
|
||||
* @param {Element|number} tab The tab element or index to set active.
|
||||
*/
|
||||
MaterialTabs.prototype.setTab = function(tab) {
|
||||
tab = (typeof tab === 'number') ? this.tabs_[tab] : tab;
|
||||
if (tab && tab.getAttribute('href').charAt(0) === '#') {
|
||||
var href = tab.href.split('#')[1];
|
||||
var panel = this.element_.querySelector('#' + href);
|
||||
this.resetTabState_();
|
||||
this.resetPanelState_();
|
||||
tab.classList.add(this.CssClasses_.ACTIVE_CLASS);
|
||||
panel.classList.add(this.CssClasses_.ACTIVE_CLASS);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize element.
|
||||
*/
|
||||
@ -141,12 +159,7 @@
|
||||
tab.addEventListener('click', function(e) {
|
||||
if (tab.getAttribute('href').charAt(0) === '#') {
|
||||
e.preventDefault();
|
||||
var href = tab.href.split('#')[1];
|
||||
var panel = ctx.element_.querySelector('#' + href);
|
||||
ctx.resetTabState_();
|
||||
ctx.resetPanelState_();
|
||||
tab.classList.add(ctx.CssClasses_.ACTIVE_CLASS);
|
||||
panel.classList.add(ctx.CssClasses_.ACTIVE_CLASS);
|
||||
ctx.setTab(tab);
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user