Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(input): adds blur and focus event support #449

Merged
merged 1 commit into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/input/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
[spellcheck]="spellcheck"
[attr.maxlength]="maxLength"
[type]="type"
(focus)="onFocus()"
(blur)="onBlur()"
(focus)="handleFocus($event)"
(blur)="handleBlur($event)"
[(ngModel)]="value"
(change)="onChange($event)">
(change)="handleChange($event)">

<label class="md-input-placeholder"
[attr.for]="id"
Expand Down
36 changes: 36 additions & 0 deletions src/components/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,30 @@ export function main() {
expect(typeof fixture.componentInstance.value).toBe('number');
});
});

it('supports blur and focus events', () => {
return builder.createAsync(MdInputWithBlurAndFocusEvents).then(fixture => {
const testComponent = fixture.componentInstance;
const inputComponent = fixture.debugElement.query(By.directive(MdInput)).componentInstance;
const fakeEvent = <FocusEvent>{};
fakeAsync(() => {
spyOn(testComponent, 'onFocus');
spyOn(testComponent, 'onBlur');

expect(testComponent.onFocus).not.toHaveBeenCalled();
expect(testComponent.onBlur).not.toHaveBeenCalled();

inputComponent.handleFocus(fakeEvent);
tick();
expect(testComponent.onFocus).toHaveBeenCalledWith(fakeEvent);


inputComponent.handleBlur(fakeEvent);
tick();
expect(testComponent.onBlur).toHaveBeenCalledWith(fakeEvent);
})();
});
});
});
}

Expand Down Expand Up @@ -387,3 +411,15 @@ class MdInputAriaTestController {
ariaLabel: string = 'label';
ariaDisabled: boolean = true;
}

@Component({
selector: 'test-input-controller',
template: `
<md-input (focus)="onFocus($event)" (blur)="onBlur($event)"></md-input>
`,
directives: [MdInput]
})
class MdInputWithBlurAndFocusEvents {
onBlur(event: FocusEvent) {}
onFocus(event: FocusEvent) {}
}
28 changes: 24 additions & 4 deletions src/components/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import {
ElementRef,
QueryList,
OnChanges,
EventEmitter,
Output,
} from '@angular/core';
import {
NG_VALUE_ACCESSOR,
ControlValueAccessor
} from '@angular/common';
import {BooleanFieldValue} from '../../core/annotations/field-value';
import {MdError} from '../../core/errors/error';
import {Observable} from 'rxjs/Observable';


const noop = () => {};
Expand Down Expand Up @@ -144,6 +147,19 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
@Input() @BooleanFieldValue() spellcheck: boolean = false;
@Input() type: string = 'text';

private _blurEmitter: EventEmitter<FocusEvent> = new EventEmitter<FocusEvent>();
private _focusEmitter: EventEmitter<FocusEvent> = new EventEmitter<FocusEvent>();

@Output('blur')
get onBlur(): Observable<FocusEvent> {
return this._blurEmitter.asObservable();
}

@Output('focus')
get onFocus(): Observable<FocusEvent> {
return this._focusEmitter.asObservable();
}

get value(): any { return this._value; };
@Input() set value(v: any) {
v = this._convertValueForInputType(v);
Expand All @@ -167,17 +183,21 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
}

/** @internal */
onFocus() {
handleFocus(event: FocusEvent) {
this._focused = true;
this._focusEmitter.emit(event);
}

/** @internal */
onBlur() {
handleBlur(event: FocusEvent) {
this._focused = false;
this._onTouchedCallback();
this._blurEmitter.emit(event);
}

/** @internal */
onChange(ev: Event) {
this.value = (<HTMLInputElement>ev.target).value;
handleChange(event: Event) {
this.value = (<HTMLInputElement>event.target).value;
this._onTouchedCallback();
}

Expand Down