Skip to content

Commit 3fbe972

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 8a71288 commit 3fbe972

File tree

4 files changed

+577
-523
lines changed

4 files changed

+577
-523
lines changed

package.json

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

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

Lines changed: 40 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,42 @@ 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): there's also a `trigger` parameter that the chip isn't
71+
// handling yet. Consider passing it along once MDC start using it.
72+
notifyInteraction: () => {
73+
// TODO(crisbeto): uncomment this code once we've inverted the dependency on `MatChip`.
74+
// this._chip._notifyInteraction();
75+
},
76+
77+
// TODO(crisbeto): there's also a `key` parameter that the chip isn't
78+
// handling yet. Consider passing it along once MDC start using it.
79+
notifyNavigation: () => {
80+
// TODO(crisbeto): uncomment this code once we've inverted the dependency on `MatChip`.
81+
// this._chip._notifyNavigation();
82+
}
83+
};
84+
85+
constructor(
86+
public _elementRef: ElementRef,
87+
// TODO(crisbeto): currently the chip needs a reference to the trailing icon for the deprecated
88+
// `setTrailingActionAttr` method. Until the method is removed, we can't use the chip here,
89+
// because it causes a circular import.
90+
// private _chip: MatChip
91+
) {
92+
this._foundation = new MDCChipTrailingActionFoundation(this._adapter);
93+
}
94+
95+
ngOnDestroy() {
96+
this._foundation.destroy();
97+
}
6298

6399
focus() {
64100
this._elementRef.nativeElement.focus();
@@ -75,8 +111,8 @@ export class MatChipTrailingIcon {
75111
* @docs-private
76112
*/
77113
class MatChipRemoveBase extends MatChipTrailingIcon {
78-
constructor(_elementRef: ElementRef) {
79-
super(_elementRef);
114+
constructor(elementRef: ElementRef) {
115+
super(elementRef);
80116
}
81117
}
82118

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,12 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
253253
return (target && (target as Element).classList) ?
254254
(target as Element).classList.contains(className) : false;
255255
},
256-
notifyInteraction: () => this.interaction.emit(this.id),
256+
notifyInteraction: () => this._notifyInteraction(),
257257
notifySelection: () => {
258258
// No-op. We call dispatchSelectionEvent ourselves in MatChipOption, because we want to
259259
// specify whether selection occurred via user input.
260260
},
261-
notifyNavigation: () => {
262-
// TODO: This is a new feature added by MDC; consider exposing this event to users in the
263-
// future.
264-
},
261+
notifyNavigation: () => this._notifyNavigation(),
265262
notifyTrailingIconInteraction: () => this.removeIconInteraction.emit(this.id),
266263
notifyRemoval: () => {
267264
this.removed.emit({ chip: this });
@@ -407,6 +404,14 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
407404
return this.disabled || this.disableRipple || this._isBasicChip;
408405
}
409406

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

0 commit comments

Comments
 (0)