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

fix(components): Unexpected behavior of multilevel accordions #3427

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/big-frogs-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-components': patch
---

Fixed a bug in nested accordions where closing a child item unintentionally closed all parent accordion elements.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class PostAccordionItem {
// capture to make sure the "collapsed" property is updated before the event is consumed
@Listen('postToggle', { capture: true })
onCollapseToggle(event: CustomEvent<boolean>): void {
if ((event.target as HTMLElement).localName === 'post-accordion-item') {
if (event.target === this.host) {
this.collapsed = !event.detail;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,24 @@ export class PostAccordion {

@Listen('postToggle')
collapseToggleHandler(event: CustomEvent<boolean>) {
if ((event.target as HTMLElement).localName === 'post-accordion-item') {
event.stopPropagation();
const toggledItem = event.target as HTMLElement;
schaertim marked this conversation as resolved.
Show resolved Hide resolved
const closestParentAccordion = toggledItem.closest('post-accordion');

const toggledItem = event.target as HTMLPostAccordionItemElement;
const isClosing = this.expandedItems.has(toggledItem);
if (closestParentAccordion === this.host) {
const toggledAccordionItem = event.target as HTMLPostAccordionItemElement;
const isClosing = this.expandedItems.has(toggledAccordionItem);

if (isClosing) {
this.expandedItems.delete(toggledItem);
this.expandedItems.delete(toggledAccordionItem);
} else {
this.expandedItems.add(toggledItem);
this.expandedItems.add(toggledAccordionItem);
}

if (this.multiple || isClosing) return;

// close other open accordion items to have only one opened at a time
Array.from(this.expandedItems.values())
.filter(item => item !== toggledItem)
.filter(item => item !== toggledAccordionItem)
.forEach(item => {
item.toggle(false);
});
Expand Down
Loading