7
7
*/
8
8
9
9
import { Subject } from 'rxjs' ;
10
- import {
11
- Directive ,
12
- ElementRef ,
13
- EventEmitter ,
14
- OnDestroy ,
15
- OnInit ,
16
- Input ,
17
- HostListener ,
18
- } from '@angular/core' ;
10
+ import { Directive , ElementRef , EventEmitter , OnDestroy , OnInit , Input } from '@angular/core' ;
19
11
import { hasModifierKey } from '@angular/cdk/keycodes' ;
20
12
import { EDIT_PANE_SELECTOR } from './constants' ;
21
13
import { closest } from './polyfill' ;
@@ -39,6 +31,11 @@ export type PopoverEditClickOutBehavior = 'close' | 'submit' | 'noop';
39
31
] ,
40
32
outputs : [ 'preservedFormValueChange: cdkEditControlPreservedFormValueChange' ] ,
41
33
providers : [ EditRef ] ,
34
+ host : {
35
+ '(ngSubmit)' : 'handleFormSubmit()' ,
36
+ '(document:click)' : 'handlePossibleClickOut($event)' ,
37
+ '(keydown)' : '_handleKeydown($event)' ,
38
+ } ,
42
39
} )
43
40
export class CdkEditControl < FormValue > implements OnDestroy , OnInit {
44
41
protected readonly destroyed = new Subject < void > ( ) ;
@@ -81,11 +78,6 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
81
78
* the form for validity before proceeding.
82
79
* Updates the revert state with the latest submitted value then closes the edit.
83
80
*/
84
- // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
85
- // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
86
- // can move this back into `host`.
87
- // tslint:disable-next-line:no-host-decorator-in-concrete
88
- @HostListener ( 'ngSubmit' )
89
81
handleFormSubmit ( ) : void {
90
82
if ( this . ignoreSubmitUnlessValid && ! this . editRef . isValid ( ) ) {
91
83
return ;
@@ -106,11 +98,6 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
106
98
* Called on click anywhere in the document.
107
99
* If the click was outside of the lens, trigger the specified click out behavior.
108
100
*/
109
- // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
110
- // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
111
- // can move this back into `host`.
112
- // tslint:disable-next-line:no-host-decorator-in-concrete
113
- @HostListener ( 'document:click' , [ '$event' ] )
114
101
handlePossibleClickOut ( evt : Event ) : void {
115
102
if ( closest ( evt . target , EDIT_PANE_SELECTOR ) ) {
116
103
return ;
@@ -129,11 +116,6 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
129
116
}
130
117
}
131
118
132
- // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
133
- // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
134
- // can move this back into `host`.
135
- // tslint:disable-next-line:no-host-decorator-in-concrete
136
- @HostListener ( 'keydown' , [ '$event' ] )
137
119
_handleKeydown ( event : KeyboardEvent ) {
138
120
if ( event . key === 'Escape' && ! hasModifierKey ( event ) ) {
139
121
this . close ( ) ;
@@ -159,6 +141,7 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
159
141
selector : 'button[cdkEditRevert]' ,
160
142
host : {
161
143
'type' : 'button' , // Prevents accidental form submits.
144
+ '(click)' : 'revertEdit()' ,
162
145
} ,
163
146
} )
164
147
export class CdkEditRevert < FormValue > {
@@ -167,18 +150,20 @@ export class CdkEditRevert<FormValue> {
167
150
168
151
constructor ( protected readonly editRef : EditRef < FormValue > ) { }
169
152
170
- // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
171
- // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
172
- // can move this back into `host`.
173
- // tslint:disable-next-line:no-host-decorator-in-concrete
174
- @HostListener ( 'click' )
175
153
revertEdit ( ) : void {
176
154
this . editRef . reset ( ) ;
177
155
}
178
156
}
179
157
180
158
/** Closes the lens on click. */
181
- @Directive ( { selector : '[cdkEditClose]' } )
159
+ @Directive ( {
160
+ selector : '[cdkEditClose]' ,
161
+ host : {
162
+ '(click)' : 'closeEdit()' ,
163
+ '(keydown.enter)' : 'closeEdit()' ,
164
+ '(keydown.space)' : 'closeEdit()' ,
165
+ } ,
166
+ } )
182
167
export class CdkEditClose < FormValue > {
183
168
constructor (
184
169
protected readonly elementRef : ElementRef < HTMLElement > ,
@@ -192,15 +177,6 @@ export class CdkEditClose<FormValue> {
192
177
}
193
178
}
194
179
195
- // In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
196
- // to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
197
- // can move this back into `host`.
198
- // tslint:disable-next-line:no-host-decorator-in-concrete
199
- @HostListener ( 'click' )
200
- // tslint:disable-next-line:no-host-decorator-in-concrete
201
- @HostListener ( 'keydown.enter' )
202
- // tslint:disable-next-line:no-host-decorator-in-concrete
203
- @HostListener ( 'keydown.space' )
204
180
closeEdit ( ) : void {
205
181
// Note that we use `click` here, rather than a keyboard event, because some screen readers
206
182
// will emit a fake click event instead of an enter keyboard event on buttons. For the keyboard
0 commit comments