8
8
9
9
import { BACKSPACE , hasModifierKey } from '@angular/cdk/keycodes' ;
10
10
import {
11
- AfterContentInit ,
12
11
Directive ,
13
12
ElementRef ,
14
13
EventEmitter ,
@@ -57,7 +56,6 @@ let nextUniqueId = 0;
57
56
// the MDC chips were landed initially with it.
58
57
'class' : 'mat-mdc-chip-input mat-mdc-input-element mdc-text-field__input mat-input-element' ,
59
58
'(keydown)' : '_keydown($event)' ,
60
- '(keyup)' : '_keyup($event)' ,
61
59
'(blur)' : '_blur()' ,
62
60
'(focus)' : '_focus()' ,
63
61
'(input)' : '_onInput()' ,
@@ -70,10 +68,7 @@ let nextUniqueId = 0;
70
68
} ,
71
69
standalone : true ,
72
70
} )
73
- export class MatChipInput implements MatChipTextControl , AfterContentInit , OnChanges , OnDestroy {
74
- /** Used to prevent focus moving to chips while user is holding backspace */
75
- private _focusLastChipOnBackspace : boolean ;
76
-
71
+ export class MatChipInput implements MatChipTextControl , OnChanges , OnDestroy {
77
72
/** Whether the control is focused. */
78
73
focused : boolean = false ;
79
74
@@ -153,36 +148,17 @@ export class MatChipInput implements MatChipTextControl, AfterContentInit, OnCha
153
148
this . chipEnd . complete ( ) ;
154
149
}
155
150
156
- ngAfterContentInit ( ) : void {
157
- this . _focusLastChipOnBackspace = this . empty ;
158
- }
159
-
160
151
/** Utility method to make host definition/tests more clear. */
161
- _keydown ( event ?: KeyboardEvent ) {
162
- if ( event ) {
163
- // To prevent the user from accidentally deleting chips when pressing BACKSPACE continuously,
164
- // We focus the last chip on backspace only after the user has released the backspace button,
165
- // And the input is empty (see behaviour in _keyup)
166
- if ( event . keyCode === BACKSPACE && this . _focusLastChipOnBackspace ) {
152
+ _keydown ( event : KeyboardEvent ) {
153
+ if ( this . empty && event . keyCode === BACKSPACE ) {
154
+ // Ignore events where the user is holding down backspace
155
+ // so that we don't accidentally remove too many chips.
156
+ if ( ! event . repeat ) {
167
157
this . _chipGrid . _focusLastChip ( ) ;
168
- event . preventDefault ( ) ;
169
- return ;
170
- } else {
171
- this . _focusLastChipOnBackspace = false ;
172
158
}
173
- }
174
-
175
- this . _emitChipEnd ( event ) ;
176
- }
177
-
178
- /**
179
- * Pass events to the keyboard manager. Available here for tests.
180
- */
181
- _keyup ( event : KeyboardEvent ) {
182
- // Allow user to move focus to chips next time he presses backspace
183
- if ( ! this . _focusLastChipOnBackspace && event . keyCode === BACKSPACE && this . empty ) {
184
- this . _focusLastChipOnBackspace = true ;
185
159
event . preventDefault ( ) ;
160
+ } else {
161
+ this . _emitChipEnd ( event ) ;
186
162
}
187
163
}
188
164
@@ -201,7 +177,6 @@ export class MatChipInput implements MatChipTextControl, AfterContentInit, OnCha
201
177
202
178
_focus ( ) {
203
179
this . focused = true ;
204
- this . _focusLastChipOnBackspace = this . empty ;
205
180
this . _chipGrid . stateChanges . next ( ) ;
206
181
}
207
182
@@ -231,7 +206,6 @@ export class MatChipInput implements MatChipTextControl, AfterContentInit, OnCha
231
206
/** Clears the input */
232
207
clear ( ) : void {
233
208
this . inputElement . value = '' ;
234
- this . _focusLastChipOnBackspace = true ;
235
209
}
236
210
237
211
setDescribedByIds ( ids : string [ ] ) : void {
0 commit comments