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

Detach accordion from card #22138

Merged
merged 3 commits into from
Mar 20, 2017
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
14 changes: 11 additions & 3 deletions js/src/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}


Expand All @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions js/tests/unit/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
})