const Default = {
toggle: true,
- parent: ''
+ parent: null
}
const DefaultType = {
toggle: 'boolean',
- parent: '(string|element)'
+ parent: '(null|element)'
}
const EVENT_SHOW = `show${EVENT_KEY}`
}
}
- this._parent = this._config.parent ? this._getParent() : null
+ this._initializeChildren()
if (!this._config.parent) {
this._addAriaAndCollapsedClass(this._triggerArray, this._isShown())
let actives = []
let activesData
- if (this._parent) {
- const children = SelectorEngine.find(`.${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`, this._parent)
- actives = SelectorEngine.find(SELECTOR_ACTIVES, this._parent).filter(elem => !children.includes(elem)) // remove children if greater depth
+ if (this._config.parent) {
+ const children = SelectorEngine.find(`.${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`, this._config.parent)
+ actives = SelectorEngine.find(SELECTOR_ACTIVES, this._config.parent).filter(elem => !children.includes(elem)) // remove children if greater depth
}
const container = SelectorEngine.findOne(this._selector)
...config
}
config.toggle = Boolean(config.toggle) // Coerce string values
+ config.parent = getElement(config.parent)
typeCheckConfig(NAME, config, DefaultType)
return config
}
return this._element.classList.contains(CLASS_NAME_HORIZONTAL) ? WIDTH : HEIGHT
}
- _getParent() {
- let { parent } = this._config
-
- parent = getElement(parent)
-
- const selector = `${SELECTOR_DATA_TOGGLE}[data-bs-parent="${parent}"]`
+ _initializeChildren() {
+ if (!this._config.parent) {
+ return
+ }
- SelectorEngine.find(selector, parent)
+ const children = SelectorEngine.find(`.${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`, this._config.parent)
+ SelectorEngine.find(SELECTOR_DATA_TOGGLE, this._config.parent).filter(elem => !children.includes(elem))
.forEach(element => {
const selected = getElementFromSelector(element)
this._addAriaAndCollapsedClass([element], this._isShown(selected))
}
})
-
- return parent
}
_addAriaAndCollapsedClass(triggerArray, isOpen) {
parent: fakejQueryObject
})
- expect(collapse._config.parent).toEqual(fakejQueryObject)
- expect(collapse._getParent()).toEqual(myCollapseEl)
+ expect(collapse._config.parent).toEqual(myCollapseEl)
})
it('should allow non jquery object in parent config', () => {
parent: 'div.my-collapse'
})
- expect(collapse._config.parent).toEqual('div.my-collapse')
- expect(collapse._getParent()).toEqual(myCollapseEl)
+ expect(collapse._config.parent).toEqual(myCollapseEl)
})
})