Skip to content

Commit

Permalink
feat(typeahead): add support for the 'select' output
Browse files Browse the repository at this point in the history
Closes #606

Closes #610
  • Loading branch information
pkozlowski-opensource committed Aug 19, 2016
1 parent 9e4035f commit c0dd7b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/typeahead/typeahead.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,22 @@ describe('ngb-typeahead', () => {
});
});

describe('select event', () => {

it('should raise select event when a result is selected', () => {
const fixture = createTestComponent('<input type="text" [ngbTypeahead]="find" (select)="onSelect($event)"/>');
const input = getNativeInput(fixture.nativeElement);

// clicking selected
changeInput(fixture.nativeElement, 'o');
fixture.detectChanges();
getWindowLinks(fixture.debugElement)[0].triggerEventHandler('click', {});
fixture.detectChanges();

expect(fixture.componentInstance.selectEventValue).toBe('one');
});
});

describe('auto attributes', () => {

it('should have autocomplete, autocapitalize and autocorrect attributes set to off', () => {
Expand All @@ -386,6 +402,7 @@ class TestComponent {
[{id: 1, value: 'one'}, {id: 10, value: 'one more'}, {id: 2, value: 'two'}, {id: 3, value: 'three'}];

model: any;
selectEventValue: any;

form = new FormGroup({control: new FormControl('', Validators.required)});

Expand All @@ -401,4 +418,6 @@ class TestComponent {
uppercaseFormatter = s => s.toUpperCase();

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

onSelect($event) { this.selectEventValue = $event; }
}
8 changes: 8 additions & 0 deletions src/typeahead/typeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
Directive,
OnInit,
Input,
Output,
EventEmitter,
ComponentRef,
ComponentFactoryResolver,
ViewContainerRef,
Expand Down Expand Up @@ -80,6 +82,11 @@ export class NgbTypeahead implements OnInit,
*/
@Input() resultTemplate: TemplateRef<ResultTplCtx>;

/**
* An event emitted when a match is selected. Event payload is equal to the selected item.
*/
@Output() select = new EventEmitter();

onChange = (value) => {
this._onChangeNoEmit(value);
this._valueChanges.next(value);
Expand Down Expand Up @@ -186,6 +193,7 @@ export class NgbTypeahead implements OnInit,
private _selectResult(result: any) {
this.writeValue(result);
this._onChangeNoEmit(result);
this.select.emit(result);
this.closePopup();
}
}
Expand Down

0 comments on commit c0dd7b7

Please sign in to comment.