Skip to content

Commit

Permalink
Allow to use Tab.js with list-group (twbs#21756)
Browse files Browse the repository at this point in the history
* Allow to use Tab.js with list-group

* Allow to use list-group with div parent instead of an ul parent
  • Loading branch information
Johann-S authored and mdo committed Mar 18, 2017
1 parent ab77282 commit 1a46d8c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
12 changes: 9 additions & 3 deletions js/src/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const Tab = (($) => {
A : 'a',
LI : 'li',
DROPDOWN : '.dropdown',
LIST : 'ul:not(.dropdown-menu), ol:not(.dropdown-menu), nav:not(.dropdown-menu)',
FADE_CHILD : '> .nav-item .fade, > .fade',
LIST : 'ul:not(.dropdown-menu), ol:not(.dropdown-menu), nav:not(.dropdown-menu), .list-group:not(.dropdown-menu)',
FADE_CHILD : '> .nav-item .fade, > .list-group-item .fade, > .fade',
ACTIVE : '.active',
ACTIVE_CHILD : '> .nav-item > .active, > .active',
ACTIVE_CHILD : '> .nav-item > .active, > .list-group-item > .active, > .active',
DATA_TOGGLE : '[data-toggle="tab"], [data-toggle="pill"]',
DROPDOWN_TOGGLE : '.dropdown-toggle',
DROPDOWN_ACTIVE_CHILD : '> .dropdown-menu .active'
Expand Down Expand Up @@ -182,6 +182,9 @@ const Tab = (($) => {
_transitionComplete(element, active, isTransitioning, callback) {
if (active) {
$(active).removeClass(ClassName.ACTIVE)
if ($(active).hasClass('list-group-item')) {
$(active).find('a.nav-link').removeClass(ClassName.ACTIVE)
}

const dropdownChild = $(active.parentNode).find(
Selector.DROPDOWN_ACTIVE_CHILD
Expand All @@ -195,6 +198,9 @@ const Tab = (($) => {
}

$(element).addClass(ClassName.ACTIVE)
if ($(element.parentNode).hasClass('list-group-item')) {
$(element.parentNode).addClass(ClassName.ACTIVE)
}
element.setAttribute('aria-expanded', true)

if (isTransitioning) {
Expand Down
16 changes: 16 additions & 0 deletions js/tests/unit/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ $(function () {
assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home')
})

QUnit.test('should activate element by tab id in list-group', function (assert) {
assert.expect(2)
var ulHTML = '<ul class="list-group">' +
'<li class="list-group-item"><a href="#home">Home</a></li>' +
'<li class="list-group-item"><a href="#profile">Profile</a></li>' +
'</ul>'

$('<ul><li id="home"></li><li id="profile"></div></li>').appendTo('#qunit-fixture')

$(ulHTML).find('li:last a').bootstrapTab('show')
assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile')

$(ulHTML).find('li:first a').bootstrapTab('show')
assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home')
})

QUnit.test('should not fire shown when show is prevented', function (assert) {
assert.expect(1)
var done = assert.async()
Expand Down
35 changes: 35 additions & 0 deletions js/tests/visual/tab.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,41 @@ <h4>Tabs with nav (with fade)</h4>
<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>

<h4>Tabs with list group (with fade)</h4>
<ul class="list-group" role="tablist">
<li class="list-group-item active">
<a class="nav-link active" data-toggle="tab" href="#home6" role="tab">Home</a>
</li>
<li class="list-group-item">
<a class="nav-link" data-toggle="tab" href="#profile6" role="tab">Profile</a>
</li>
<li class="list-group-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="dropdown6" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdown6">
<a class="dropdown-item" data-toggle="tab" href="#fat6" role="tab">@fat</a>
<a class="dropdown-item" data-toggle="tab" href="#mdo6" role="tab">@mdo</a>
</div>
</li>
</ul>
<div class="tab-content" role="tabpanel">
<div role="tabpanel" class="tab-pane active" id="home6">
<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 role="tabpanel" class="tab-pane fade" id="profile6">
<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 fade" id="fat6" role="tabpanel">
<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 fade" id="mdo6" role="tabpanel">
<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>

<script src="../../../docs/assets/js/vendor/jquery-slim.min.js"></script>
Expand Down

0 comments on commit 1a46d8c

Please sign in to comment.