Skip to content

Commit

Permalink
Merge pull request #544 from geonetwork/fix-search-buttons
Browse files Browse the repository at this point in the history
fix: autocomplete behavior
  • Loading branch information
f-necas authored Jul 27, 2023
2 parents 47cf495 + cab7e52 commit 044f11f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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'
Expand Down
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 || ''
}
}

clear(): void {
Expand Down

0 comments on commit 044f11f

Please sign in to comment.