Skip to content

Commit 170665a

Browse files
crisbetojelbourn
authored andcommitted
feat(chips): allow set in separatorKeyCodes (#12477)
Resolves a long-standing TODO about supporting a `Set` in a chip input's `separatorKeyCodes`.
1 parent 4a9fe87 commit 170665a

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/lib/chips/chip-default-options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {InjectionToken} from '@angular/core';
1111
/** Default options, for the chips module, that can be overridden. */
1212
export interface MatChipsDefaultOptions {
1313
/** The list of key codes that will trigger a chipEnd event. */
14-
separatorKeyCodes: number[];
14+
separatorKeyCodes: number[] | Set<number>;
1515
}
1616

1717
/** Injection token to be used to override the default options for the chips module. */

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

+11
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ describe('MatChipInput', () => {
129129
expect(testChipInput.add).toHaveBeenCalled();
130130
});
131131

132+
it('emits accepts the custom separator keys in a Set', () => {
133+
let COMMA_EVENT = createKeyboardEvent('keydown', COMMA, inputNativeElement);
134+
spyOn(testChipInput, 'add');
135+
136+
chipInputDirective.separatorKeyCodes = new Set([COMMA]);
137+
fixture.detectChanges();
138+
139+
chipInputDirective._keydown(COMMA_EVENT);
140+
expect(testChipInput.add).toHaveBeenCalled();
141+
});
142+
132143
it('emits (chipEnd) when the separator keys are configured globally', () => {
133144
fixture.destroy();
134145

src/lib/chips/chip-input.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ export class MatChipInput implements OnChanges {
6868
*
6969
* Defaults to `[ENTER]`.
7070
*/
71-
// TODO(tinayuangao): Support Set here
7271
@Input('matChipInputSeparatorKeyCodes')
73-
separatorKeyCodes: number[] = this._defaultOptions.separatorKeyCodes;
72+
separatorKeyCodes: number[] | Set<number> = this._defaultOptions.separatorKeyCodes;
7473

7574
/** Emitted when a chip is to be added. */
7675
@Output('matChipInputTokenEnd')
@@ -126,7 +125,7 @@ export class MatChipInput implements OnChanges {
126125
if (!this._inputElement.value && !!event) {
127126
this._chipList._keydown(event);
128127
}
129-
if (!event || this.separatorKeyCodes.indexOf(event.keyCode) > -1) {
128+
if (!event || this._isSeparatorKey(event.keyCode)) {
130129
this.chipEnd.emit({ input: this._inputElement, value: this._inputElement.value });
131130

132131
if (event) {
@@ -141,5 +140,13 @@ export class MatChipInput implements OnChanges {
141140
}
142141

143142
/** Focuses the input. */
144-
focus(): void { this._inputElement.focus(); }
143+
focus(): void {
144+
this._inputElement.focus();
145+
}
146+
147+
/** Checks whether a keycode is one of the configured separators. */
148+
private _isSeparatorKey(keyCode: number) {
149+
const separators = this.separatorKeyCodes;
150+
return Array.isArray(separators) ? separators.indexOf(keyCode) > -1 : separators.has(keyCode);
151+
}
145152
}

0 commit comments

Comments
 (0)