Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collapse children with no data-parent #27202

Merged
merged 1 commit into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})
})