7
7
*/
8
8
9
9
import { ReplaySubject } from 'rxjs' ;
10
- import { Directive , ElementRef , EventEmitter , OnDestroy , OnInit } from '@angular/core' ;
10
+ import { Directive , ElementRef , EventEmitter , OnDestroy , OnInit , HostListener } from '@angular/core' ;
11
11
import { EDIT_PANE_SELECTOR } from './constants' ;
12
12
import { closest } from './polyfill' ;
13
13
import { EditRef } from './edit-ref' ;
@@ -23,13 +23,6 @@ export type PopoverEditClickOutBehavior = 'close' | 'submit' | 'noop';
23
23
*/
24
24
@Directive ( {
25
25
selector : 'form[cdkEditControl]' ,
26
- host : {
27
- '(ngSubmit)' : 'handleFormSubmit()' ,
28
- '(keydown.enter)' : 'editRef.trackEnterPressForClose(true)' ,
29
- '(keyup.enter)' : 'editRef.trackEnterPressForClose(false)' ,
30
- '(keyup.escape)' : 'close()' ,
31
- '(document:click)' : 'handlePossibleClickOut($event)' ,
32
- } ,
33
26
inputs : [
34
27
'clickOutBehavior: cdkEditControlClickOutBehavior' ,
35
28
'preservedFormValue: cdkEditControlPreservedFormValue' ,
@@ -79,6 +72,11 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
79
72
* the form for validity before proceeding.
80
73
* Updates the revert state with the latest submitted value then closes the edit.
81
74
*/
75
+ // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
76
+ // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
77
+ // can move this back into `host`.
78
+ // tslint:disable:no-host-decorator-in-concrete
79
+ @HostListener ( 'ngSubmit' )
82
80
handleFormSubmit ( ) : void {
83
81
if ( this . ignoreSubmitUnlessValid && ! this . editRef . isValid ( ) ) { return ; }
84
82
@@ -97,6 +95,11 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
97
95
* Called on click anywhere in the document.
98
96
* If the click was outside of the lens, trigger the specified click out behavior.
99
97
*/
98
+ // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
99
+ // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
100
+ // can move this back into `host`.
101
+ // tslint:disable:no-host-decorator-in-concrete
102
+ @HostListener ( 'document:click' , [ '$event' ] )
100
103
handlePossibleClickOut ( evt : Event ) : void {
101
104
if ( closest ( evt . target , EDIT_PANE_SELECTOR ) ) { return ; }
102
105
@@ -113,6 +116,29 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
113
116
}
114
117
}
115
118
119
+ // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
120
+ // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
121
+ // can move this back into `host`.
122
+ // tslint:disable:no-host-decorator-in-concrete
123
+ @HostListener ( 'keydown' )
124
+ _handleKeydown ( ) {
125
+ this . editRef . trackEnterPressForClose ( true ) ;
126
+ }
127
+
128
+ // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
129
+ // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
130
+ // can move this back into `host`.
131
+ // tslint:disable:no-host-decorator-in-concrete
132
+ @HostListener ( 'keyup' , [ '$event' ] )
133
+ _handleKeyup ( event : KeyboardEvent ) {
134
+ // TODO(crisbeto): should use cdk/keycodes once the tests are reworked to use cdk/testing.
135
+ if ( event . key === 'Enter' ) {
136
+ this . editRef . trackEnterPressForClose ( false ) ;
137
+ } else if ( event . key === 'Escape' ) {
138
+ this . close ( ) ;
139
+ }
140
+ }
141
+
116
142
/** Triggers submit on tab out if clickOutBehavior is 'submit'. */
117
143
private _handleBlur ( ) : void {
118
144
if ( this . clickOutBehavior === 'submit' ) {
@@ -130,14 +156,18 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
130
156
@Directive ( {
131
157
selector : 'button[cdkEditRevert]' ,
132
158
host : {
133
- '(click)' : 'revertEdit()' ,
134
159
'type' : 'button' , // Prevents accidental form submits.
135
160
}
136
161
} )
137
162
export class CdkEditRevert < FormValue > {
138
163
constructor (
139
164
protected readonly editRef : EditRef < FormValue > ) { }
140
165
166
+ // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
167
+ // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
168
+ // can move this back into `host`.
169
+ // tslint:disable:no-host-decorator-in-concrete
170
+ @HostListener ( 'click' )
141
171
revertEdit ( ) : void {
142
172
this . editRef . reset ( ) ;
143
173
}
@@ -147,14 +177,18 @@ export class CdkEditRevert<FormValue> {
147
177
@Directive ( {
148
178
selector : 'button[cdkEditClose]' ,
149
179
host : {
150
- '(click)' : 'closeEdit()' ,
151
180
'type' : 'button' , // Prevents accidental form submits.
152
181
}
153
182
} )
154
183
export class CdkEditClose < FormValue > {
155
184
constructor (
156
185
protected readonly editRef : EditRef < FormValue > ) { }
157
186
187
+ // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
188
+ // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
189
+ // can move this back into `host`.
190
+ // tslint:disable:no-host-decorator-in-concrete
191
+ @HostListener ( 'click' )
158
192
closeEdit ( ) : void {
159
193
this . editRef . closeAfterEnterKeypress ( ) ;
160
194
}
0 commit comments