From 04137a1cc7952efe693cec4dbe48c43d7904b646 Mon Sep 17 00:00:00 2001 From: GeoSot Date: Sat, 11 Sep 2021 02:07:21 +0300 Subject: [PATCH] Regression on Collapse, to proper handle toggling between sibling children --- js/src/collapse.js | 5 +-- js/tests/unit/collapse.spec.js | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/js/src/collapse.js b/js/src/collapse.js index f38878f9b3e0..edfc7ea8517e 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -50,6 +50,7 @@ const CLASS_NAME_SHOW = 'show' const CLASS_NAME_COLLAPSE = 'collapse' const CLASS_NAME_COLLAPSING = 'collapsing' const CLASS_NAME_COLLAPSED = 'collapsed' +const CLASS_NAME_DEEPER_CHILDREN = `:scope .${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}` const CLASS_NAME_HORIZONTAL = 'collapse-horizontal' const WIDTH = 'width' @@ -126,7 +127,7 @@ class Collapse extends BaseComponent { let activesData if (this._config.parent) { - const children = SelectorEngine.find(`.${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`, this._config.parent) + const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent) actives = SelectorEngine.find(SELECTOR_ACTIVES, this._config.parent).filter(elem => !children.includes(elem)) // remove children if greater depth } @@ -253,7 +254,7 @@ class Collapse extends BaseComponent { return } - const children = SelectorEngine.find(`.${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`, this._config.parent) + const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent) SelectorEngine.find(SELECTOR_DATA_TOGGLE, this._config.parent).filter(elem => !children.includes(elem)) .forEach(element => { const selected = getElementFromSelector(element) diff --git a/js/tests/unit/collapse.spec.js b/js/tests/unit/collapse.spec.js index ece88eff581f..ea0b53c105ac 100644 --- a/js/tests/unit/collapse.spec.js +++ b/js/tests/unit/collapse.spec.js @@ -267,6 +267,63 @@ describe('Collapse', () => { collapse.show() }) + it('should be able to handle toggling of other children siblings', done => { + fixtureEl.innerHTML = [ + '
', + '
', + ' ', + '
', + '
', + '
', + '
', + '
', + '
', + ' ', + '
', + '
', + '
content
', + '
', + '
', + '
', + '
', + ' ', + '
', + '
', + '
content
', + '
', + '
', + '
', + '
', + '
', + '
' + ].join('') + + const el = selector => fixtureEl.querySelector(selector) + + const parentBtn = el('[data-bs-target="#parentContent"]') + const childBtn1 = el('[data-bs-target="#childContent1"]') + const childBtn2 = el('[data-bs-target="#childContent2"]') + + const parentCollapseEl = el('#parentContent') + const childCollapseEl1 = el('#childContent1') + const childCollapseEl2 = el('#childContent2') + + parentCollapseEl.addEventListener('shown.bs.collapse', () => { + expect(parentCollapseEl.classList.contains('show')).toEqual(true) + childBtn1.click() + }) + childCollapseEl1.addEventListener('shown.bs.collapse', () => { + expect(childCollapseEl1.classList.contains('show')).toEqual(true) + childBtn2.click() + }) + childCollapseEl2.addEventListener('shown.bs.collapse', () => { + expect(childCollapseEl2.classList.contains('show')).toEqual(true) + expect(childCollapseEl1.classList.contains('show')).toEqual(false) + done() + }) + + parentBtn.click() + }) it('should not change tab tabpanels descendants on accordion', done => { fixtureEl.innerHTML = [ '
',