diff --git a/js/src/tab.js b/js/src/tab.js index 33f56faaee3e6f19dd2fbfa940c0ad0e55ad75db..f84663c69ffb305e605a37d6bcf121ea77e38c0a 100644 --- a/js/src/tab.js +++ b/js/src/tab.js @@ -37,7 +37,8 @@ const Tab = (($) => { DROPDOWN_MENU : 'dropdown-menu', ACTIVE : 'active', FADE : 'fade', - IN : 'in' + IN : 'in', + DISABLED : 'disabled' } const Selector = { @@ -77,15 +78,24 @@ const Tab = (($) => { // public show() { - if (this._element.parentNode && - (this._element.parentNode.nodeType === Node.ELEMENT_NODE) && - ($(this._element).hasClass(ClassName.ACTIVE))) { + let $thisEl = $(this._element) + + if ( + ( + this._element.parentNode && + (this._element.parentNode.nodeType === Node.ELEMENT_NODE) && + ($thisEl.hasClass(ClassName.ACTIVE)) + ) || ( + this._element.hasAttribute('disabled') || + $thisEl.hasClass(ClassName.DISABLED) + ) + ) { return } let target let previous - let ulElement = $(this._element).closest(Selector.UL)[0] + let ulElement = $thisEl.closest(Selector.UL)[0] let selector = Util.getSelectorFromElement(this._element) if (ulElement) { @@ -105,7 +115,7 @@ const Tab = (($) => { $(previous).trigger(hideEvent) } - $(this._element).trigger(showEvent) + $thisEl.trigger(showEvent) if (showEvent.isDefaultPrevented() || (hideEvent.isDefaultPrevented())) { @@ -131,7 +141,7 @@ const Tab = (($) => { }) $(previous).trigger(hiddenEvent) - $(this._element).trigger(shownEvent) + $thisEl.trigger(shownEvent) } if (target) { diff --git a/js/tests/unit/tab.js b/js/tests/unit/tab.js index 2e01432930b427d78f240522d443a1776d7ec4fe..29c1364c712143567669be7223949a381f57c3cb 100644 --- a/js/tests/unit/tab.js +++ b/js/tests/unit/tab.js @@ -1,5 +1,6 @@ +/* global QUnit */ $(function () { - 'use strict'; + 'use strict' QUnit.module('tabs plugin') @@ -225,4 +226,36 @@ $(function () { assert.strictEqual($tabs.find('a:not(.active)').attr('aria-expanded'), 'false', 'after second show event, hidden tab has aria-expanded = false') }) + QUnit.test('should not active element when disabled by class', function (assert) { + assert.expect(0) + + var tabsHTML = '<ul class="tabs">' + + '<li><a href="#home">Home</a></li>' + + '<li><a href="#profile" class="disabled">Profile</a></li>' + + '</ul>' + + $(tabsHTML) + .find('li:last a') + .on('show.bs.tab', function () { + assert.ok(false, 'show event fired') + }) + .bootstrapTab('show') + }) + + QUnit.test('should not active element when disabled by attribute', function (assert) { + assert.expect(0) + + var tabsHTML = '<ul class="tabs">' + + '<li><a href="#home">Home</a></li>' + + '<li><a href="#profile" disabled>Profile</a></li>' + + '</ul>' + + $(tabsHTML) + .find('li:last a') + .on('show.bs.tab', function () { + assert.ok(false, 'show event fired') + }) + .bootstrapTab('show') + }) + }) diff --git a/js/tests/visual/tab.html b/js/tests/visual/tab.html index d09622d72f1f6cac45e08b703e5917ec62c40d37..f3f255ca6a5cfd64810f2ea2e8a93a0dc5f07a62 100644 --- a/js/tests/visual/tab.html +++ b/js/tests/visual/tab.html @@ -150,6 +150,38 @@ </div> </div> + <h4>Tabs with disabled tab</h4> + + <ul id="myTab4" class="nav nav-tabs"> + <li class="nav-item"><a class="nav-link active" href="#home4" data-toggle="tab">Home</a></li> + <li class="nav-item"><a class="nav-link disabled" href="#profile4" data-toggle="tab">Profile</a></li> + <li class="dropdown nav-item"> + <a href="#" id="myTabDrop5" class="dropdown-toggle nav-link" data-toggle="dropdown">Dropdown <b class="caret"></b></a> + <div class="dropdown-menu" role="menu" aria-labelledby="myTabDrop5"> + <a class="dropdown-item" href="#dropdown4-1" tabindex="-1" data-toggle="tab">@fat</a> + <a class="dropdown-item" href="#dropdown4-2" tabindex="-1" data-toggle="tab">@mdo</a> + </div> + </li> + </ul> + <div id="myTabContent4" class="tab-content"> + <div class="tab-pane active" id="home4"> + <p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p> + <p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p> + </div> + <div class="tab-pane" id="profile4"> + <p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p> + <p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p> + </div> + <div class="tab-pane" id="dropdown4-1"> + <p>Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade. Messenger bag gentrify pitchfork tattooed craft beer, iphone skateboard locavore carles etsy salvia banksy hoodie helvetica. DIY synth PBR banksy irony. Leggings gentrify squid 8-bit cred pitchfork. Williamsburg banh mi whatever gluten-free, carles pitchfork biodiesel fixie etsy retro mlkshk vice blog. Scenester cred you probably haven't heard of them, vinyl craft beer blog stumptown. Pitchfork sustainable tofu synth chambray yr.</p> + <p>Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade. Messenger bag gentrify pitchfork tattooed craft beer, iphone skateboard locavore carles etsy salvia banksy hoodie helvetica. DIY synth PBR banksy irony. Leggings gentrify squid 8-bit cred pitchfork. Williamsburg banh mi whatever gluten-free, carles pitchfork biodiesel fixie etsy retro mlkshk vice blog. Scenester cred you probably haven't heard of them, vinyl craft beer blog stumptown. Pitchfork sustainable tofu synth chambray yr.</p> + </div> + <div class="tab-pane" id="dropdown4-2"> + <p>Trust fund seitan letterpress, keytar raw denim keffiyeh etsy art party before they sold out master cleanse gluten-free squid scenester freegan cosby sweater. Fanny pack portland seitan DIY, art party locavore wolf cliche high life echo park Austin. Cred vinyl keffiyeh DIY salvia PBR, banh mi before they sold out farm-to-table VHS viral locavore cosby sweater. Lomo wolf viral, mustache readymade thundercats keffiyeh craft beer marfa ethical. Wolf salvia freegan, sartorial keffiyeh echo park vegan.</p> + <p>Trust fund seitan letterpress, keytar raw denim keffiyeh etsy art party before they sold out master cleanse gluten-free squid scenester freegan cosby sweater. Fanny pack portland seitan DIY, art party locavore wolf cliche high life echo park Austin. Cred vinyl keffiyeh DIY salvia PBR, banh mi before they sold out farm-to-table VHS viral locavore cosby sweater. Lomo wolf viral, mustache readymade thundercats keffiyeh craft beer marfa ethical. Wolf salvia freegan, sartorial keffiyeh echo park vegan.</p> + </div> + </div> + </div> <!-- JavaScript Includes -->