Skip to content

Commit 134cbf3

Browse files
authored
fix(chips): support readonly collections in inputs (#20292)
1 parent 8fb560f commit 134cbf3

File tree

6 files changed

+15
-25
lines changed

6 files changed

+15
-25
lines changed

src/material-experimental/mdc-chips/chip-default-options.ts

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

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

src/material-experimental/mdc-chips/chip-input.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88

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

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

7980
/** Emitted when a chip is to be added. */
8081
@Output('matChipInputTokenEnd')
@@ -163,13 +164,7 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
163164

164165
/** Checks whether a keycode is one of the configured separators. */
165166
private _isSeparatorKey(event: KeyboardEvent) {
166-
if (hasModifierKey(event)) {
167-
return false;
168-
}
169-
170-
const separators = this.separatorKeyCodes;
171-
const keyCode = event.keyCode;
172-
return Array.isArray(separators) ? separators.indexOf(keyCode) > -1 : separators.has(keyCode);
167+
return !hasModifierKey(event) && new Set(this.separatorKeyCodes).has(event.keyCode);
173168
}
174169

175170
static ngAcceptInputType_addOnBlur: BooleanInput;

src/material-experimental/mdc-chips/chip.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
137137
/** Emits when the chip is blurred. */
138138
readonly _onBlur = new Subject<MatChipEvent>();
139139

140-
readonly REMOVE_ICON_HANDLED_KEYS: Set<number> = new Set([SPACE, ENTER]);
140+
readonly REMOVE_ICON_HANDLED_KEYS: ReadonlySet<number> = new Set([SPACE, ENTER]);
141141

142142
/** Whether this chip is a basic (unstyled) chip. */
143143
readonly _isBasicChip: boolean;

src/material/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[] | Set<number>;
14+
separatorKeyCodes: readonly number[] | ReadonlySet<number>;
1515
}
1616

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

src/material/chips/chip-input.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88

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

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

7980
/** Emitted when a chip is to be added. */
8081
@Output('matChipInputTokenEnd')
@@ -163,13 +164,7 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
163164

164165
/** Checks whether a keycode is one of the configured separators. */
165166
private _isSeparatorKey(event: KeyboardEvent) {
166-
if (hasModifierKey(event)) {
167-
return false;
168-
}
169-
170-
const separators = this.separatorKeyCodes;
171-
const keyCode = event.keyCode;
172-
return Array.isArray(separators) ? separators.indexOf(keyCode) > -1 : separators.has(keyCode);
167+
return !hasModifierKey(event) && new Set(this.separatorKeyCodes).has(event.keyCode);
173168
}
174169

175170
static ngAcceptInputType_addOnBlur: BooleanInput;

tools/public_api_guard/material/chips.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export declare class MatChipInput implements MatChipTextControl, OnChanges {
8585
focused: boolean;
8686
id: string;
8787
placeholder: string;
88-
separatorKeyCodes: number[] | Set<number>;
88+
separatorKeyCodes: readonly number[] | ReadonlySet<number>;
8989
constructor(_elementRef: ElementRef<HTMLInputElement>, _defaultOptions: MatChipsDefaultOptions);
9090
_blur(): void;
9191
_emitChipEnd(event?: KeyboardEvent): void;
@@ -200,7 +200,7 @@ export declare class MatChipRemove {
200200
}
201201

202202
export interface MatChipsDefaultOptions {
203-
separatorKeyCodes: number[] | Set<number>;
203+
separatorKeyCodes: readonly number[] | ReadonlySet<number>;
204204
}
205205

206206
export declare class MatChipSelectionChange {

0 commit comments

Comments
 (0)