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

Typeahead MinLength=0 #412

Merged
merged 4 commits into from
Apr 21, 2016
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
2 changes: 1 addition & 1 deletion components/typeahead/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 27 additions & 4 deletions components/typeahead/typeahead.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,7 +92,7 @@ export class Typeahead implements OnInit {
this.debouncer();
}

if (this.typeaheadAsync === false) {
if (!this.typeaheadAsync) {
this.processMatches();
this.finalizeAsyncCall();
}
Expand All @@ -102,6 +102,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) {
this.processMatches();
this.finalizeAsyncCall();
}
}
}

@HostListener('blur', ['$event.target'])
protected onBlur():void {
console.log('blur')
Expand Down Expand Up @@ -147,7 +163,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
Expand Down Expand Up @@ -290,6 +306,13 @@ export class Typeahead implements OnInit {
return;
}

if (!this.cd.model) {
for (let i = 0; i < Math.min(this.typeaheadOptionsLimit, 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
Expand Down Expand Up @@ -351,7 +374,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;
}
Expand Down