Skip to content

Commit 3e88fe9

Browse files
committed
fix(input): add event emitters for blur and focus to the ion-input component
fixes #5487
1 parent 9047754 commit 3e88fe9

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

ionic/components/input/input-base.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export class InputBase {
3333
@Input() clearInput;
3434
@Input() placeholder: string = '';
3535
@ViewChild(NativeInput) protected _native: NativeInput;
36+
@Output() blur: EventEmitter<Event> = new EventEmitter;
37+
@Output() focus: EventEmitter<Event> = new EventEmitter;
3638

3739
constructor(
3840
config: Config,

ionic/components/input/input.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, Optional, ElementRef} from 'angular2/core';
1+
import {Component, Optional, ElementRef, ViewChild} from 'angular2/core';
22
import {NgIf, NgControl} from 'angular2/common';
33

44
import {Button} from '../button/button';
@@ -65,7 +65,7 @@ import {Platform} from '../../platform/platform';
6565
@Component({
6666
selector: 'ion-input',
6767
template:
68-
'<input [type]="type" [(ngModel)]="_value" [placeholder]="placeholder" class="text-input">' +
68+
'<input [type]="type" [(ngModel)]="_value" (blur)="inputBlurred($event)" (focus)="inputFocused($event)" [placeholder]="placeholder" class="text-input">' +
6969
'<input [type]="type" aria-hidden="true" next-input *ngIf="_useAssist">' +
7070
'<button clear *ngIf="clearInput && value" class="text-input-clear-icon" (click)="clearTextInput()" (mousedown)="clearTextInput()"></button>' +
7171
'<div (touchstart)="pointerStart($event)" (touchend)="pointerEnd($event)" (mousedown)="pointerStart($event)" (mouseup)="pointerEnd($event)" class="input-cover" tappable *ngIf="_useAssist"></div>',
@@ -90,6 +90,20 @@ export class TextInput extends InputBase {
9090
) {
9191
super(config, form, item, app, platform, elementRef, scrollView, nav, ngControl);
9292
}
93+
94+
/**
95+
* @private
96+
*/
97+
inputBlurred(event) {
98+
this.blur.emit(event);
99+
}
100+
101+
/**
102+
* @private
103+
*/
104+
inputFocused(event) {
105+
this.focus.emit(event);
106+
}
93107
}
94108

95109

@@ -134,7 +148,7 @@ export class TextInput extends InputBase {
134148
@Component({
135149
selector: 'ion-textarea',
136150
template:
137-
'<textarea [(ngModel)]="_value" [placeholder]="placeholder" class="text-input"></textarea>' +
151+
'<textarea [(ngModel)]="_value" (blur)="inputBlurred($event)" (focus)="inputFocused($event)" [placeholder]="placeholder" class="text-input"></textarea>' +
138152
'<input type="text" aria-hidden="true" next-input *ngIf="_useAssist">' +
139153
'<div (touchstart)="pointerStart($event)" (touchend)="pointerEnd($event)" (mousedown)="pointerStart($event)" (mouseup)="pointerEnd($event)" class="input-cover" tappable *ngIf="_useAssist"></div>',
140154
directives: [
@@ -167,4 +181,18 @@ export class TextArea extends InputBase {
167181
this._item.setCssClass('item-textarea', true);
168182
}
169183
}
184+
185+
/**
186+
* @private
187+
*/
188+
inputBlurred(event) {
189+
this.blur.emit(event);
190+
}
191+
192+
/**
193+
* @private
194+
*/
195+
inputFocused(event) {
196+
this.focus.emit(event);
197+
}
170198
}
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import {App} from 'ionic-angular';
1+
import {App, Page} from 'ionic-angular';
22

33

4-
@App({
4+
@Page({
55
templateUrl: 'main.html'
66
})
7-
class E2EApp {
7+
class PageOne {
88
url;
9+
input1: string = 'Text 1';
910

10-
constructor() {
11-
this.input1 = 'Text 1';
11+
onEvent(event) {
12+
console.log("Did Event:", event.type);
1213
}
1314
}
15+
16+
@App({
17+
template: '<ion-nav [root]="root"></ion-nav>'
18+
})
19+
class E2EApp {
20+
root = PageOne;
21+
}

ionic/components/input/test/fixed-inline-labels/main.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
<ion-item>
1111
<ion-label fixed>To</ion-label>
12-
<ion-input [(ngModel)]="input1"></ion-input>
12+
<ion-input [(ngModel)]="input1"
13+
(blur)="onEvent($event)"
14+
(input)="onEvent($event)"
15+
(focus)="onEvent($event)">
16+
</ion-input>
1317
</ion-item>
1418

1519
<ion-item>
@@ -29,7 +33,11 @@
2933

3034
<ion-item>
3135
<ion-label fixed>Comments</ion-label>
32-
<ion-textarea value="Comment value"></ion-textarea>
36+
<ion-textarea value="Comment value"
37+
(blur)="onEvent($event)"
38+
(input)="onEvent($event)"
39+
(focus)="onEvent($event)">
40+
</ion-textarea>
3341
</ion-item>
3442

3543
<ion-item>

0 commit comments

Comments
 (0)