Skip to content

Commit

Permalink
feat(typeahead): - added typeaheadOnBlur event (#1639)
Browse files Browse the repository at this point in the history
* - added typeaheadOnBlur event
- added documentation for typeaheadOnBlur
- removed tab event key since blur event handles it
- added getter to _active item

* - fixed syntax error

* - maybe dependency problem?

* - added unit tests

* trying to build again
  • Loading branch information
imribarr-compit authored and valorkin committed Feb 28, 2017
1 parent d8f42df commit 62eb22a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions demo/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,10 @@ export const ngdoc: any = {
{
"name": "typeaheadOnSelect",
"description": "<p>fired when option was selected, return object with data of this option </p>\n"
},
{
"name": "typeaheadOnBlur",
"description": "<p>fired when blur event occurres. returns the active item </p>\n"
}
],
"properties": [
Expand Down
23 changes: 22 additions & 1 deletion src/spec/typeahead.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ interface State {
template: `
<input [(ngModel)]="selectedState"
[typeahead]="states"
[typeaheadOptionField]="'name'">
[typeaheadOptionField]="'name'"
(typeaheadOnBlur)="onBlurEvent($event)">
`
})
class TestTypeaheadComponent {
Expand All @@ -28,6 +29,8 @@ class TestTypeaheadComponent {
{id: 1, name: 'Alabama', region: 'South'},
{id: 2, name: 'Alaska', region: 'West'}
];

public onBlurEvent (activeItem) { };
}

describe('Directive: Typeahead', () => {
Expand Down Expand Up @@ -166,4 +169,22 @@ describe('Directive: Typeahead', () => {
});
});

describe('onBlur', () => {
beforeEach(fakeAsync(() => {
inputElement.value = 'Alab';
fireEvent(inputElement, 'keyup');

fixture.detectChanges();
tick(100);
}));

it('blur event should send the correct active item', () => {
spyOn(fixture.componentInstance, 'onBlurEvent').and.callFake((param) => {
expect(param.item.id).toBe(1);
});
directive.onBlur();
fixture.detectChanges();
});
});

});
4 changes: 4 additions & 0 deletions src/typeahead/typeahead-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export class TypeaheadContainerComponent {
this.element = element;
}

public get active(): TypeaheadMatch {
return this._active;
}

public get matches(): TypeaheadMatch[] {
return this._matches;
}
Expand Down
10 changes: 3 additions & 7 deletions src/typeahead/typeahead.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
@Output() public typeaheadNoResults: EventEmitter<boolean> = new EventEmitter();
/** fired when option was selected, return object with data of this option */
@Output() public typeaheadOnSelect: EventEmitter<TypeaheadMatch> = new EventEmitter();
/** fired when blur event occurres. returns the active item */
@Output() public typeaheadOnBlur: EventEmitter<any> = new EventEmitter();

/**
* A selector specifying the element the typeahead should be appended to.
Expand Down Expand Up @@ -143,6 +145,7 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
@HostListener('blur')
public onBlur(): void {
if (this._container && !this._container.isFocused) {
this.typeaheadOnBlur.emit(this._container.active);
this.hide();
}
}
Expand All @@ -159,13 +162,6 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
e.preventDefault();
return;
}

// if tab default browser behavior will select next input field, and
// therefore we should close the items list
if (e.keyCode === 9) {
this.hide();
return;
}
}

public constructor(control: NgControl, viewContainerRef: ViewContainerRef, element: ElementRef, renderer: Renderer, cis: ComponentLoaderFactory) {
Expand Down

0 comments on commit 62eb22a

Please sign in to comment.