Skip to content

Commit d1cec1f

Browse files
crisbetovivian-hu-zz
authored andcommittedDec 12, 2018
fix(chips): don't handle separator keys while pressing modifiers (#14424)
No longer reacts to pressing the separator keys, if a modifier key is being held down. This avoids interfering with other OS-level shortcuts.
1 parent 058f5ef commit d1cec1f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
 

‎src/lib/chips/chip-input.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ describe('MatChipInput', () => {
186186
expect(testChipInput.add).toHaveBeenCalled();
187187
});
188188

189+
it('should not emit the chipEnd event if a separator is pressed with a modifier key', () => {
190+
const ENTER_EVENT = createKeyboardEvent('keydown', ENTER, inputNativeElement);
191+
Object.defineProperty(ENTER_EVENT, 'shiftKey', {get: () => true});
192+
spyOn(testChipInput, 'add');
193+
194+
chipInputDirective.separatorKeyCodes = [ENTER];
195+
fixture.detectChanges();
196+
197+
chipInputDirective._keydown(ENTER_EVENT);
198+
expect(testChipInput.add).not.toHaveBeenCalled();
199+
});
200+
189201
});
190202
});
191203

‎src/lib/chips/chip-input.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1010
import {Directive, ElementRef, EventEmitter, Inject, Input, OnChanges, Output} from '@angular/core';
11+
import {hasModifierKey} from '@angular/cdk/keycodes';
1112
import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './chip-default-options';
1213
import {MatChipList} from './chip-list';
1314
import {MatChipTextControl} from './chip-text-control';
@@ -134,7 +135,7 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
134135
if (!this._inputElement.value && !!event) {
135136
this._chipList._keydown(event);
136137
}
137-
if (!event || this._isSeparatorKey(event.keyCode)) {
138+
if (!event || this._isSeparatorKey(event)) {
138139
this.chipEnd.emit({ input: this._inputElement, value: this._inputElement.value });
139140

140141
if (event) {
@@ -154,8 +155,13 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
154155
}
155156

156157
/** Checks whether a keycode is one of the configured separators. */
157-
private _isSeparatorKey(keyCode: number) {
158+
private _isSeparatorKey(event: KeyboardEvent) {
159+
if (hasModifierKey(event)) {
160+
return false;
161+
}
162+
158163
const separators = this.separatorKeyCodes;
164+
const keyCode = event.keyCode;
159165
return Array.isArray(separators) ? separators.indexOf(keyCode) > -1 : separators.has(keyCode);
160166
}
161167
}

0 commit comments

Comments
 (0)
Please sign in to comment.