Skip to content

Commit

Permalink
Merge pull request #93 from squidit/Feature/SQ-66660-front-colapsar-s…
Browse files Browse the repository at this point in the history
…q-collapse-que-nao-sao-filhos-direto-de-um-sq-accordion

Feature/SQ-66660 - Permitir colapsar sq-collapse que não são filhos direto de um sq-accordion
  • Loading branch information
danielpcs authored Oct 25, 2024
2 parents dc33abe + cc33613 commit 86f7f46
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
46 changes: 43 additions & 3 deletions src/components/sq-accordion/sq-accordion.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
ContentChildren,
QueryList,
AfterContentInit,
OnDestroy
OnDestroy,
ElementRef
} from '@angular/core'
import { SqCollapseComponent } from './sq-collapse/sq-collapse.component'
import { sleep } from '../../helpers/sleep.helper'
import { Subscription } from 'rxjs'

/**
* Represents the SqAccordionComponent, an accordion component that manages a collection of SqCollapseComponents.
Expand Down Expand Up @@ -51,13 +53,30 @@ export class SqAccordionComponent implements AfterContentInit, OnDestroy {
/**
* A QueryList containing the SqCollapseComponent instances within the accordion.
*/
@ContentChildren(SqCollapseComponent)
collapses: QueryList<SqCollapseComponent> = [] as unknown as QueryList<SqCollapseComponent>
@ContentChildren(SqCollapseComponent, { descendants: true }) collapses: QueryList<SqCollapseComponent> = [] as unknown as QueryList<SqCollapseComponent>

/**
* A subscription to the changes of the collapses QueryList.
* This subscription is used to update the collapses QueryList when the content changes.
*/
collapsesSubscription: Subscription = new Subscription()

/**
* Initializes a new instance of the SqAccordionComponent class.
* @param elementRef - The ElementRef instance.
*/
constructor(private elementRef: ElementRef) {}

/**
* Performs actions after the content has been initialized.
*/
async ngAfterContentInit() {
this.filterChildren()

this.collapsesSubscription = this.collapses.changes.subscribe(() => {
this.filterChildren()
})

if (this.openFirst) {
const collapses = this.collapses.toArray()
if (collapses?.length) {
Expand Down Expand Up @@ -88,6 +107,8 @@ export class SqAccordionComponent implements AfterContentInit, OnDestroy {
collapse.openedEmitter.unsubscribe()
})
}

this.collapsesSubscription?.unsubscribe()
}

/**
Expand All @@ -104,4 +125,23 @@ export class SqAccordionComponent implements AfterContentInit, OnDestroy {
}
collapse.toggleCollapse()
}

/**
* Filters the children of the accordion.
* This method is used to filter the children of the accordion and update the collapses QueryList.
*/
filterChildren() {
const hostElement = this.elementRef.nativeElement
const filteredChildren = this.collapses.filter(collapse => {
let parent = collapse['elementRef'].nativeElement.parentElement
for (let i = 0; i < 4; i++) {
if (parent === hostElement) {
return true
}
parent = parent.parentElement
}
return false
})
this.collapses.reset(filteredChildren)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ export class SqCollapseComponent {
/**
* Component Constructor
* @param colorsHelper - The ColorsHelper instance
* @param elementRef - The ElementRef instance
*/
constructor(public colorsHelper: ColorsHelper) { }
constructor(public colorsHelper: ColorsHelper, public elementRef: ElementRef) { }

/**
* Toggles the state of the collapse component.
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@squidit/ngx-css",
"version": "1.3.60",
"version": "1.3.61",
"peerDependencies": {
"@angular/common": ">=15.0.0",
"@angular/cdk": ">=15.0.0",
Expand Down

0 comments on commit 86f7f46

Please sign in to comment.