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 - Allow to pass jQuery object or DOM element to the parent option #24083

Merged
merged 1 commit into from
Sep 25, 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
4 changes: 2 additions & 2 deletions docs/4.0/components/collapse.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
<tbody>
<tr>
<td>parent</td>
<td>selector</td>
<td>selector | jQuery object | DOM element </td>
<td>false</td>
<td>If a selector is provided, then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior - this is dependent on the <code>card</code> class). The attribute has to be set on the target collapsible area.</td>
<td>If parent is provided, then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior - this is dependent on the <code>card</code> class). The attribute has to be set on the target collapsible area.</td>
</tr>
<tr>
<td>toggle</td>
Expand Down
15 changes: 13 additions & 2 deletions js/src/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const Collapse = (() => {

const DefaultType = {
toggle : 'boolean',
parent : 'string'
parent : '(string|element)'
}

const Event = {
Expand Down Expand Up @@ -289,7 +289,18 @@ const Collapse = (() => {
}

_getParent() {
const parent = $(this._config.parent)[0]
let parent = null
if (Util.isElement(this._config.parent)) {
parent = this._config.parent

// it's a jQuery object
if (typeof this._config.parent.jquery !== 'undefined') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

typeof this._config.parent.jquery !== 'undefined'

->

this._config.parent instanceof jQuery

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep works too 👍

parent = this._config.parent[0]
}
} else {
parent = $(this._config.parent)[0]
}

const selector =
`[data-toggle="collapse"][data-parent="${this._config.parent}"]`

Expand Down
10 changes: 5 additions & 5 deletions js/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ const Util = (() => {
return {}.toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}

function isElement(obj) {
return (obj[0] || obj).nodeType
}

function getSpecialTransitionEndEvent() {
return {
bindType: transition.end,
Expand Down Expand Up @@ -138,12 +134,16 @@ const Util = (() => {
return Boolean(transition)
},

isElement(obj) {
return (obj[0] || obj).nodeType
},

typeCheckConfig(componentName, config, configTypes) {
for (const property in configTypes) {
if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
const expectedTypes = configTypes[property]
const value = config[property]
const valueType = value && isElement(value) ?
const valueType = value && Util.isElement(value) ?
'element' : toType(value)

if (!new RegExp(expectedTypes).test(valueType)) {
Expand Down
44 changes: 44 additions & 0 deletions js/tests/unit/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,4 +698,48 @@ $(function () {

$target.trigger($.Event('click'))
})

QUnit.test('should allow jquery object in parent config', function (assert) {
assert.expect(1)
var html =
'<div class="my-collapse">' +
' <div class="item">' +
' <a data-toggle="collapse" href="#">Toggle item</a>' +
' <div class="collapse">Lorem ipsum</div>' +
' </div>' +
'</div>'

$(html).appendTo('#qunit-fixture')
try {
$('[data-toggle="collapse"]').bootstrapCollapse({
parent: $('.my-collapse')
})
assert.ok(true, 'collapse correctly created')
}
catch (e) {
assert.ok(false, 'collapse not created')
}
})

QUnit.test('should allow DOM object in parent config', function (assert) {
assert.expect(1)
var html =
'<div class="my-collapse">' +
' <div class="item">' +
' <a data-toggle="collapse" href="#">Toggle item</a>' +
' <div class="collapse">Lorem ipsum</div>' +
' </div>' +
'</div>'

$(html).appendTo('#qunit-fixture')
try {
$('[data-toggle="collapse"]').bootstrapCollapse({
parent: $('.my-collapse')[0]
})
assert.ok(true, 'collapse correctly created')
}
catch (e) {
assert.ok(false, 'collapse not created')
}
})
})