Skip to content

Commit d2d990d

Browse files
committed
refactor(material-experimental/mdc-chips): implement trailing icon foundation
The new foundation isn't being used yet, but these changes implement it so it's easier to roll out in the future.
1 parent 7c75d6e commit d2d990d

File tree

4 files changed

+575
-523
lines changed

4 files changed

+575
-523
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@types/youtube": "^0.0.38",
5757
"@webcomponents/custom-elements": "^1.1.0",
5858
"core-js": "^2.6.9",
59-
"material-components-web": "6.0.0-canary.d5808057f.0",
59+
"material-components-web": "6.0.0-canary.35a32aaea.0",
6060
"rxjs": "^6.5.3",
6161
"systemjs": "0.19.43",
6262
"tslib": "^1.10.0",

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ChangeDetectorRef,
1212
Directive,
1313
ElementRef,
14+
OnDestroy,
1415
} from '@angular/core';
1516
import {
1617
CanDisable,
@@ -20,6 +21,7 @@ import {
2021
mixinDisabled,
2122
mixinTabIndex,
2223
} from '@angular/material/core';
24+
import {MDCChipTrailingActionAdapter, MDCChipTrailingActionFoundation} from '@material/chips';
2325
import {Subject} from 'rxjs';
2426

2527

@@ -57,8 +59,40 @@ export class MatChipAvatar {
5759
'aria-hidden': 'true',
5860
}
5961
})
60-
export class MatChipTrailingIcon {
61-
constructor(public _elementRef: ElementRef) {}
62+
export class MatChipTrailingIcon implements OnDestroy {
63+
private _foundation: MDCChipTrailingActionFoundation;
64+
private _adapter: MDCChipTrailingActionAdapter = {
65+
focus: () => this._elementRef.nativeElement.focus(),
66+
getAttribute: (name: string) => this._elementRef.nativeElement.getAttribute(name),
67+
setAttribute: (name: string, value: string) => {
68+
this._elementRef.nativeElement.setAttribute(name, value);
69+
},
70+
// TODO(crisbeto): this also a `trigger` parameter that the chip isn't
71+
// handling yet. Consider passing it along once MDC start using it.
72+
notifyInteraction: () => {
73+
// this._chip._notifyInteraction();
74+
},
75+
76+
// TODO(crisbeto): this also a `key` parameter that the chip isn't
77+
// handling yet. Consider passing it along once MDC start using it.
78+
notifyNavigation: () => {
79+
// this._chip._notifyNavigation();
80+
}
81+
};
82+
83+
constructor(
84+
public _elementRef: ElementRef,
85+
// TODO(crisbeto): currently the chip needs a reference to the trailing icon for the deprecated
86+
// `setTrailingActionAttr` method. Until the method is removed, we can't use the chip here,
87+
// because it causes a circular import.
88+
// private _chip: MatChip
89+
) {
90+
this._foundation = new MDCChipTrailingActionFoundation(this._adapter);
91+
}
92+
93+
ngOnDestroy() {
94+
this._foundation.destroy();
95+
}
6296

6397
focus() {
6498
this._elementRef.nativeElement.focus();
@@ -75,8 +109,8 @@ export class MatChipTrailingIcon {
75109
* @docs-private
76110
*/
77111
class MatChipRemoveBase extends MatChipTrailingIcon {
78-
constructor(_elementRef: ElementRef) {
79-
super(_elementRef);
112+
constructor(elementRef: ElementRef) {
113+
super(elementRef);
80114
}
81115
}
82116

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,12 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
250250
return (target && (target as Element).classList) ?
251251
(target as Element).classList.contains(className) : false;
252252
},
253-
notifyInteraction: () => this.interaction.emit(this.id),
253+
notifyInteraction: () => this._notifyInteraction(),
254254
notifySelection: () => {
255255
// No-op. We call dispatchSelectionEvent ourselves in MatChipOption, because we want to
256256
// specify whether selection occurred via user input.
257257
},
258-
notifyNavigation: () => {
259-
// TODO: This is a new feature added by MDC; consider exposing this event to users in the
260-
// future.
261-
},
258+
notifyNavigation: () => this._notifyNavigation(),
262259
notifyTrailingIconInteraction: () => this.removeIconInteraction.emit(this.id),
263260
notifyRemoval: () => {
264261
this.removed.emit({ chip: this });
@@ -408,6 +405,14 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
408405
return this.disabled || this.disableRipple || this._isBasicChip();
409406
}
410407

408+
_notifyInteraction() {
409+
this.interaction.emit(this.id);
410+
}
411+
412+
_notifyNavigation() {
413+
// TODO: This is a new feature added by MDC. Consider exposing it to users in the future.
414+
}
415+
411416
static ngAcceptInputType_disabled: BooleanInput;
412417
static ngAcceptInputType_removable: BooleanInput;
413418
static ngAcceptInputType_highlighted: BooleanInput;

0 commit comments

Comments
 (0)