Skip to content

feat(chips): allow set in separatorKeyCodes #12477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/chips/chip-default-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
11 changes: 11 additions & 0 deletions src/lib/chips/chip-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
15 changes: 11 additions & 4 deletions src/lib/chips/chip-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}