Skip to content

Commit

Permalink
collapse children with no data-parent
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S committed Sep 10, 2018
1 parent de75bd0 commit 6e2a823
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
23 changes: 14 additions & 9 deletions js/src/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ const Collapse = (($) => {

if (this._parent) {
actives = [].slice.call(this._parent.querySelectorAll(Selector.ACTIVES))
.filter((elem) => elem.getAttribute('data-parent') === this._config.parent)
.filter((elem) => {
if (typeof this._config.parent === 'string') {
return elem.getAttribute('data-parent') === this._config.parent
}

return elem.classList.contains(ClassName.COLLAPSE)
})

if (actives.length === 0) {
actives = null
Expand Down Expand Up @@ -282,7 +288,8 @@ const Collapse = (($) => {
}

_getParent() {
let parent = null
let parent

if (Util.isElement(this._config.parent)) {
parent = this._config.parent

Expand All @@ -309,14 +316,12 @@ const Collapse = (($) => {
}

_addAriaAndCollapsedClass(element, triggerArray) {
if (element) {
const isOpen = $(element).hasClass(ClassName.SHOW)
const isOpen = $(element).hasClass(ClassName.SHOW)

if (triggerArray.length) {
$(triggerArray)
.toggleClass(ClassName.COLLAPSED, !isOpen)
.attr('aria-expanded', isOpen)
}
if (triggerArray.length) {
$(triggerArray)
.toggleClass(ClassName.COLLAPSED, !isOpen)
.attr('aria-expanded', isOpen)
}
}

Expand Down
34 changes: 34 additions & 0 deletions js/tests/unit/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,38 @@ $(function () {
assert.ok(false, 'collapse not created')
}
})

QUnit.test('should find collapse children if they have collapse class too not only data-parent', function (assert) {
assert.expect(2)
var done = assert.async()

var html =
'<div class="my-collapse">' +
' <div class="item">' +
' <a data-toggle="collapse" href="#">Toggle item 1</a>' +
' <div id="collapse1" class="collapse show">Lorem ipsum 1</div>' +
' </div>' +
' <div class="item">' +
' <a id="triggerCollapse2" data-toggle="collapse" href="#">Toggle item 2</a>' +
' <div id="collapse2" class="collapse">Lorem ipsum 2</div>' +
' </div>' +
'</div>'

$(html).appendTo('#qunit-fixture')

var $parent = $('.my-collapse')
var $collapse2 = $('#collapse2')
$parent.find('.collapse').bootstrapCollapse({
parent: $parent,
toggle: false
})

$collapse2.on('shown.bs.collapse', function () {
assert.ok($collapse2.hasClass('show'))
assert.ok(!$('#collapse1').hasClass('show'))
done()
})

$collapse2.bootstrapCollapse('toggle')
})
})

0 comments on commit 6e2a823

Please sign in to comment.