-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathchip-input.ts
238 lines (204 loc) · 6.94 KB
/
chip-input.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {BACKSPACE, hasModifierKey} from '@angular/cdk/keycodes';
import {
Directive,
ElementRef,
EventEmitter,
Input,
OnChanges,
OnDestroy,
Output,
booleanAttribute,
inject,
} from '@angular/core';
import {_IdGenerator} from '@angular/cdk/a11y';
import {MatFormField, MAT_FORM_FIELD} from '@angular/material/form-field';
import {MatChipsDefaultOptions, MAT_CHIPS_DEFAULT_OPTIONS} from './tokens';
import {MatChipGrid} from './chip-grid';
import {MatChipTextControl} from './chip-text-control';
/** Represents an input event on a `matChipInput`. */
export interface MatChipInputEvent {
/**
* The native `<input>` element that the event is being fired for.
* @deprecated Use `MatChipInputEvent#chipInput.inputElement` instead.
* @breaking-change 13.0.0 This property will be removed.
*/
input: HTMLInputElement;
/** The value of the input. */
value: string;
/** Reference to the chip input that emitted the event. */
chipInput: MatChipInput;
}
/**
* Directive that adds chip-specific behaviors to an input element inside `<mat-form-field>`.
* May be placed inside or outside of a `<mat-chip-grid>`.
*/
@Directive({
selector: 'input[matChipInputFor]',
exportAs: 'matChipInput, matChipInputFor',
host: {
// TODO: eventually we should remove `mat-input-element` from here since it comes from the
// non-MDC version of the input. It's currently being kept for backwards compatibility, because
// the MDC chips were landed initially with it.
'class': 'mat-mdc-chip-input mat-mdc-input-element mdc-text-field__input mat-input-element',
'(keydown)': '_keydown($event)',
'(blur)': '_blur()',
'(focus)': '_focus()',
'(input)': '_onInput()',
'[id]': 'id',
'[attr.disabled]': 'disabled || null',
'[attr.placeholder]': 'placeholder || null',
'[attr.aria-invalid]': '_chipGrid && _chipGrid.ngControl ? _chipGrid.ngControl.invalid : null',
'[attr.aria-required]': '_chipGrid && _chipGrid.required || null',
'[attr.required]': '_chipGrid && _chipGrid.required || null',
},
})
export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {
protected _elementRef = inject<ElementRef<HTMLInputElement>>(ElementRef);
/** Whether the control is focused. */
focused: boolean = false;
/** Register input for chip list */
@Input('matChipInputFor')
get chipGrid(): MatChipGrid {
return this._chipGrid;
}
set chipGrid(value: MatChipGrid) {
if (value) {
this._chipGrid = value;
this._chipGrid.registerInput(this);
}
}
private _chipGrid: MatChipGrid;
/**
* Whether or not the chipEnd event will be emitted when the input is blurred.
*/
@Input({alias: 'matChipInputAddOnBlur', transform: booleanAttribute})
addOnBlur: boolean = false;
/**
* The list of key codes that will trigger a chipEnd event.
*
* Defaults to `[ENTER]`.
*/
@Input('matChipInputSeparatorKeyCodes')
separatorKeyCodes: readonly number[] | ReadonlySet<number>;
/** Emitted when a chip is to be added. */
@Output('matChipInputTokenEnd')
readonly chipEnd: EventEmitter<MatChipInputEvent> = new EventEmitter<MatChipInputEvent>();
/** The input's placeholder text. */
@Input() placeholder: string = '';
/** Unique id for the input. */
@Input() id: string = inject(_IdGenerator).getId('mat-mdc-chip-list-input-');
/** Whether the input is disabled. */
@Input({transform: booleanAttribute})
get disabled(): boolean {
return this._disabled || (this._chipGrid && this._chipGrid.disabled);
}
set disabled(value: boolean) {
this._disabled = value;
}
private _disabled: boolean = false;
/** Whether the input is empty. */
get empty(): boolean {
return !this.inputElement.value;
}
/** The native input element to which this directive is attached. */
readonly inputElement!: HTMLInputElement;
constructor(...args: unknown[]);
constructor() {
const defaultOptions = inject<MatChipsDefaultOptions>(MAT_CHIPS_DEFAULT_OPTIONS);
const formField = inject<MatFormField>(MAT_FORM_FIELD, {optional: true});
this.inputElement = this._elementRef.nativeElement as HTMLInputElement;
this.separatorKeyCodes = defaultOptions.separatorKeyCodes;
if (formField) {
this.inputElement.classList.add('mat-mdc-form-field-input-control');
}
}
ngOnChanges() {
this._chipGrid.stateChanges.next();
}
ngOnDestroy(): void {
this.chipEnd.complete();
}
/** Utility method to make host definition/tests more clear. */
_keydown(event: KeyboardEvent) {
if (this.empty && event.keyCode === BACKSPACE) {
// Ignore events where the user is holding down backspace
// so that we don't accidentally remove too many chips.
if (!event.repeat) {
this._chipGrid._focusLastChip();
}
event.preventDefault();
} else {
this._emitChipEnd(event);
}
}
/** Checks to see if the blur should emit the (chipEnd) event. */
_blur() {
if (this.addOnBlur) {
this._emitChipEnd();
}
this.focused = false;
// Blur the chip list if it is not focused
if (!this._chipGrid.focused) {
this._chipGrid._blur();
}
this._chipGrid.stateChanges.next();
}
_focus() {
this.focused = true;
this._chipGrid.stateChanges.next();
}
/** Checks to see if the (chipEnd) event needs to be emitted. */
_emitChipEnd(event?: KeyboardEvent) {
if (!event || (this._isSeparatorKey(event) && !event.repeat)) {
this.chipEnd.emit({
input: this.inputElement,
value: this.inputElement.value,
chipInput: this,
});
event?.preventDefault();
}
}
_onInput() {
// Let chip list know whenever the value changes.
this._chipGrid.stateChanges.next();
}
/** Focuses the input. */
focus(): void {
this.inputElement.focus();
}
/** Clears the input */
clear(): void {
this.inputElement.value = '';
}
setDescribedByIds(ids: string[]): void {
const element = this._elementRef.nativeElement;
// Set the value directly in the DOM since this binding
// is prone to "changed after checked" errors.
if (ids.length) {
element.setAttribute('aria-describedby', ids.join(' '));
} else {
element.removeAttribute('aria-describedby');
}
}
setLabelledByIds(ids: string[]): void {
const element = this._elementRef.nativeElement;
// Set the value directly in the DOM since this binding
// is prone to "changed after checked" errors.
if (ids.length) {
element.setAttribute('aria-labelledby', ids.join(' '));
} else {
element.removeAttribute('aria-labelledby');
}
}
/** Checks whether a keycode is one of the configured separators. */
private _isSeparatorKey(event: KeyboardEvent) {
return !hasModifierKey(event) && new Set(this.separatorKeyCodes).has(event.keyCode);
}
}