diff --git a/js/src/collapse.js b/js/src/collapse.js index ed3c064b1665b5dccec5062914b67e06fc94018a..13c44502ca6bd9662862735ed64f920d67a055fc 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -57,7 +57,8 @@ const Collapse = (($) => { const Selector = { ACTIVES : '.card > .show, .card > .collapsing', - DATA_TOGGLE : '[data-toggle="collapse"]' + DATA_TOGGLE : '[data-toggle="collapse"]', + DATA_CHILDREN : 'data-children' } @@ -77,13 +78,20 @@ const Collapse = (($) => { `[data-toggle="collapse"][href="#${element.id}"],` + `[data-toggle="collapse"][data-target="#${element.id}"]` )) - this._parent = this._config.parent ? this._getParent() : null if (!this._config.parent) { this._addAriaAndCollapsedClass(this._element, this._triggerArray) } + this._selectorActives = Selector.ACTIVES + if (this._parent) { + const childrenSelector = this._parent.hasAttribute(Selector.DATA_CHILDREN) ? this._parent.getAttribute(Selector.DATA_CHILDREN) : null + if (childrenSelector !== null) { + this._selectorActives = `${childrenSelector} > .show, ${childrenSelector} > .collapsing` + } + } + if (this._config.toggle) { this.toggle() } @@ -124,7 +132,7 @@ const Collapse = (($) => { let activesData if (this._parent) { - actives = $.makeArray($(this._parent).find(Selector.ACTIVES)) + actives = $.makeArray($(this._parent).find(this._selectorActives)) if (!actives.length) { actives = null } diff --git a/js/tests/unit/collapse.js b/js/tests/unit/collapse.js index 7139304338a0d64c2ef652679484a0afdb7fda66..c39adacdb025c906299c38218686163549a0718a 100644 --- a/js/tests/unit/collapse.js +++ b/js/tests/unit/collapse.js @@ -490,4 +490,27 @@ $(function () { .bootstrapCollapse('show') }) + QUnit.test('should allow accordion to use children other than card', function (assert) { + assert.expect(2) + var done = assert.async() + var accordionHTML = '<div id="accordion" data-children=".item">' + + '<div class="item">' + + '<a id="linkTrigger" data-parent="#accordion" data-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>' + + '<div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingThree"></div>' + + '</div>' + + '<div class="item">' + + '<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>' + + '<div id="collapseTwo" class="collapse show" role="tabpanel" aria-labelledby="headingTwo"></div>' + + '</div>' + + '</div>' + + $(accordionHTML).appendTo('#qunit-fixture') + var $target = $('#linkTrigger') + $('#collapseOne').on('shown.bs.collapse', function () { + assert.ok($(this).hasClass('show')) + assert.ok(!$('#collapseTwo').hasClass('show')) + done() + }) + $target.trigger($.Event('click')) + }) })