Skip to content

Commit

Permalink
fix(typeahead): don't fail when user returns falsy results
Browse files Browse the repository at this point in the history
Fixes #2530

Closes #2550
  • Loading branch information
maxokorokov authored and pkozlowski-opensource committed Jul 27, 2018
1 parent efe01f1 commit 39a58a3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/typeahead/typeahead.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ describe('ngb-typeahead', () => {
expect(getWindow(compiled)).toBeNull();
});

it('should work when returning null as results', async(() => {
const fixture = createTestComponent(`<input type="text" [ngbTypeahead]="findNull"/>`);
const compiled = fixture.nativeElement;

fixture.whenStable().then(() => {
changeInput(compiled, 'one');
fixture.detectChanges();
expect(getWindow(compiled)).toBeNull();
});
}));

it('should be closed on document click', () => {
const fixture = createTestComponent(`<input type="text" [ngbTypeahead]="find"/>`);
const compiled = fixture.nativeElement;
Expand Down Expand Up @@ -1007,6 +1018,8 @@ class TestComponent {

findNothing = (text$: Observable<string>) => { return text$.pipe(map(text => [])); };

findNull = (text$: Observable<string>) => { return text$.pipe(map(text => null)); };

findObjects = (text$: Observable<string>) => {
return text$.pipe(map(text => this._objects.filter(v => v.value.startsWith(text))));
};
Expand Down
4 changes: 3 additions & 1 deletion src/typeahead/typeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ export class NgbTypeahead implements ControlValueAccessor,

this._showHint();
}
const count = results.length;

// live announcer
const count = results ? results.length : 0;
this._live.say(count === 0 ? 'No results available' : `${count} result${count === 1 ? '' : 's'} available`);
});
}
Expand Down

0 comments on commit 39a58a3

Please sign in to comment.