Skip to content

Commit fb95b1c

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 fb95b1c

File tree

4 files changed

+575
-526
lines changed

4 files changed

+575
-526
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: 37 additions & 6 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,8 @@ import {
2021
mixinDisabled,
2122
mixinTabIndex,
2223
} from '@angular/material/core';
24+
import {MDCChipTrailingActionAdapter, MDCChipTrailingActionFoundation} from '@material/chips';
25+
import {MatChip} from './chip';
2326
import {Subject} from 'rxjs';
2427

2528

@@ -57,8 +60,36 @@ export class MatChipAvatar {
5760
'aria-hidden': 'true',
5861
}
5962
})
60-
export class MatChipTrailingIcon {
61-
constructor(public _elementRef: ElementRef) {}
63+
export class MatChipTrailingIcon implements OnDestroy {
64+
private _foundation: MDCChipTrailingActionFoundation;
65+
private _adapter: MDCChipTrailingActionAdapter = {
66+
focus: () => this._elementRef.nativeElement.focus(),
67+
getAttribute: (name: string) => this._elementRef.nativeElement.getAttribute(name),
68+
setAttribute: (name: string, value: string) => {
69+
this._elementRef.nativeElement.setAttribute(name, value);
70+
},
71+
// TODO(crisbeto): this also a `trigger` parameter that the chip isn't
72+
// handling yet. Consider passing it along once MDC start using it.
73+
notifyInteraction: () => this._chip._notifyInteraction(),
74+
75+
// TODO(crisbeto): this also a `key` parameter that the chip isn't
76+
// handling yet. Consider passing it along once MDC start using it.
77+
notifyNavigation: () => this._chip._notifyNavigation()
78+
};
79+
80+
constructor(public _elementRef: ElementRef, private _chip: MatChip) {
81+
this._foundation = new MDCChipTrailingActionFoundation(this._adapter);
82+
83+
// TODO(crisbeto): for the time being we need to assign the trailing icon like this in order
84+
// to avoid circular references, because the chip foundation still depends on it for the
85+
// `setTrailingActionAttr` method. Once that method is removed in favor of the trailing
86+
// action foundation, we can remove this call.
87+
_chip.trailingIcon = this;
88+
}
89+
90+
ngOnDestroy() {
91+
this._foundation.destroy();
92+
}
6293

6394
focus() {
6495
this._elementRef.nativeElement.focus();
@@ -75,8 +106,8 @@ export class MatChipTrailingIcon {
75106
* @docs-private
76107
*/
77108
class MatChipRemoveBase extends MatChipTrailingIcon {
78-
constructor(_elementRef: ElementRef) {
79-
super(_elementRef);
109+
constructor(elementRef: ElementRef, chip: MatChip) {
110+
super(elementRef, chip);
80111
}
81112
}
82113

@@ -123,8 +154,8 @@ export class MatChipRemove extends _MatChipRemoveMixinBase implements CanDisable
123154
*/
124155
interaction: Subject<MouseEvent | KeyboardEvent> = new Subject<MouseEvent | KeyboardEvent>();
125156

126-
constructor(elementRef: ElementRef) {
127-
super(elementRef);
157+
constructor(elementRef: ElementRef, chip: MatChip) {
158+
super(elementRef, chip);
128159

129160
if (elementRef.nativeElement.nodeName === 'BUTTON') {
130161
elementRef.nativeElement.setAttribute('type', 'button');

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
226226
@ContentChild(MatChipAvatar) leadingIcon: MatChipAvatar;
227227

228228
/** The chip's trailing icon. */
229-
@ContentChild(MatChipTrailingIcon) trailingIcon: MatChipTrailingIcon;
229+
trailingIcon: MatChipTrailingIcon;
230230

231231
/** The chip's trailing remove icon. */
232232
@ContentChild(MatChipRemove) removeIcon: MatChipRemove;
@@ -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)