|
1 |
| -import { AfterContentInit, AfterViewInit, Component, ContentChildren, EventEmitter, |
| 1 | +import { AfterContentInit, AfterViewInit, ChangeDetectorRef, Component, ContentChildren, EventEmitter, |
2 | 2 | HostBinding, Input, OnDestroy, Output, QueryList } from '@angular/core';
|
3 | 3 | import { fromEvent, Subject } from 'rxjs';
|
4 | 4 | import { takeUntil } from 'rxjs/operators';
|
@@ -113,7 +113,16 @@ export class IgxAccordionComponent implements AfterContentInit, AfterViewInit, O
|
113 | 113 | * ```
|
114 | 114 | */
|
115 | 115 | @Input()
|
116 |
| - public singleBranchExpand = false; |
| 116 | + public get singleBranchExpand(): boolean { |
| 117 | + return this._singleBranchExpand; |
| 118 | + } |
| 119 | + |
| 120 | + public set singleBranchExpand(val: boolean) { |
| 121 | + this._singleBranchExpand = val; |
| 122 | + if (val) { |
| 123 | + this.collapseAllExceptLast(); |
| 124 | + } |
| 125 | + } |
117 | 126 |
|
118 | 127 | /**
|
119 | 128 | * Emitted before a panel is expanded.
|
@@ -200,12 +209,16 @@ export class IgxAccordionComponent implements AfterContentInit, AfterViewInit, O
|
200 | 209 | private _destroy$ = new Subject<void>();
|
201 | 210 | private _unsubChildren$ = new Subject<void>();
|
202 | 211 | private _enabledPanels!: IgxExpansionPanelComponent[];
|
| 212 | + private _singleBranchExpand = false; |
203 | 213 |
|
204 |
| - constructor() { } |
| 214 | + constructor(private cdr: ChangeDetectorRef) { } |
205 | 215 |
|
206 | 216 | /** @hidden @internal **/
|
207 | 217 | public ngAfterContentInit(): void {
|
208 | 218 | this.updatePanelsAnimation();
|
| 219 | + if (this.singleBranchExpand) { |
| 220 | + this.collapseAllExceptLast(); |
| 221 | + } |
209 | 222 | }
|
210 | 223 |
|
211 | 224 | /** @hidden @internal **/
|
@@ -256,6 +269,16 @@ export class IgxAccordionComponent implements AfterContentInit, AfterViewInit, O
|
256 | 269 | this.panels.forEach(panel => panel.collapse());
|
257 | 270 | }
|
258 | 271 |
|
| 272 | + private collapseAllExceptLast(): void { |
| 273 | + const lastExpanded = this.panels?.filter(p => !p.collapsed && !p.header.disabled).pop(); |
| 274 | + this.panels?.forEach((p: IgxExpansionPanelComponent) => { |
| 275 | + if (p !== lastExpanded && !p.header.disabled) { |
| 276 | + p.collapsed = true; |
| 277 | + } |
| 278 | + }); |
| 279 | + this.cdr.detectChanges(); |
| 280 | + } |
| 281 | + |
259 | 282 | private handleKeydown(event: KeyboardEvent, panel: IgxExpansionPanelComponent): void {
|
260 | 283 | const key = event.key.toLowerCase();
|
261 | 284 | if (!(ACCORDION_NAVIGATION_KEYS.has(key))) {
|
|
0 commit comments