keep parent only as element

This commit is contained in:
GeoSot 2021-06-10 02:51:51 +03:00 committed by XhmikosR
parent 4c1f7bb051
commit 6f17e634ce
2 changed files with 15 additions and 19 deletions

View File

@ -32,12 +32,12 @@ const DATA_API_KEY = '.data-api'
const Default = { const Default = {
toggle: true, toggle: true,
parent: '' parent: null
} }
const DefaultType = { const DefaultType = {
toggle: 'boolean', toggle: 'boolean',
parent: '(string|element)' parent: '(null|element)'
} }
const EVENT_SHOW = `show${EVENT_KEY}` const EVENT_SHOW = `show${EVENT_KEY}`
@ -86,7 +86,7 @@ class Collapse extends BaseComponent {
} }
} }
this._parent = this._config.parent ? this._getParent() : null this._initializeChildren()
if (!this._config.parent) { if (!this._config.parent) {
this._addAriaAndCollapsedClass(this._triggerArray, this._isShown()) this._addAriaAndCollapsedClass(this._triggerArray, this._isShown())
@ -125,9 +125,9 @@ class Collapse extends BaseComponent {
let actives = [] let actives = []
let activesData let activesData
if (this._parent) { if (this._config.parent) {
const children = SelectorEngine.find(`.${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`, this._parent) const children = SelectorEngine.find(`.${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`, this._config.parent)
actives = SelectorEngine.find(SELECTOR_ACTIVES, this._parent).filter(elem => !children.includes(elem)) // remove children if greater depth actives = SelectorEngine.find(SELECTOR_ACTIVES, this._config.parent).filter(elem => !children.includes(elem)) // remove children if greater depth
} }
const container = SelectorEngine.findOne(this._selector) const container = SelectorEngine.findOne(this._selector)
@ -239,6 +239,7 @@ class Collapse extends BaseComponent {
...config ...config
} }
config.toggle = Boolean(config.toggle) // Coerce string values config.toggle = Boolean(config.toggle) // Coerce string values
config.parent = getElement(config.parent)
typeCheckConfig(NAME, config, DefaultType) typeCheckConfig(NAME, config, DefaultType)
return config return config
} }
@ -247,14 +248,13 @@ class Collapse extends BaseComponent {
return this._element.classList.contains(CLASS_NAME_HORIZONTAL) ? WIDTH : HEIGHT return this._element.classList.contains(CLASS_NAME_HORIZONTAL) ? WIDTH : HEIGHT
} }
_getParent() { _initializeChildren() {
let { parent } = this._config if (!this._config.parent) {
return
}
parent = getElement(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))
const selector = `${SELECTOR_DATA_TOGGLE}[data-bs-parent="${parent}"]`
SelectorEngine.find(selector, parent)
.forEach(element => { .forEach(element => {
const selected = getElementFromSelector(element) const selected = getElementFromSelector(element)
@ -262,8 +262,6 @@ class Collapse extends BaseComponent {
this._addAriaAndCollapsedClass([element], this._isShown(selected)) this._addAriaAndCollapsedClass([element], this._isShown(selected))
} }
}) })
return parent
} }
_addAriaAndCollapsedClass(triggerArray, isOpen) { _addAriaAndCollapsedClass(triggerArray, isOpen) {

View File

@ -65,8 +65,7 @@ describe('Collapse', () => {
parent: fakejQueryObject parent: fakejQueryObject
}) })
expect(collapse._config.parent).toEqual(fakejQueryObject) expect(collapse._config.parent).toEqual(myCollapseEl)
expect(collapse._getParent()).toEqual(myCollapseEl)
}) })
it('should allow non jquery object in parent config', () => { it('should allow non jquery object in parent config', () => {
@ -104,8 +103,7 @@ describe('Collapse', () => {
parent: 'div.my-collapse' parent: 'div.my-collapse'
}) })
expect(collapse._config.parent).toEqual('div.my-collapse') expect(collapse._config.parent).toEqual(myCollapseEl)
expect(collapse._getParent()).toEqual(myCollapseEl)
}) })
}) })