forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist-option.ts
343 lines (304 loc) · 11.7 KB
/
list-option.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* @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 {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
import {SelectionModel} from '@angular/cdk/collections';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChildren,
ElementRef,
EventEmitter,
InjectionToken,
Input,
OnDestroy,
OnInit,
Output,
QueryList,
ViewChild,
ViewEncapsulation,
inject,
} from '@angular/core';
import {ThemePalette} from '../core';
import {MatListBase, MatListItemBase} from './list-base';
import {LIST_OPTION, ListOption, MatListOptionTogglePosition} from './list-option-types';
import {MatListItemLine, MatListItemTitle} from './list-item-sections';
import {NgTemplateOutlet} from '@angular/common';
import {CdkObserveContent} from '@angular/cdk/observers';
/**
* Injection token that can be used to reference instances of an `SelectionList`. It serves
* as alternative token to an actual implementation which would result in circular references.
* @docs-private
*/
export const SELECTION_LIST = new InjectionToken<SelectionList>('SelectionList');
/**
* Interface describing the containing list of a list option. This is used to avoid
* circular dependencies between the list-option and the selection list.
* @docs-private
*/
export interface SelectionList extends MatListBase {
multiple: boolean;
color: ThemePalette;
selectedOptions: SelectionModel<MatListOption>;
hideSingleSelectionIndicator: boolean;
compareWith: (o1: any, o2: any) => boolean;
_value: string[] | null;
_reportValueChange(): void;
_emitChangeEvent(options: MatListOption[]): void;
_onTouched(): void;
}
@Component({
selector: 'mat-list-option',
exportAs: 'matListOption',
styleUrl: 'list-option.css',
host: {
'class': 'mat-mdc-list-item mat-mdc-list-option mdc-list-item',
'role': 'option',
// As per MDC, only list items without checkbox or radio indicator should receive the
// `--selected` class.
'[class.mdc-list-item--selected]':
'selected && !_selectionList.multiple && _selectionList.hideSingleSelectionIndicator',
// Based on the checkbox/radio position and whether there are icons or avatars, we apply MDC's
// list-item `--leading` and `--trailing` classes.
'[class.mdc-list-item--with-leading-avatar]': '_hasProjected("avatars", "before")',
'[class.mdc-list-item--with-leading-icon]': '_hasProjected("icons", "before")',
'[class.mdc-list-item--with-trailing-icon]': '_hasProjected("icons", "after")',
'[class.mat-mdc-list-option-with-trailing-avatar]': '_hasProjected("avatars", "after")',
// Based on the checkbox/radio position, we apply the `--leading` or `--trailing` MDC classes
// which ensure that the checkbox/radio is positioned correctly within the list item.
'[class.mdc-list-item--with-leading-checkbox]': '_hasCheckboxAt("before")',
'[class.mdc-list-item--with-trailing-checkbox]': '_hasCheckboxAt("after")',
'[class.mdc-list-item--with-leading-radio]': '_hasRadioAt("before")',
'[class.mdc-list-item--with-trailing-radio]': '_hasRadioAt("after")',
// Utility class that makes it easier to target the case where there's both a leading
// and a trailing icon. Avoids having to write out all the combinations.
'[class.mat-mdc-list-item-both-leading-and-trailing]': '_hasBothLeadingAndTrailing()',
'[class.mat-accent]': 'color !== "primary" && color !== "warn"',
'[class.mat-warn]': 'color === "warn"',
'[class._mat-animation-noopable]': '_noopAnimations',
'[attr.aria-selected]': 'selected',
'(blur)': '_handleBlur()',
'(click)': '_toggleOnInteraction()',
},
templateUrl: 'list-option.html',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{provide: MatListItemBase, useExisting: MatListOption},
{provide: LIST_OPTION, useExisting: MatListOption},
],
imports: [NgTemplateOutlet, CdkObserveContent],
})
export class MatListOption extends MatListItemBase implements ListOption, OnInit, OnDestroy {
protected _selectionList = inject<SelectionList>(SELECTION_LIST);
private _changeDetectorRef = inject(ChangeDetectorRef);
@ContentChildren(MatListItemLine, {descendants: true}) _lines: QueryList<MatListItemLine>;
@ContentChildren(MatListItemTitle, {descendants: true}) _titles: QueryList<MatListItemTitle>;
@ViewChild('unscopedContent') _unscopedContent: ElementRef<HTMLSpanElement>;
/**
* Emits when the selected state of the option has changed.
* Use to facilitate two-data binding to the `selected` property.
* @docs-private
*/
@Output()
readonly selectedChange: EventEmitter<boolean> = new EventEmitter<boolean>();
/** Whether the label should appear before or after the checkbox/radio. Defaults to 'after' */
@Input() togglePosition: MatListOptionTogglePosition = 'after';
/**
* Whether the label should appear before or after the checkbox/radio. Defaults to 'after'
*
* @deprecated Use `togglePosition` instead.
* @breaking-change 17.0.0
*/
@Input() get checkboxPosition(): MatListOptionTogglePosition {
return this.togglePosition;
}
set checkboxPosition(value: MatListOptionTogglePosition) {
this.togglePosition = value;
}
/**
* Theme color of the list option. This sets the color of the checkbox/radio.
* This API is supported in M2 themes only, it has no effect in M3 themes. For color customization
* in M3, see https://material.angular.io/components/list/styling.
*
* For information on applying color variants in M3, see
* https://material.angular.io/guide/material-2-theming#optional-add-backwards-compatibility-styles-for-color-variants
*/
@Input()
get color(): ThemePalette {
return this._color || this._selectionList.color;
}
set color(newValue: ThemePalette) {
this._color = newValue;
}
private _color: ThemePalette;
/** Value of the option */
@Input()
get value(): any {
return this._value;
}
set value(newValue: any) {
if (this.selected && newValue !== this.value && this._inputsInitialized) {
this.selected = false;
}
this._value = newValue;
}
private _value: any;
/** Whether the option is selected. */
@Input()
get selected(): boolean {
return this._selectionList.selectedOptions.isSelected(this);
}
set selected(value: BooleanInput) {
const isSelected = coerceBooleanProperty(value);
if (isSelected !== this._selected) {
this._setSelected(isSelected);
if (isSelected || this._selectionList.multiple) {
this._selectionList._reportValueChange();
}
}
}
private _selected = false;
/**
* This is set to true after the first OnChanges cycle so we don't
* clear the value of `selected` in the first cycle.
*/
private _inputsInitialized = false;
ngOnInit() {
const list = this._selectionList;
if (list._value && list._value.some(value => list.compareWith(this._value, value))) {
this._setSelected(true);
}
const wasSelected = this._selected;
// List options that are selected at initialization can't be reported properly to the form
// control. This is because it takes some time until the selection-list knows about all
// available options. Also it can happen that the ControlValueAccessor has an initial value
// that should be used instead. Deferring the value change report to the next tick ensures
// that the form control value is not being overwritten.
Promise.resolve().then(() => {
if (this._selected || wasSelected) {
this.selected = true;
this._changeDetectorRef.markForCheck();
}
});
this._inputsInitialized = true;
}
override ngOnDestroy(): void {
super.ngOnDestroy();
if (this.selected) {
// We have to delay this until the next tick in order
// to avoid changed after checked errors.
Promise.resolve().then(() => {
this.selected = false;
});
}
}
/** Toggles the selection state of the option. */
toggle(): void {
this.selected = !this.selected;
}
/** Allows for programmatic focusing of the option. */
focus(): void {
this._hostElement.focus();
}
/** Gets the text label of the list option. Used for the typeahead functionality in the list. */
getLabel() {
const titleElement = this._titles?.get(0)?._elementRef.nativeElement;
// If there is no explicit title element, the unscoped text content
// is treated as the list item title.
const labelEl = titleElement || this._unscopedContent?.nativeElement;
return labelEl?.textContent || '';
}
/** Whether a checkbox is shown at the given position. */
_hasCheckboxAt(position: MatListOptionTogglePosition): boolean {
return this._selectionList.multiple && this._getTogglePosition() === position;
}
/** Where a radio indicator is shown at the given position. */
_hasRadioAt(position: MatListOptionTogglePosition): boolean {
return (
!this._selectionList.multiple &&
this._getTogglePosition() === position &&
!this._selectionList.hideSingleSelectionIndicator
);
}
/** Whether icons or avatars are shown at the given position. */
_hasIconsOrAvatarsAt(position: 'before' | 'after'): boolean {
return this._hasProjected('icons', position) || this._hasProjected('avatars', position);
}
/** Gets whether the given type of element is projected at the specified position. */
_hasProjected(type: 'icons' | 'avatars', position: 'before' | 'after'): boolean {
// If the checkbox/radio is shown at the specified position, neither icons or
// avatars can be shown at the position.
return (
this._getTogglePosition() !== position &&
(type === 'avatars' ? this._avatars.length !== 0 : this._icons.length !== 0)
);
}
_handleBlur() {
this._selectionList._onTouched();
}
/** Gets the current position of the checkbox/radio. */
_getTogglePosition() {
return this.togglePosition || 'after';
}
/**
* Sets the selected state of the option.
* @returns Whether the value has changed.
*/
_setSelected(selected: boolean): boolean {
if (selected === this._selected) {
return false;
}
this._selected = selected;
if (selected) {
this._selectionList.selectedOptions.select(this);
} else {
this._selectionList.selectedOptions.deselect(this);
}
this.selectedChange.emit(selected);
this._changeDetectorRef.markForCheck();
return true;
}
/**
* Notifies Angular that the option needs to be checked in the next change detection run.
* Mainly used to trigger an update of the list option if the disabled state of the selection
* list changed.
*/
_markForCheck() {
this._changeDetectorRef.markForCheck();
}
/** Toggles the option's value based on a user interaction. */
_toggleOnInteraction() {
if (!this.disabled) {
if (this._selectionList.multiple) {
this.selected = !this.selected;
this._selectionList._emitChangeEvent([this]);
} else if (!this.selected) {
this.selected = true;
this._selectionList._emitChangeEvent([this]);
}
}
}
/** Sets the tabindex of the list option. */
_setTabindex(value: number) {
this._hostElement.setAttribute('tabindex', value + '');
}
protected _hasBothLeadingAndTrailing(): boolean {
const hasLeading =
this._hasProjected('avatars', 'before') ||
this._hasProjected('icons', 'before') ||
this._hasCheckboxAt('before') ||
this._hasRadioAt('before');
const hasTrailing =
this._hasProjected('icons', 'after') ||
this._hasProjected('avatars', 'after') ||
this._hasCheckboxAt('after') ||
this._hasRadioAt('after');
return hasLeading && hasTrailing;
}
}