/
chip-option.ts
188 lines (169 loc) · 6.39 KB
/
chip-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
/**
* @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.io/license
*/
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
Input,
Output,
ViewEncapsulation,
OnInit,
} from '@angular/core';
import {MatChip} from './chip';
import {MAT_CHIP} from './tokens';
/** Event object emitted by MatChipOption when selected or deselected. */
export class MatChipSelectionChange {
constructor(
/** Reference to the chip that emitted the event. */
public source: MatChipOption,
/** Whether the chip that emitted the event is selected. */
public selected: boolean,
/** Whether the selection change was a result of a user interaction. */
public isUserInput = false,
) {}
}
/**
* An extension of the MatChip component that supports chip selection. Used with MatChipListbox.
*
* Unlike other chips, the user can focus on disabled chip options inside a MatChipListbox. The
* user cannot click disabled chips.
*/
@Component({
selector: 'mat-basic-chip-option, mat-chip-option',
templateUrl: 'chip-option.html',
styleUrls: ['chip.css'],
inputs: ['color', 'disabled', 'disableRipple', 'tabIndex'],
host: {
'class': 'mat-mdc-chip mat-mdc-chip-option mdc-evolution-chip mdc-evolution-chip--filter',
'[class.mat-mdc-chip-selected]': 'selected',
'[class.mat-mdc-chip-multiple]': '_chipListMultiple',
'[class.mat-mdc-chip-disabled]': 'disabled',
'[class.mat-mdc-chip-with-avatar]': 'leadingIcon',
'[class.mdc-evolution-chip--selectable]': 'selectable',
'[class.mdc-evolution-chip--disabled]': 'disabled',
'[class.mdc-evolution-chip--selected]': 'selected',
// This class enables the transition on the checkmark. Usually MDC adds it when selection
// starts and removes it once the animation is finished. We don't need to go through all
// the trouble, because we only care about the selection animation. MDC needs to do it,
// because they also have an exit animation that we don't care about.
'[class.mdc-evolution-chip--selecting]': '!_animationsDisabled',
'[class.mdc-evolution-chip--with-trailing-action]': '_hasTrailingIcon()',
'[class.mdc-evolution-chip--with-primary-graphic]': '_hasLeadingGraphic()',
'[class.mdc-evolution-chip--with-primary-icon]': 'leadingIcon',
'[class.mdc-evolution-chip--with-avatar]': 'leadingIcon',
'[class.mat-mdc-chip-highlighted]': 'highlighted',
'[class.mat-mdc-chip-with-trailing-icon]': '_hasTrailingIcon()',
'[attr.tabindex]': 'null',
'[attr.aria-label]': 'null',
'[attr.role]': 'role',
'[id]': 'id',
},
providers: [
{provide: MatChip, useExisting: MatChipOption},
{provide: MAT_CHIP, useExisting: MatChipOption},
],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatChipOption extends MatChip implements OnInit {
/** Whether the chip list is selectable. */
chipListSelectable: boolean = true;
/** Whether the chip list is in multi-selection mode. */
_chipListMultiple: boolean = false;
/**
* Whether or not the chip is selectable.
*
* When a chip is not selectable, changes to its selected state are always
* ignored. By default an option chip is selectable, and it becomes
* non-selectable if its parent chip list is not selectable.
*/
@Input()
get selectable(): boolean {
return this._selectable && this.chipListSelectable;
}
set selectable(value: BooleanInput) {
this._selectable = coerceBooleanProperty(value);
this._changeDetectorRef.markForCheck();
}
protected _selectable: boolean = true;
/** Whether the chip is selected. */
@Input()
get selected(): boolean {
return this._selected;
}
set selected(value: BooleanInput) {
this._setSelectedState(coerceBooleanProperty(value), false, true);
}
private _selected = false;
/**
* The ARIA selected applied to the chip. Conforms to WAI ARIA best practices for listbox
* interaction patterns.
*
* From [WAI ARIA Listbox authoring practices guide](
* https://www.w3.org/WAI/ARIA/apg/patterns/listbox/):
* "If any options are selected, each selected option has either aria-selected or aria-checked
* set to true. All options that are selectable but not selected have either aria-selected or
* aria-checked set to false."
*
* Set `aria-selected="false"` on not-selected listbox options that are selectable to fix
* VoiceOver reading every option as "selected" (#25736).
*/
get ariaSelected(): string | null {
return this.selectable ? this.selected.toString() : null;
}
/** The unstyled chip selector for this component. */
protected override basicChipAttrName = 'mat-basic-chip-option';
/** Emitted when the chip is selected or deselected. */
@Output() readonly selectionChange: EventEmitter<MatChipSelectionChange> =
new EventEmitter<MatChipSelectionChange>();
ngOnInit() {
this.role = 'presentation';
}
/** Selects the chip. */
select(): void {
this._setSelectedState(true, false, true);
}
/** Deselects the chip. */
deselect(): void {
this._setSelectedState(false, false, true);
}
/** Selects this chip and emits userInputSelection event */
selectViaInteraction(): void {
this._setSelectedState(true, true, true);
}
/** Toggles the current selected state of this chip. */
toggleSelected(isUserInput: boolean = false): boolean {
this._setSelectedState(!this.selected, isUserInput, true);
return this.selected;
}
override _handlePrimaryActionInteraction() {
if (this.selectable && !this.disabled) {
this.toggleSelected(true);
}
}
_hasLeadingGraphic() {
// The checkmark graphic communicates selected state for both single-select and multi-select.
// Include checkmark in single-select to fix a11y issue where selected state is communicated
// visually only using color (#25886).
return this.leadingIcon || this.selectable;
}
_setSelectedState(isSelected: boolean, isUserInput: boolean, emitEvent: boolean) {
if (isSelected !== this.selected) {
this._selected = isSelected;
if (emitEvent) {
this.selectionChange.emit({
source: this,
isUserInput,
selected: this.selected,
});
}
this._changeDetectorRef.markForCheck();
}
}
}