@@ -17,6 +17,7 @@ import {
17
17
ControlValueAccessor ,
18
18
FormControl ,
19
19
NG_VALUE_ACCESSOR ,
20
+ Validators ,
20
21
} from "@angular/forms" ;
21
22
import {
22
23
MatAutocomplete ,
@@ -27,6 +28,7 @@ import { BehaviorSubject, combineLatest, fromEvent, Subject } from "rxjs";
27
28
import { debounceTime , startWith , takeUntil } from "rxjs/operators" ;
28
29
import { MatSelectCountryLangToken } from "./tokens" ;
29
30
import { MatInput } from "@angular/material/input" ;
31
+ import { deprecate } from "util" ;
30
32
31
33
/**
32
34
* Country interface ISO 3166
@@ -69,9 +71,10 @@ export class MatSelectCountryComponent
69
71
@Input ( ) countries : Country [ ] = [ ] ;
70
72
@Input ( ) label : string ;
71
73
@Input ( ) placeHolder = "Select country" ;
72
- @Input ( ) required : boolean ;
74
+ @Input ( ) required : boolean = false ;
73
75
@Input ( ) disabled : boolean ;
74
- @Input ( ) nullable : boolean ;
76
+ /** @deprecated Use clearable to allow user unselect country.*/
77
+ @Input ( ) nullable : boolean = true ;
75
78
@Input ( ) readonly : boolean ;
76
79
@Input ( ) tabIndex : number | string ;
77
80
@Input ( ) class : string ;
@@ -83,14 +86,10 @@ export class MatSelectCountryComponent
83
86
@Input ( ) name : string = "country" ;
84
87
@Input ( ) error : string = "" ;
85
88
@Input ( ) cleareable : boolean = false ;
86
- @Input ( ) formControl ?: FormControl | undefined = undefined ;
87
- @Input ( ) panelWidth : string | number = "" ;
88
- @Input ( "value" ) _value ?: Country | undefined = undefined ;
89
-
90
- @ViewChild ( "countryAutocomplete" ) statesAutocompleteRef : MatAutocomplete ;
91
- @ViewChild ( MatAutocompleteTrigger )
92
- autocompleteTrigger : MatAutocompleteTrigger ;
93
- @ViewChild ( "inputElement" ) inputElement : MatInput ;
89
+ @Input ( ) formControl ?: FormControl | undefined ;
90
+ @Input ( ) panelWidth ?: string | undefined ;
91
+ @Input ( "value" ) _value ?: Country | undefined ;
92
+ @Input ( ) extendWidth = false ;
94
93
95
94
// tslint:disable-next-line: no-output-on-prefix
96
95
@Output ( ) onCountrySelected : EventEmitter < Country > =
@@ -250,27 +249,27 @@ export class MatSelectCountryComponent
250
249
// }
251
250
}
252
251
252
+ clear ( ) {
253
+ this . filterString = "" ;
254
+ this . _applyFilters ( "" ) ;
255
+ this . value = null ;
256
+ if ( ! this . formControl ) {
257
+ this . onCountrySelected . emit ( null ) ;
258
+ }
259
+ }
260
+
253
261
inputChanged ( value : string ) : void {
254
262
console . log ( "input change detected: " , value ) ;
263
+ if ( ! value ) {
264
+ this . clear ( ) ;
265
+ return ;
266
+ }
255
267
if ( this . debounceTimeout ) {
256
268
clearTimeout ( this . debounceTimeout ) ;
257
269
}
258
- this . debounceTimeout = setTimeout (
259
- ( ) => this . _applyFilters ( value ) ,
260
- this . debounceTime
261
- ) ;
262
- this . _applyFilters ( value ) ;
263
- }
264
-
265
- onBlur ( ) {
266
- if (
267
- this . nullable &&
268
- ! this . inputElement . value &&
269
- this . statesAutocompleteRef . panel
270
- ) {
271
- this . _setValue ( null ) ;
272
- this . onCountrySelected . emit ( null ) ;
273
- }
270
+ this . debounceTimeout = setTimeout ( ( ) => {
271
+ this . _applyFilters ( value ?? "" ) ;
272
+ } , this . debounceTime ) ;
274
273
}
275
274
276
275
onOptionsSelected ( $event : MatAutocompleteSelectedEvent ) {
@@ -286,9 +285,7 @@ export class MatSelectCountryComponent
286
285
}
287
286
288
287
writeValue ( value ) {
289
- if ( value ) {
290
- this . value = value ;
291
- }
288
+ this . value = value ;
292
289
}
293
290
294
291
registerOnChange ( fn ) {
@@ -299,48 +296,41 @@ export class MatSelectCountryComponent
299
296
this . onTouched = fn ;
300
297
}
301
298
302
- autocompleteScroll ( ) {
303
- if ( this . itemsLoadSize ) {
304
- setTimeout ( ( ) => {
305
- if (
306
- this . statesAutocompleteRef &&
307
- this . autocompleteTrigger &&
308
- this . statesAutocompleteRef . panel
309
- ) {
310
- fromEvent ( this . statesAutocompleteRef . panel . nativeElement , "scroll" )
311
- . pipe ( takeUntil ( this . autocompleteTrigger . panelClosingActions ) )
312
- . subscribe ( ( ) => {
313
- const scrollTop =
314
- this . statesAutocompleteRef . panel . nativeElement . scrollTop ;
315
- const scrollHeight =
316
- this . statesAutocompleteRef . panel . nativeElement . scrollHeight ;
317
- const elementHeight =
318
- this . statesAutocompleteRef . panel . nativeElement . clientHeight ;
319
- const atBottom = scrollHeight === scrollTop + elementHeight ;
320
- if ( atBottom ) {
321
- // fetch more data if not filtered
322
- if ( this . filterString === "" ) {
323
- const fromIndex = this . filteredOptions . length ;
324
- const toIndex : number =
325
- + this . filteredOptions . length + + this . itemsLoadSize ;
326
- this . filteredOptions = [
327
- ...this . filteredOptions ,
328
- ...this . countries . slice ( fromIndex , toIndex ) ,
329
- ] ;
330
- }
331
- }
332
- } ) ;
333
- }
334
- } ) ;
335
- }
336
- }
337
-
338
- clear ( ) {
339
- this . filterString = "" ;
340
- this . inputChanged ( "" ) ;
341
- this . _setValue ( null ) ;
342
- this . onCountrySelected . emit ( null ) ;
343
- }
299
+ // autocompleteScroll() {
300
+ // if (this.itemsLoadSize) {
301
+ // setTimeout(() => {
302
+ // if (
303
+ // this.statesAutocompleteRef &&
304
+ // this.autocompleteTrigger &&
305
+ // this.statesAutocompleteRef.panel
306
+ // ) {
307
+ // fromEvent(this.statesAutocompleteRef.panel.nativeElement, "scroll")
308
+ // .pipe(takeUntil(this.autocompleteTrigger.panelClosingActions))
309
+ // .subscribe(() => {
310
+ // const scrollTop =
311
+ // this.statesAutocompleteRef.panel.nativeElement.scrollTop;
312
+ // const scrollHeight =
313
+ // this.statesAutocompleteRef.panel.nativeElement.scrollHeight;
314
+ // const elementHeight =
315
+ // this.statesAutocompleteRef.panel.nativeElement.clientHeight;
316
+ // const atBottom = scrollHeight === scrollTop + elementHeight;
317
+ // if (atBottom) {
318
+ // // fetch more data if not filtered
319
+ // if (this.filterString === "") {
320
+ // const fromIndex = this.filteredOptions.length;
321
+ // const toIndex: number =
322
+ // +this.filteredOptions.length + +this.itemsLoadSize;
323
+ // this.filteredOptions = [
324
+ // ...this.filteredOptions,
325
+ // ...this.countries.slice(fromIndex, toIndex),
326
+ // ];
327
+ // }
328
+ // }
329
+ // });
330
+ // }
331
+ // });
332
+ // }
333
+ // }
344
334
345
335
async _loadCountriesFromDb ( alpha2Code ?: string ) : Promise < void > {
346
336
this . loadingDB = true ;
0 commit comments