From 2f7ddfcf32e29d0a7faf42ff857b5d5983efaad4 Mon Sep 17 00:00:00 2001 From: Ershova Irina Date: Fri, 7 Feb 2020 15:24:58 +0200 Subject: [PATCH] fix(typeahead): revert filter functionality Revert filter functionality to allow developers implement their own Close #5624 --- .../+typeahead/demos/async/async.html | 1 + .../+typeahead/demos/async/async.ts | 26 +++++++++++++++++-- src/typeahead/typeahead.directive.ts | 15 ++--------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/demo/src/app/components/+typeahead/demos/async/async.html b/demo/src/app/components/+typeahead/demos/async/async.html index 46732d8fae..35181ebc40 100644 --- a/demo/src/app/components/+typeahead/demos/async/async.html +++ b/demo/src/app/components/+typeahead/demos/async/async.html @@ -4,5 +4,6 @@ [typeahead]="dataSource" [typeaheadAsync]="true" typeaheadOptionField="name" + (typeaheadLoading)="changeTypeaheadLoading($event)" placeholder="Locations loaded via observable" class="form-control"> diff --git a/demo/src/app/components/+typeahead/demos/async/async.ts b/demo/src/app/components/+typeahead/demos/async/async.ts index 81f47581ec..bd93e2b89e 100644 --- a/demo/src/app/components/+typeahead/demos/async/async.ts +++ b/demo/src/app/components/+typeahead/demos/async/async.ts @@ -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; @@ -15,6 +16,7 @@ interface DataSourceType { export class DemoTypeaheadAsyncComponent { asyncSelected: string; dataSource: Observable; + typeaheadLoading: boolean; statesComplex: DataSourceType[] = [ { id: 1, name: 'Alabama', region: 'South' }, { id: 2, name: 'Alaska', region: 'West' }, @@ -69,6 +71,26 @@ export class DemoTypeaheadAsyncComponent { ]; constructor() { - this.dataSource = of(this.statesComplex); + this.dataSource = new Observable((observer: Subscriber) => { + // Runs on every search + observer.next(this.asyncSelected); + }) + .pipe( + mergeMap((token: string) => this.getStatesAsObservable(token)) + ); + } + + getStatesAsObservable(token: string): Observable { + 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; } } diff --git a/src/typeahead/typeahead.directive.ts b/src/typeahead/typeahead.directive.ts index 1feaab2695..31772123e6 100644 --- a/src/typeahead/typeahead.directive.ts +++ b/src/typeahead/typeahead.directive.ts @@ -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 { @@ -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);