From 6b90c17e62609fc920361435c71a273f611eb217 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Fri, 19 Mar 2021 14:23:47 +0000 Subject: [PATCH] [Library | Trunk]: Fix autocomplete input git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@60674 d315682c-612b-4755-9ff5-7f18f6832af3 --- sharedComponents/input/input.component.ts | 92 +++++++++++++---------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/sharedComponents/input/input.component.ts b/sharedComponents/input/input.component.ts index 74f989c9..291767d9 100644 --- a/sharedComponents/input/input.component.ts +++ b/sharedComponents/input/input.component.ts @@ -15,7 +15,8 @@ import {Observable, of, Subscription} from "rxjs"; import {MatSelect} from "@angular/material/select"; import {MatAutocompleteSelectedEvent} from "@angular/material/autocomplete"; import {map, startWith} from "rxjs/operators"; -import {MatChipInputEvent} from "@angular/material/chips"; +import {MatChipInputEvent, MatChipSelectionChange} from "@angular/material/chips"; +import {THIS_EXPR} from "@angular/compiler/src/output/output_ast"; export interface Option { @@ -52,21 +53,6 @@ export interface Option { - -
- - - - {{option}} - - -
-
+ +
+ + + + + {{getLabel(formControl.value)}} + + + +
+ +
{{placeholder}}
+
+
+
+ + + {{option.label}} + + +
+
+
; public searchControl: FormControl; private subscriptions: any[] = []; - /** Autocomplete */ - @Input() - public autocompleteList: string[] = []; - public autocompleteFilteredList: Observable; @ViewChild('select') select: MatSelect; @ViewChild('searchInput') searchInput: ElementRef; focused: boolean = false; @@ -231,17 +244,18 @@ export class InputComponent implements OnInit, OnDestroy, OnChanges { if(this.type === 'logoURL') { this.secure = (!this.initValue || this.initValue.includes('https://')); } - if (this.options && this.type === 'chips') { + if (this.options && (this.type === 'chips' || this.type === 'autocomplete')) { this.filteredOptions = of(this.options); this.searchControl = new FormControl(''); + this.subscriptions.push(this.searchControl.valueChanges.subscribe(value => { + setTimeout(() => { + this.searchInput.nativeElement.focus(); + this.searchInput.nativeElement.value = value; + },0); + })); this.filteredOptions = this.searchControl.valueChanges.pipe(startWith(''), map(option => this.filter(option))); } - if(this.autocompleteList && this.type === 'autocomplete') { - this.autocompleteFilteredList = of(this.autocompleteList); - this.autocompleteFilteredList = this.formControl.valueChanges.pipe(startWith(''), - map(value => this.simpleFilter(value))); - } if (this.formControl && this.formControl.validator) { let validator = this.formControl.validator({} as AbstractControl); this.required = (validator && validator.required); @@ -286,8 +300,6 @@ export class InputComponent implements OnInit, OnDestroy, OnChanges { this.formAsArray.removeAt(index); this.formAsArray.markAsDirty(); this.searchControl.setValue(''); - this.searchInput.nativeElement.focus(); - this.searchInput.nativeElement.value = ''; this.stopPropagation(); } @@ -295,25 +307,21 @@ export class InputComponent implements OnInit, OnDestroy, OnChanges { this.formAsArray.push(new FormControl(event.option.value)); this.formAsArray.markAsDirty(); this.searchControl.setValue(''); - this.searchInput.nativeElement.focus(); - this.searchInput.nativeElement.value = ''; this.stopPropagation(); } private filter(value: string): Option[] { - let options = this.options.filter(option => !this.formAsArray.value.find(value => option.value === value)); - if ((!value || value.length == 0) && !this.showOptionsOnEmpty) { - return []; + let options = this.options; + if(this.type === "chips") { + options = options.filter(option => !this.formAsArray.value.find(value => option.value === value)); + } + if ((!value || value.length == 0)) { + return (this.showOptionsOnEmpty)?options:[]; } const filterValue = value.toString().toLowerCase(); return options.filter(option => option.label.toLowerCase().indexOf(filterValue) != -1); } - private simpleFilter(value: string): string[] { - const filterValue = value.toLowerCase(); - return this.autocompleteList.filter(option => option.toLowerCase().includes(filterValue)); - } - add(event: MatChipInputEvent) { if (this.addExtraChips && event.value) { this.stopPropagation(); @@ -328,4 +336,10 @@ export class InputComponent implements OnInit, OnDestroy, OnChanges { let option = this.options.find(option => option.value === value); return (option) ? option.label : value; } + + resetSearch(event: any, value: string = null) { + event.stopPropagation(); + this.formControl.setValue(null); + this.searchControl.setValue(this.getLabel(value)); + } }