1- import { Component , OnInit } from '@angular/core' ;
1+ import { Component , OnInit , OnDestroy } from '@angular/core' ;
22import { ActivatedRoute , Router } from '@angular/router' ;
3+ import { Subject } from 'rxjs' ;
4+ import { debounceTime , distinctUntilChanged , takeUntil } from 'rxjs/operators' ;
35import { PageLayoutComponent } from '../../page-layout/page-layout.component' ;
46import { 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