Auto-expand first active filter panel or default to first

This commit is contained in:
aditya.chandel
2025-06-06 17:05:25 -06:00
committed by Aditya Chandel
parent 7053cfa963
commit 68dce9bd23
3 changed files with 27 additions and 14 deletions
@@ -635,6 +635,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
this.selectedFilter.next(parsedFilters); this.selectedFilter.next(parsedFilters);
this.bookFilterComponent.setFilters?.(parsedFilters); this.bookFilterComponent.setFilters?.(parsedFilters);
this.bookFilterComponent.onFiltersChanged?.();
const firstFilter = filterParams.split(',')[0]; const firstFilter = filterParams.split(',')[0];
const [key, ...values] = firstFilter.split(':'); const [key, ...values] = firstFilter.split(':');
@@ -11,7 +11,7 @@
allowEmpty="false" allowEmpty="false"
styleClass="h-8"></p-selectButton> styleClass="h-8"></p-selectButton>
</div> </div>
<p-accordion [value]="0"> <p-accordion [value]="expandedPanels">
@for (filterType of filterTypes; track filterType; let i = $index) { @for (filterType of filterTypes; track filterType; let i = $index) {
<p-accordion-panel [value]="i"> <p-accordion-panel [value]="i">
<p-accordion-header> <p-accordion-header>
@@ -135,7 +135,7 @@ export class BookFilterComponent implements OnInit {
{label: 'OR', value: 'or'} {label: 'OR', value: 'or'}
]; ];
private _selectedFilterMode: 'and' | 'or' = 'and'; private _selectedFilterMode: 'and' | 'or' = 'and';
expandedPanels: number = 0;
readonly filterLabels: Record<string, string> = { readonly filterLabels: Record<string, string> = {
author: 'Author', author: 'Author',
category: 'Category', category: 'Category',
@@ -175,6 +175,8 @@ export class BookFilterComponent implements OnInit {
if (this.resetFilter$) { if (this.resetFilter$) {
this.resetFilter$.subscribe(() => this.clearActiveFilter()); this.resetFilter$.subscribe(() => this.clearActiveFilter());
} }
this.setExpandedPanels();
} }
private getFilterStream<T>( private getFilterStream<T>(
@@ -255,7 +257,6 @@ export class BookFilterComponent implements OnInit {
this.activeFilters[key] = [value]; this.activeFilters[key] = [value];
} }
} }
this.filterSelected.emit({...this.activeFilters}); this.filterSelected.emit({...this.activeFilters});
} }
@@ -263,4 +264,15 @@ export class BookFilterComponent implements OnInit {
this.activeFilters = {}; this.activeFilters = {};
this.filterSelected.emit(null); this.filterSelected.emit(null);
} }
setExpandedPanels(): void {
const firstActiveIndex = this.filterTypes.findIndex(
type => this.activeFilters[type]?.length
);
this.expandedPanels = firstActiveIndex !== -1 ? firstActiveIndex : 0;
}
onFiltersChanged(): void {
this.setExpandedPanels();
}
} }