Skip to content

Commit

Permalink
fix(typeahead): use custom input formatter with falsy values
Browse files Browse the repository at this point in the history
Fixes #2389

Closes #2399
  • Loading branch information
pkozlowski-opensource committed May 24, 2018
1 parent dcd7111 commit b73f350
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 29 additions & 2 deletions src/typeahead/typeahead.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,33 @@ describe('ngb-typeahead', () => {
})
.then(() => { expectInputValue(el, 'TEXT'); });
}));

it('should use custom input formatter with falsy values', async(() => {
const html = '<input [(ngModel)]="model" [ngbTypeahead]="findNothing" [inputFormatter]="uppercaseFormatter"/>';
const fixture = createTestComponent(html);
const el = fixture.nativeElement;
const comp = fixture.componentInstance;
expectInputValue(el, '');

comp.model = null;
fixture.detectChanges();
fixture.whenStable()
.then(() => {
expectInputValue(el, '');

comp.model = 0;
fixture.detectChanges();
return fixture.whenStable();
})
.then(() => {
expectInputValue(el, '0');

comp.model = false;
fixture.detectChanges();
return fixture.whenStable();
})
.then(() => { expectInputValue(el, 'FALSE'); });
}));
});

describe('window', () => {
Expand Down Expand Up @@ -1098,9 +1125,9 @@ class TestComponent {

formatter = (obj: {id: number, value: string}) => { return `${obj.id} ${obj.value}`; };

uppercaseFormatter = s => s.toUpperCase();
uppercaseFormatter = s => `${s}`.toUpperCase();

uppercaseObjFormatter = (obj: {value: string}) => { return obj.value.toUpperCase(); };
uppercaseObjFormatter = (obj: {value: string}) => { return `${obj.value}`.toUpperCase(); };


onSelect($event) { this.selectEventValue = $event; }
Expand Down
2 changes: 1 addition & 1 deletion src/typeahead/typeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export class NgbTypeahead implements ControlValueAccessor,
}

private _formatItemForInput(item: any): string {
return item && this.inputFormatter ? this.inputFormatter(item) : toString(item);
return item != null && this.inputFormatter ? this.inputFormatter(item) : toString(item);
}

private _writeInputValue(value: string): void {
Expand Down

0 comments on commit b73f350

Please sign in to comment.