File tree 3 files changed +23
-5
lines changed
3 files changed +23
-5
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import {InjectionToken} from '@angular/core';
11
11
/** Default options, for the chips module, that can be overridden. */
12
12
export interface MatChipsDefaultOptions {
13
13
/** The list of key codes that will trigger a chipEnd event. */
14
- separatorKeyCodes : number [ ] ;
14
+ separatorKeyCodes : number [ ] | Set < number > ;
15
15
}
16
16
17
17
/** Injection token to be used to override the default options for the chips module. */
Original file line number Diff line number Diff line change @@ -129,6 +129,17 @@ describe('MatChipInput', () => {
129
129
expect ( testChipInput . add ) . toHaveBeenCalled ( ) ;
130
130
} ) ;
131
131
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
+
132
143
it ( 'emits (chipEnd) when the separator keys are configured globally' , ( ) => {
133
144
fixture . destroy ( ) ;
134
145
Original file line number Diff line number Diff line change @@ -68,9 +68,8 @@ export class MatChipInput implements OnChanges {
68
68
*
69
69
* Defaults to `[ENTER]`.
70
70
*/
71
- // TODO(tinayuangao): Support Set here
72
71
@Input ( 'matChipInputSeparatorKeyCodes' )
73
- separatorKeyCodes : number [ ] = this . _defaultOptions . separatorKeyCodes ;
72
+ separatorKeyCodes : number [ ] | Set < number > = this . _defaultOptions . separatorKeyCodes ;
74
73
75
74
/** Emitted when a chip is to be added. */
76
75
@Output ( 'matChipInputTokenEnd' )
@@ -126,7 +125,7 @@ export class MatChipInput implements OnChanges {
126
125
if ( ! this . _inputElement . value && ! ! event ) {
127
126
this . _chipList . _keydown ( event ) ;
128
127
}
129
- if ( ! event || this . separatorKeyCodes . indexOf ( event . keyCode ) > - 1 ) {
128
+ if ( ! event || this . _isSeparatorKey ( event . keyCode ) ) {
130
129
this . chipEnd . emit ( { input : this . _inputElement , value : this . _inputElement . value } ) ;
131
130
132
131
if ( event ) {
@@ -141,5 +140,13 @@ export class MatChipInput implements OnChanges {
141
140
}
142
141
143
142
/** 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
+ }
145
152
}
You can’t perform that action at this time.
0 commit comments