feat(chips): allow set in separatorKeyCodes (#12477)

Resolves a long-standing TODO about supporting a `Set` in a chip input's `separatorKeyCodes`.
This commit is contained in:
Kristiyan Kostadinov
2018-08-03 16:03:15 +02:00
committed by Jeremy Elbourn
parent 4a9fe87f8a
commit 170665ab16
3 changed files with 23 additions and 5 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ import {InjectionToken} from '@angular/core';
/** Default options, for the chips module, that can be overridden. */
export interface MatChipsDefaultOptions {
/** The list of key codes that will trigger a chipEnd event. */
separatorKeyCodes: number[];
separatorKeyCodes: number[] | Set<number>;
}
/** Injection token to be used to override the default options for the chips module. */
+11
View File
@@ -129,6 +129,17 @@ describe('MatChipInput', () => {
expect(testChipInput.add).toHaveBeenCalled();
});
it('emits accepts the custom separator keys in a Set', () => {
let COMMA_EVENT = createKeyboardEvent('keydown', COMMA, inputNativeElement);
spyOn(testChipInput, 'add');
chipInputDirective.separatorKeyCodes = new Set([COMMA]);
fixture.detectChanges();
chipInputDirective._keydown(COMMA_EVENT);
expect(testChipInput.add).toHaveBeenCalled();
});
it('emits (chipEnd) when the separator keys are configured globally', () => {
fixture.destroy();
+11 -4
View File
@@ -68,9 +68,8 @@ export class MatChipInput implements OnChanges {
*
* Defaults to `[ENTER]`.
*/
// TODO(tinayuangao): Support Set here
@Input('matChipInputSeparatorKeyCodes')
separatorKeyCodes: number[] = this._defaultOptions.separatorKeyCodes;
separatorKeyCodes: number[] | Set<number> = this._defaultOptions.separatorKeyCodes;
/** Emitted when a chip is to be added. */
@Output('matChipInputTokenEnd')
@@ -126,7 +125,7 @@ export class MatChipInput implements OnChanges {
if (!this._inputElement.value && !!event) {
this._chipList._keydown(event);
}
if (!event || this.separatorKeyCodes.indexOf(event.keyCode) > -1) {
if (!event || this._isSeparatorKey(event.keyCode)) {
this.chipEnd.emit({ input: this._inputElement, value: this._inputElement.value });
if (event) {
@@ -141,5 +140,13 @@ export class MatChipInput implements OnChanges {
}
/** Focuses the input. */
focus(): void { this._inputElement.focus(); }
focus(): void {
this._inputElement.focus();
}
/** Checks whether a keycode is one of the configured separators. */
private _isSeparatorKey(keyCode: number) {
const separators = this.separatorKeyCodes;
return Array.isArray(separators) ? separators.indexOf(keyCode) > -1 : separators.has(keyCode);
}
}