Skip to content

Commit 13bf086

Browse files
making icon search search-as-you-type
1 parent fa4c32c commit 13bf086

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

projects/website-angular/src/app/community/icon-lib/icon-lib.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h1>Icon Library</h1>
1717
type="text"
1818
placeholder="Search icons..."
1919
[value]="searchQuery"
20-
(input)="searchQuery = $any($event.target).value"
20+
(input)="onSearchInput($event)"
2121
(keydown)="onSearchKeydown($event)"
2222
/>
2323
@if (searchQuery) {

projects/website-angular/src/app/community/icon-lib/icon-lib.component.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, OnDestroy } from '@angular/core';
22
import { ActivatedRoute, Router } from '@angular/router';
3+
import { Subject } from 'rxjs';
4+
import { debounceTime, distinctUntilChanged, takeUntil } from 'rxjs/operators';
35
import { PageLayoutComponent } from '../../page-layout/page-layout.component';
46
import { IconService, IconCategory, IconEntry } from '../../../services/icon.service';
57

@@ -51,7 +53,7 @@ interface ParsedReference {
5153
templateUrl: './icon-lib.component.html',
5254
styleUrl: './icon-lib.component.scss'
5355
})
54-
export class IconLibComponent implements OnInit {
56+
export class IconLibComponent implements OnInit, OnDestroy {
5557
view: View = 'grid';
5658
loading = true;
5759
error = false;
@@ -72,13 +74,25 @@ export class IconLibComponent implements OnInit {
7274
detailLoading = false;
7375
parsedReferences: Map<string, ParsedReference[]> = new Map();
7476

77+
// Debounced search
78+
private searchSubject = new Subject<string>();
79+
private destroy$ = new Subject<void>();
80+
7581
constructor(
7682
private iconService: IconService,
7783
private route: ActivatedRoute,
7884
private router: Router
7985
) {}
8086

8187
ngOnInit() {
88+
this.searchSubject.pipe(
89+
debounceTime(300),
90+
distinctUntilChanged(),
91+
takeUntil(this.destroy$)
92+
).subscribe(() => {
93+
this.searchIcons();
94+
});
95+
8296
this.loadCategories();
8397
const iconId = this.route.snapshot.paramMap.get('id');
8498
if (iconId) {
@@ -88,6 +102,11 @@ export class IconLibComponent implements OnInit {
88102
}
89103
}
90104

105+
ngOnDestroy() {
106+
this.destroy$.next();
107+
this.destroy$.complete();
108+
}
109+
91110
get maxPage(): number {
92111
return Math.ceil(this.totalEntries / ICONS_PER_PAGE);
93112
}
@@ -252,6 +271,12 @@ export class IconLibComponent implements OnInit {
252271
img.parentElement?.appendChild(fallback);
253272
}
254273

274+
onSearchInput(event: Event) {
275+
const value = (event.target as HTMLInputElement).value;
276+
this.searchQuery = value;
277+
this.searchSubject.next(value);
278+
}
279+
255280
onSearchKeydown(event: KeyboardEvent) {
256281
if (event.key === 'Enter') {
257282
this.searchIcons();

0 commit comments

Comments
 (0)