Skip to content

Commit

Permalink
fix(typeahead): don't show hint if there is no active option
Browse files Browse the repository at this point in the history
Fixes #2185

Closes #2198
  • Loading branch information
pkozlowski-opensource committed Mar 9, 2018
1 parent b9ad060 commit 5d20d1f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/typeahead/typeahead-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export class NgbTypeaheadWindow implements OnInit {

@Output('activeChange') activeChangeEvent = new EventEmitter();

hasActive() { return this.activeIdx > -1 && this.activeIdx < this.results.length; }

getActive() { return this.results[this.activeIdx]; }

markActive(activeIdx: number) {
Expand Down
15 changes: 15 additions & 0 deletions src/typeahead/typeahead.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,21 @@ describe('ngb-typeahead', () => {
expect(inputEl.selectionEnd).toBe(2);
});
}));

it('should not show hint when there is no result selected', async(() => {
const fixture = createTestComponent(
`<input type="text" [(ngModel)]="model" [ngbTypeahead]="find" [showHint]="true" [focusFirst]="false"/>`);
fixture.detectChanges();
const compiled = fixture.nativeElement;
const inputEl = getNativeInput(compiled);

fixture.whenStable().then(() => {
changeInput(compiled, 'on');
fixture.detectChanges();
expectWindowResults(compiled, ['one', 'one more']);
expect(inputEl.value).toBe('on');
});
}));
});

describe('Custom config', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/typeahead/typeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export class NgbTypeahead implements ControlValueAccessor,
}

private _showHint() {
if (this.showHint && this._inputValueBackup != null) {
if (this.showHint && this._windowRef.instance.hasActive() && this._inputValueBackup != null) {
const userInputLowerCase = this._inputValueBackup.toLowerCase();
const formattedVal = this._formatItemForInput(this._windowRef.instance.getActive());

Expand Down Expand Up @@ -350,12 +350,13 @@ export class NgbTypeahead implements ControlValueAccessor,
if (this.resultTemplate) {
this._windowRef.instance.resultTemplate = this.resultTemplate;
}
this._showHint();

// The observable stream we are subscribing to might have async steps
// and if a component containing typeahead is using the OnPush strategy
// the change detection turn wouldn't be invoked automatically.
this._windowRef.changeDetectorRef.detectChanges();

this._showHint();
}
});
}
Expand Down

0 comments on commit 5d20d1f

Please sign in to comment.