Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: autocomplete behavior #544

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
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 @@
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 Expand Up @@ -168,10 +179,10 @@
})

describe('@Input() value', () => {
let anyEmitted

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

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

'anyEmitted' is assigned a value but never used
describe('when set', () => {
beforeEach(() => {
const simpleChanges: any = {

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

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

Unexpected any. Specify a different type
value: {
previousValue: undefined,
currentValue: { title: 'hello' },
Expand All @@ -187,7 +198,7 @@
})
describe('when changed', () => {
beforeEach(() => {
const simpleChanges: any = {

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

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

Unexpected any. Specify a different type
value: {
previousValue: { title: 'hello' },
currentValue: { title: 'good bye' },
Expand All @@ -204,7 +215,7 @@
describe('when ref changed but same text', () => {
let anyEmitted
beforeEach(() => {
const simpleChanges: any = {

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

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

Unexpected any. Specify a different type
value: {
previousValue: { title: 'good bye' },
currentValue: { title: 'good bye' },
Expand All @@ -224,7 +235,7 @@
describe('when not set on init (firstChange == true)', () => {
beforeEach(() => {
component.inputSubmitted.subscribe((event) => (anyEmitted = event))
const simpleChanges: any = {

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

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

Unexpected any. Specify a different type
value: {
firstChange: true,
previousValue: undefined,
Expand All @@ -241,7 +252,7 @@

describe('#handleSelection', () => {
let selectionEmitted
const selectionEvent: any = {

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

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

Unexpected any. Specify a different type
option: {
value: 'first',
},
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 @@
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 @@
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 @@
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
Loading