Skip to content

Commit

Permalink
fix: autocomplete behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
f-necas committed Jul 25, 2023
1 parent 45c0861 commit ec535b8
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions libs/ui/inputs/src/lib/autocomplete/autocomplete.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ 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,
distinctUntilChanged,
filter,
finalize,
first,
map,
switchMap,
take,
tap,
Expand Down Expand Up @@ -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
Expand All @@ -115,6 +119,9 @@ export class AutocompleteComponent
if (value) {
this.control.setValue(value)
}
if (this.inputRef) {
this.inputRef.nativeElement.value = (value as any)?.title || ''

Check warning on line 123 in libs/ui/inputs/src/lib/autocomplete/autocomplete.component.ts

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

Unexpected any. Specify a different type
}
}

clear(): void {
Expand Down

0 comments on commit ec535b8

Please sign in to comment.