Skip to content

Commit

Permalink
fix(chips): support readonly collections in inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 committed Aug 13, 2020
1 parent cb8de61 commit 02faa18
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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[] | Set<number>;
separatorKeyCodes: readonly number[] | ReadonlySet<number>;
}

/** Injection token to be used to override the default options for the chips module. */
Expand Down
11 changes: 5 additions & 6 deletions src/material-experimental/mdc-chips/chip-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
import {Directive, ElementRef, EventEmitter, Inject, Input, OnChanges, Output} from '@angular/core';
import {hasModifierKey, TAB} from '@angular/cdk/keycodes';
import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './chip-default-options';
import {Directive, ElementRef, EventEmitter, Inject, Input, OnChanges, Output} from '@angular/core';
import {MatChipsDefaultOptions, MAT_CHIPS_DEFAULT_OPTIONS} from './chip-default-options';
import {MatChipGrid} from './chip-grid';
import {MatChipTextControl} from './chip-text-control';

Expand Down Expand Up @@ -74,7 +74,8 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
* Defaults to `[ENTER]`.
*/
@Input('matChipInputSeparatorKeyCodes')
separatorKeyCodes: number[] | Set<number> = this._defaultOptions.separatorKeyCodes;
separatorKeyCodes: readonly number[] | ReadonlySet<number> =
this._defaultOptions.separatorKeyCodes;

/** Emitted when a chip is to be added. */
@Output('matChipInputTokenEnd')
Expand Down Expand Up @@ -167,9 +168,7 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
return false;
}

const separators = this.separatorKeyCodes;
const keyCode = event.keyCode;
return Array.isArray(separators) ? separators.indexOf(keyCode) > -1 : separators.has(keyCode);
return new Set(this.separatorKeyCodes).has(event.keyCode);
}

static ngAcceptInputType_addOnBlur: BooleanInput;
Expand Down
2 changes: 1 addition & 1 deletion src/material/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[] | Set<number>;
separatorKeyCodes: readonly number[] | ReadonlySet<number>;
}

/** Injection token to be used to override the default options for the chips module. */
Expand Down
11 changes: 5 additions & 6 deletions src/material/chips/chip-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
import {Directive, ElementRef, EventEmitter, Inject, Input, OnChanges, Output} from '@angular/core';
import {hasModifierKey, TAB} from '@angular/cdk/keycodes';
import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './chip-default-options';
import {Directive, ElementRef, EventEmitter, Inject, Input, OnChanges, Output} from '@angular/core';
import {MatChipsDefaultOptions, MAT_CHIPS_DEFAULT_OPTIONS} from './chip-default-options';
import {MatChipList} from './chip-list';
import {MatChipTextControl} from './chip-text-control';

Expand Down Expand Up @@ -74,7 +74,8 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
* Defaults to `[ENTER]`.
*/
@Input('matChipInputSeparatorKeyCodes')
separatorKeyCodes: number[] | Set<number> = this._defaultOptions.separatorKeyCodes;
separatorKeyCodes: readonly number[] | ReadonlySet<number> =
this._defaultOptions.separatorKeyCodes;

/** Emitted when a chip is to be added. */
@Output('matChipInputTokenEnd')
Expand Down Expand Up @@ -167,9 +168,7 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
return false;
}

const separators = this.separatorKeyCodes;
const keyCode = event.keyCode;
return Array.isArray(separators) ? separators.indexOf(keyCode) > -1 : separators.has(keyCode);
return new Set(this.separatorKeyCodes).has(event.keyCode);
}

static ngAcceptInputType_addOnBlur: BooleanInput;
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/material/chips.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export declare class MatChipInput implements MatChipTextControl, OnChanges {
focused: boolean;
id: string;
placeholder: string;
separatorKeyCodes: number[] | Set<number>;
separatorKeyCodes: readonly number[] | ReadonlySet<number>;
constructor(_elementRef: ElementRef<HTMLInputElement>, _defaultOptions: MatChipsDefaultOptions);
_blur(): void;
_emitChipEnd(event?: KeyboardEvent): void;
Expand Down Expand Up @@ -200,7 +200,7 @@ export declare class MatChipRemove {
}

export interface MatChipsDefaultOptions {
separatorKeyCodes: number[] | Set<number>;
separatorKeyCodes: readonly number[] | ReadonlySet<number>;
}

export declare class MatChipSelectionChange {
Expand Down

0 comments on commit 02faa18

Please sign in to comment.