1- import {
2- Component ,
3- OnInit ,
4- ViewChild ,
5- ElementRef ,
6- AfterViewInit ,
7- Input ,
8- ChangeDetectorRef ,
9- TemplateRef ,
10- } from '@angular/core' ;
1+ import { Component , ViewChild , ElementRef , AfterViewInit , Input , ChangeDetectorRef , TemplateRef } from '@angular/core' ;
112import { from , fromEvent , Observable , of } from 'rxjs' ;
12- import { map , startWith , distinctUntilChanged , tap , switchMap , shareReplay } from 'rxjs/operators' ;
3+ import { map , startWith , distinctUntilChanged , switchMap , shareReplay } from 'rxjs/operators' ;
134import { ModalController } from '@ionic/angular' ;
14- import { isEqual , includes } from 'lodash' ;
5+ import { isEqual } from 'lodash' ;
156import { RecentLocalStorageItemsService } from 'src/app/core/services/recent-local-storage-items.service' ;
167import { UtilityService } from 'src/app/core/services/utility.service' ;
178import { ExtendedOption , ModalOption , Option } from './fy-select-modal.interface' ;
@@ -21,7 +12,7 @@ import { ExtendedOption, ModalOption, Option } from './fy-select-modal.interface
2112 templateUrl : './fy-select-modal.component.html' ,
2213 styleUrls : [ './fy-select-modal.component.scss' ] ,
2314} )
24- export class FySelectModalComponent implements OnInit , AfterViewInit {
15+ export class FySelectModalComponent implements AfterViewInit {
2516 @ViewChild ( 'searchBar' ) searchBarRef : ElementRef ;
2617
2718 @Input ( ) options : Option [ ] = [ ] ;
@@ -52,6 +43,8 @@ export class FySelectModalComponent implements OnInit, AfterViewInit {
5243
5344 @Input ( ) label : string ;
5445
46+ @Input ( ) isCustomSelect = false ;
47+
5548 value : string | ModalOption = '' ;
5649
5750 recentrecentlyUsedItems$ : Observable < ExtendedOption [ ] > ;
@@ -63,16 +56,14 @@ export class FySelectModalComponent implements OnInit, AfterViewInit {
6356 private utilityService : UtilityService
6457 ) { }
6558
66- ngOnInit ( ) { }
67-
68- clearValue ( ) {
59+ clearValue ( ) : void {
6960 this . value = '' ;
7061 const searchInput = this . searchBarRef . nativeElement as HTMLInputElement ;
7162 searchInput . value = '' ;
7263 searchInput . dispatchEvent ( new Event ( 'keyup' ) ) ;
7364 }
7465
75- getRecentlyUsedItems ( ) {
66+ getRecentlyUsedItems ( ) : Observable < Option [ ] > {
7667 // Check if recently items exists from api and set, else, set the recent items from the localStorage
7768 if ( this . recentlyUsed ) {
7869 return of ( this . recentlyUsed ) ;
@@ -92,14 +83,14 @@ export class FySelectModalComponent implements OnInit, AfterViewInit {
9283 }
9384 }
9485
95- ngAfterViewInit ( ) {
86+ ngAfterViewInit ( ) : void {
9687 if ( this . searchBarRef && this . searchBarRef . nativeElement ) {
97- this . filteredOptions$ = fromEvent ( this . searchBarRef . nativeElement , 'keyup' ) . pipe (
98- map ( ( event : any ) => event . srcElement . value ) ,
88+ this . filteredOptions$ = fromEvent ( this . searchBarRef . nativeElement as HTMLElement , 'keyup' ) . pipe (
89+ map ( ( event : Event ) => ( event . target as HTMLInputElement ) . value ) ,
9990 startWith ( '' ) ,
10091 distinctUntilChanged ( ) ,
10192 map ( ( searchText ) => {
102- const initial = [ ] ;
93+ const initial : Option [ ] = [ ] ;
10394
10495 if ( this . nullOption && this . currentSelection ) {
10596 initial . push ( { label : 'None' , value : null } ) ;
@@ -113,6 +104,7 @@ export class FySelectModalComponent implements OnInit, AfterViewInit {
113104 const selectedOption = this . options . find ( ( option ) => isEqual ( option . value , this . currentSelection ) ) ;
114105 if ( ! selectedOption ) {
115106 extraOption = extraOption . concat ( {
107+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
116108 label : this . currentSelection [ this . defaultLabelProp ] ,
117109 value : this . currentSelection ,
118110 selected : false ,
@@ -129,12 +121,14 @@ export class FySelectModalComponent implements OnInit, AfterViewInit {
129121 option . selected = isEqual ( option . value , this . currentSelection ) ;
130122 return option ;
131123 } )
124+ . sort ( ( a , b ) => ( this . isCustomSelect ? ( a . selected === b . selected ? 0 : a . selected ? - 1 : 1 ) : 0 ) )
125+ . slice ( 0 , this . isCustomSelect ? 200 : this . options . length )
132126 ) ;
133127 } ) ,
134128 shareReplay ( 1 )
135129 ) ;
136- this . recentrecentlyUsedItems$ = fromEvent ( this . searchBarRef . nativeElement , 'keyup' ) . pipe (
137- map ( ( event : any ) => event . srcElement . value ) ,
130+ this . recentrecentlyUsedItems$ = fromEvent ( this . searchBarRef . nativeElement as HTMLElement , 'keyup' ) . pipe (
131+ map ( ( event : Event ) => ( event . target as HTMLInputElement ) . value ) ,
138132 startWith ( '' ) ,
139133 distinctUntilChanged ( ) ,
140134 switchMap ( ( searchText ) =>
@@ -146,41 +140,44 @@ export class FySelectModalComponent implements OnInit, AfterViewInit {
146140 shareReplay ( 1 )
147141 ) ;
148142 } else {
149- const initial = [ ] ;
143+ const initial : Option [ ] = [ ] ;
150144
151145 if ( this . nullOption && this . currentSelection ) {
152146 initial . push ( { label : 'None' , value : null } ) ;
153147 }
154148
155149 this . filteredOptions$ = of (
156150 initial . concat (
157- this . options . map ( ( option ) => {
158- option . selected = isEqual ( option . value , this . currentSelection ) ;
159- return option ;
160- } )
151+ this . options
152+ . map ( ( option ) => {
153+ option . selected = isEqual ( option . value , this . currentSelection ) ;
154+ return option ;
155+ } )
156+ . sort ( ( a , b ) => ( this . isCustomSelect ? ( a . selected === b . selected ? 0 : a . selected ? - 1 : 1 ) : 0 ) )
157+ . slice ( 0 , this . isCustomSelect ? 200 : this . options . length )
161158 )
162159 ) ;
163160 }
164161
165162 this . cdr . detectChanges ( ) ;
166163 }
167164
168- onDoneClick ( ) {
165+ onDoneClick ( ) : void {
169166 this . modalController . dismiss ( ) ;
170167 }
171168
172- onElementSelect ( option : ExtendedOption ) {
169+ onElementSelect ( option : ExtendedOption ) : void {
173170 if ( this . cacheName && option . value ) {
174171 option . custom = ! this . options . some ( ( internalOption ) => internalOption . value !== option . value ) ;
175172 this . recentLocalStorageItemsService . post ( this . cacheName , option , 'label' ) ;
176173 }
177174 this . modalController . dismiss ( option ) ;
178175 }
179176
180- saveToCacheAndUse ( ) {
177+ saveToCacheAndUse ( ) : void {
181178 const option : ExtendedOption = {
182- label : this . searchBarRef . nativeElement . value ,
183- value : this . searchBarRef . nativeElement . value ,
179+ label : ( this . searchBarRef . nativeElement as HTMLInputElement ) . value ,
180+ value : ( this . searchBarRef . nativeElement as HTMLInputElement ) . value ,
184181 selected : false ,
185182 } ;
186183 this . onElementSelect ( option ) ;
0 commit comments