Skip to content

Commit

Permalink
fix(typeahead): revert filter functionality
Browse files Browse the repository at this point in the history
Revert filter functionality to allow developers implement their own

Close valor-software#5624
  • Loading branch information
IraErshova committed Feb 7, 2020
1 parent 7fc365e commit 2f7ddfc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions demo/src/app/components/+typeahead/demos/async/async.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
[typeahead]="dataSource"
[typeaheadAsync]="true"
typeaheadOptionField="name"
(typeaheadLoading)="changeTypeaheadLoading($event)"
placeholder="Locations loaded via observable"
class="form-control">
26 changes: 24 additions & 2 deletions demo/src/app/components/+typeahead/demos/async/async.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component } from '@angular/core';

import { Observable, of } from 'rxjs';
import { Observable, of, Subscriber } from 'rxjs';
import { mergeMap } from 'rxjs/operators';

interface DataSourceType {
id: number;
Expand All @@ -15,6 +16,7 @@ interface DataSourceType {
export class DemoTypeaheadAsyncComponent {
asyncSelected: string;
dataSource: Observable<DataSourceType[]>;
typeaheadLoading: boolean;
statesComplex: DataSourceType[] = [
{ id: 1, name: 'Alabama', region: 'South' },
{ id: 2, name: 'Alaska', region: 'West' },
Expand Down Expand Up @@ -69,6 +71,26 @@ export class DemoTypeaheadAsyncComponent {
];

constructor() {
this.dataSource = of(this.statesComplex);
this.dataSource = new Observable((observer: Subscriber<string>) => {
// Runs on every search
observer.next(this.asyncSelected);
})
.pipe(
mergeMap((token: string) => this.getStatesAsObservable(token))
);
}

getStatesAsObservable(token: string): Observable<DataSourceType[]> {
const query = new RegExp(token, 'i');

return of(
this.statesComplex.filter((state: any) => {
return query.test(state.name);
})
);
}

changeTypeaheadLoading(e: boolean): void {
this.typeaheadLoading = e;
}
}
15 changes: 2 additions & 13 deletions src/typeahead/typeahead.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { TypeaheadContainerComponent } from './typeahead-container.component';
import { TypeaheadMatch } from './typeahead-match.class';
import { TypeaheadConfig } from './typeahead.config';
import { getValueFromObject, latinize, tokenize } from './typeahead-utils';
import { debounceTime, filter, map, mergeMap, switchMap, toArray } from 'rxjs/operators';
import { debounceTime, filter, mergeMap, switchMap, toArray } from 'rxjs/operators';

@Directive({selector: '[typeahead]', exportAs: 'bs-typeahead'})
export class TypeaheadDirective implements OnInit, OnDestroy {
Expand Down Expand Up @@ -394,18 +394,7 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
this.keyUpEventEmitter
.pipe(
debounceTime(this.typeaheadWaitMs),
switchMap((value: string) => {
return this.typeahead
.pipe(
// tslint:disable-next-line:no-any
map((typeahead: any[]) => {
const normalizedQuery = this.normalizeQuery(value);

return typeahead.filter((option: any) =>
option && this.testMatch(this.normalizeOption(option), normalizedQuery));
})
);
})
switchMap(() => this.typeahead)
)
.subscribe((matches: TypeaheadMatch[]) => {
this.finalizeAsyncCall(matches);
Expand Down

0 comments on commit 2f7ddfc

Please sign in to comment.