From 61d27fa247660f1c751e74d95ce1b42b57e1c5a3 Mon Sep 17 00:00:00 2001 From: reinbaarsma Date: Thu, 14 Apr 2016 14:32:42 +0200 Subject: [PATCH 1/3] Possibility of MinLength=0 --- components/typeahead/typeahead.directive.ts | 29 ++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/components/typeahead/typeahead.directive.ts b/components/typeahead/typeahead.directive.ts index 8bb0bcdb9f..20ca60516b 100644 --- a/components/typeahead/typeahead.directive.ts +++ b/components/typeahead/typeahead.directive.ts @@ -21,7 +21,7 @@ export class Typeahead implements OnInit { @Output() public typeaheadOnSelect:EventEmitter<{item:any}> = new EventEmitter(false); @Input() public typeahead:any; - @Input() public typeaheadMinLength:number; + @Input() public typeaheadMinLength:number = void 0; @Input() public typeaheadWaitMs:number; @Input() public typeaheadOptionsLimit:number; @Input() public typeaheadOptionField:string; @@ -100,6 +100,22 @@ export class Typeahead implements OnInit { } } + @HostListener('focus', ['$event.target']) + protected onFocus():void { + if (this.typeaheadMinLength === 0) { + this.typeaheadLoading.emit(true); + + if (this.typeaheadAsync === true) { + this.debouncer(); + } + + if (this.typeaheadAsync === false) { + this.processMatches(); + this.finalizeAsyncCall(); + } + } + } + @HostListener('blur', ['$event.target']) protected onBlur():void { // Allow typeahead container click event to be triggered requires a timeout @@ -124,7 +140,7 @@ export class Typeahead implements OnInit { public ngOnInit():void { this.typeaheadOptionsLimit = this.typeaheadOptionsLimit || 20; - this.typeaheadMinLength = this.typeaheadMinLength || 1; + this.typeaheadMinLength = this.typeaheadMinLength === void 0 ? 1 : this.typeaheadMinLength; this.typeaheadWaitMs = this.typeaheadWaitMs || 0; // async should be false in case of array @@ -266,6 +282,13 @@ export class Typeahead implements OnInit { return; } + if (!this.cd.model) { + for (let i = 0; i < this.typeahead.length; i++) { + this._matches.push(this.typeahead[i]); + } + return; + } + // If singleWords, break model here to not be doing extra work on each // iteration let normalizedQuery = (this.typeaheadLatinize @@ -327,7 +350,7 @@ export class Typeahead implements OnInit { this.typeaheadNoResults.emit(this.cd.model.toString().length >= this.typeaheadMinLength && this.matches.length <= 0); - if (this.cd.model.toString().length <= 0 || this._matches.length <= 0) { + if (this._matches.length <= 0) { this.hide(); return; } From 775b6cea36ad0da5cd9b09dd42d0458ff613d696 Mon Sep 17 00:00:00 2001 From: reinbaarsma Date: Wed, 20 Apr 2016 15:18:57 +0200 Subject: [PATCH 2/3] fixed comments from @valorkin --- components/typeahead/typeahead.directive.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/typeahead/typeahead.directive.ts b/components/typeahead/typeahead.directive.ts index 20ca60516b..0d43602a96 100644 --- a/components/typeahead/typeahead.directive.ts +++ b/components/typeahead/typeahead.directive.ts @@ -90,7 +90,7 @@ export class Typeahead implements OnInit { this.debouncer(); } - if (this.typeaheadAsync === false) { + if (!this.typeaheadAsync) { this.processMatches(); this.finalizeAsyncCall(); } @@ -109,7 +109,7 @@ export class Typeahead implements OnInit { this.debouncer(); } - if (this.typeaheadAsync === false) { + if (!this.typeaheadAsync) { this.processMatches(); this.finalizeAsyncCall(); } @@ -283,7 +283,7 @@ export class Typeahead implements OnInit { } if (!this.cd.model) { - for (let i = 0; i < this.typeahead.length; i++) { + for (let i = 0; i < Math.min(this.typeaheadOptionsLimit, this.typeahead.length); i++) { this._matches.push(this.typeahead[i]); } return; From f3a76c1ea624434de954b5d1a8adc36b8139bf29 Mon Sep 17 00:00:00 2001 From: Rein Baarsma Date: Thu, 21 Apr 2016 10:17:59 +0200 Subject: [PATCH 3/3] Update readme.md --- components/typeahead/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/typeahead/readme.md b/components/typeahead/readme.md index 5bb92b1bdf..d093c1370f 100644 --- a/components/typeahead/readme.md +++ b/components/typeahead/readme.md @@ -40,7 +40,7 @@ export class Typeahead implements OnInit { - `ngModel` (`string`) - binds to string user's input - `typeahead` (`any`) - options source, can be Array of strings or objects or function that return Promise for external matching process - - `typeaheadMinLength` (`?number=1`) - minimal no of characters that needs to be entered before typeahead kicks-in. Must be greater than or equal to 1. + - `typeaheadMinLength` (`?number=1`) - minimal no of characters that needs to be entered before typeahead kicks-in. When set to 0, typeahead shows on focus with full list of options (limited as normal by typeaheadOptionsLimit) - `typeaheadWaitMs` (`?number=0`) - minimal wait time after last character typed before typeahead kicks-in - `typeaheadOptionsLimit` (`?number=20`) - maximum length of options items list - `typeaheadOptionField` (`?string`) - name of field in array of states that contain options as objects, we use array item as option in case of this field is missing