Skip to content

Commit 971df9b

Browse files
committed
refactor(material-icons-extened): use async pipes
1 parent 2db8c8a commit 971df9b

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

src/app/data-display/icon/material-icons-extended/material-icons-extended.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</igx-input-group>
3232
</div>
3333
<div class="sample__body">
34-
@if ((allIcons | filterByName: searchTerm() | categoriesFilter: selectedCategory); as fResults) {
34+
@if (filteredResults$ | async; as fResults) {
3535
@for (group of fResults; track trackByCategory($index, group)) {
3636
<article class="sample__body-inner">
3737
<header class="sample__body-title">

src/app/data-display/icon/material-icons-extended/material-icons-extended.component.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
forwardRef,
99
inject
1010
} from '@angular/core';
11+
import { AsyncPipe } from '@angular/common';
1112
import fileSaver from 'file-saver';
1213
import Fuse from 'fuse.js';
13-
import { Subject } from 'rxjs';
14-
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
15-
import { toSignal } from '@angular/core/rxjs-interop';
14+
import { Subject, combineLatest, BehaviorSubject, Observable } from 'rxjs';
15+
import { debounceTime, distinctUntilChanged, map, startWith } from 'rxjs/operators';
1616

1717
import { IgxIconComponent, IgxIconService } from 'igniteui-angular/icon';
1818
import { ISelectionEventArgs } from 'igniteui-angular/drop-down';
@@ -42,6 +42,7 @@ interface ICategoryOption {
4242
templateUrl: './material-icons-extended.component.html',
4343
styleUrls: ['./material-icons-extended.component.scss'],
4444
imports: [
45+
AsyncPipe,
4546
IgxSelectComponent,
4647
IgxLabelDirective,
4748
IgxSelectItemComponent,
@@ -58,14 +59,28 @@ export class MaterialIconsExtendedComponent implements OnInit {
5859
private iconService = inject(IgxIconService);
5960
private renderer = inject(Renderer2);
6061

61-
// Search with debounce using signals
62+
// Search with debounce
6263
private searchInput$ = new Subject<string>();
63-
public searchTerm = toSignal(
64-
this.searchInput$.pipe(
65-
debounceTime(300),
66-
distinctUntilChanged()
67-
),
68-
{ initialValue: '' }
64+
private categorySubject$ = new BehaviorSubject<IconCategory | 'all'>('all');
65+
66+
private searchTerm$ = this.searchInput$.pipe(
67+
debounceTime(300),
68+
distinctUntilChanged(),
69+
startWith('')
70+
);
71+
72+
// Combine search and category filters
73+
public filteredResults$: Observable<IIconsGroup[]> = combineLatest([
74+
this.searchTerm$,
75+
this.categorySubject$
76+
]).pipe(
77+
map(([searchTerm, category]) => {
78+
const filterByNamePipe = new FilterByName();
79+
const categoriesPipe = new CategoriesFilterPipe();
80+
81+
const filtered = filterByNamePipe.transform(this.allIcons, searchTerm);
82+
return categoriesPipe.transform(filtered, category);
83+
})
6984
);
7085

7186
public categories: ICategoryOption[] = [
@@ -99,15 +114,16 @@ export class MaterialIconsExtendedComponent implements OnInit {
99114
}
100115

101116
public allIcons = imxIcons;
102-
103117
public selectedCategory: IconCategory | 'all' = 'all';
104118

105119
handleSelection(event: ISelectionEventArgs) {
106120
this.selectedCategory = event.newSelection.value;
121+
this.categorySubject$.next(event.newSelection.value);
107122
}
108123

109124
resetFilter() {
110125
this.selectedCategory = 'all';
126+
this.categorySubject$.next('all');
111127
}
112128

113129
trackByIcon(_index: number, icon: IMXIcon): string {
@@ -172,7 +188,8 @@ interface IIconsGroup {
172188
}
173189

174190
@Pipe({
175-
name: 'categoriesFilter'
191+
name: 'categoriesFilter',
192+
pure: true
176193
})
177194
export class CategoriesFilterPipe implements PipeTransform {
178195
sortIcons(acc: IIconsGroup[], icon: IMXIcon): IIconsGroup[] {
@@ -215,7 +232,8 @@ export class CategoriesFilterPipe implements PipeTransform {
215232
}
216233

217234
@Pipe({
218-
name: 'filterByName'
235+
name: 'filterByName',
236+
pure: true
219237
})
220238
export class FilterByName implements PipeTransform {
221239
private fuse: Fuse<IMXIcon> | null = null;

0 commit comments

Comments
 (0)