From ec535b831b83f2dd0f22f736f85c1034c09307f7 Mon Sep 17 00:00:00 2001 From: Florian Necas Date: Tue, 25 Jul 2023 16:55:27 +0200 Subject: [PATCH 1/2] fix: autocomplete behavior --- .../autocomplete/autocomplete.component.ts | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/libs/ui/inputs/src/lib/autocomplete/autocomplete.component.ts b/libs/ui/inputs/src/lib/autocomplete/autocomplete.component.ts index f29b995f43..d8c9c25441 100644 --- a/libs/ui/inputs/src/lib/autocomplete/autocomplete.component.ts +++ b/libs/ui/inputs/src/lib/autocomplete/autocomplete.component.ts @@ -18,7 +18,7 @@ import { MatAutocompleteSelectedEvent, MatAutocompleteTrigger, } from '@angular/material/autocomplete' -import { Observable, of, ReplaySubject, Subscription } from 'rxjs' +import { merge, Observable, of, ReplaySubject, Subscription } from 'rxjs' import { catchError, debounceTime, @@ -26,6 +26,7 @@ import { filter, finalize, first, + map, switchMap, take, tap, @@ -72,27 +73,30 @@ export class AutocompleteComponent this.updateInputValue(value.currentValue) } } - if (this.inputRef) { - this.inputRef.nativeElement.value = value?.currentValue?.title - ? value.currentValue.title - : '' - } } ngOnInit(): void { - this.suggestions$ = this.control.valueChanges.pipe( - tap(() => (this.error = null)), - filter((value) => value.length > 2), - debounceTime(400), - distinctUntilChanged(), - tap(() => (this.searching = true)), - switchMap((value) => this.action(value)), + this.suggestions$ = merge( + this.control.valueChanges.pipe( + filter((value) => typeof value === 'string'), + filter((value: string) => value.length > 2), + debounceTime(400), + distinctUntilChanged(), + tap(() => (this.searching = true)) + ), + this.control.valueChanges.pipe( + filter((value) => typeof value === 'object' && value.title), + map((item) => item.title) + ) + ).pipe( + switchMap((value) => (value ? this.action(value) : of([]))), catchError((error: Error) => { this.error = error.message return of([]) }), finalize(() => (this.searching = false)) ) + this.subscription = this.control.valueChanges.subscribe((any) => { if (any !== '') { this.cancelEnter = false @@ -115,6 +119,9 @@ export class AutocompleteComponent if (value) { this.control.setValue(value) } + if (this.inputRef) { + this.inputRef.nativeElement.value = (value as any)?.title || '' + } } clear(): void { From cab7e52a868c5798a323e8c579ad4bfbc29bfd14 Mon Sep 17 00:00:00 2001 From: Florian Necas Date: Wed, 26 Jul 2023 16:09:00 +0200 Subject: [PATCH 2/2] fix: add test --- .../lib/autocomplete/autocomplete.component.spec.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libs/ui/inputs/src/lib/autocomplete/autocomplete.component.spec.ts b/libs/ui/inputs/src/lib/autocomplete/autocomplete.component.spec.ts index d8ad652ea1..be2eb5d4b1 100644 --- a/libs/ui/inputs/src/lib/autocomplete/autocomplete.component.spec.ts +++ b/libs/ui/inputs/src/lib/autocomplete/autocomplete.component.spec.ts @@ -5,7 +5,10 @@ import { MatAutocompleteModule } from '@angular/material/autocomplete' import { MatIconModule } from '@angular/material/icon' import { By } from '@angular/platform-browser' import { of, throwError } from 'rxjs' -import { AutocompleteComponent } from './autocomplete.component' +import { + AutocompleteComponent, + AutocompleteItem, +} from './autocomplete.component' import { UiWidgetsModule } from '@geonetwork-ui/ui/widgets' describe('AutocompleteComponent', () => { @@ -69,6 +72,14 @@ describe('AutocompleteComponent', () => { expect(popup).toBeFalsy() }) }) + describe('when clicking a predefined button', () => { + beforeEach(() => { + component.updateInputValue({ title: 'cc' } as AutocompleteItem) + }) + it('calls the action with object given as input', () => { + expect(component.action).toHaveBeenCalledWith('cc') + }) + }) describe('when writing text with 2 chars or less', () => { beforeEach(() => { component.inputRef.nativeElement.value = 'bl'