Skip to content

refactor(material-experimental/mdc-chips): implement trailing icon foundation #18877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/material-experimental/mdc-chips/chip-icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ChangeDetectorRef,
Directive,
ElementRef,
OnDestroy,
} from '@angular/core';
import {
CanDisable,
Expand All @@ -20,6 +21,7 @@ import {
mixinDisabled,
mixinTabIndex,
} from '@angular/material/core';
import {MDCChipTrailingActionAdapter, MDCChipTrailingActionFoundation} from '@material/chips';
import {Subject} from 'rxjs';


Expand Down Expand Up @@ -57,8 +59,42 @@ export class MatChipAvatar {
'aria-hidden': 'true',
}
})
export class MatChipTrailingIcon {
constructor(public _elementRef: ElementRef) {}
export class MatChipTrailingIcon implements OnDestroy {
private _foundation: MDCChipTrailingActionFoundation;
private _adapter: MDCChipTrailingActionAdapter = {
focus: () => this._elementRef.nativeElement.focus(),
getAttribute: (name: string) => this._elementRef.nativeElement.getAttribute(name),
setAttribute: (name: string, value: string) => {
this._elementRef.nativeElement.setAttribute(name, value);
},
// TODO(crisbeto): there's also a `trigger` parameter that the chip isn't
// handling yet. Consider passing it along once MDC start using it.
notifyInteraction: () => {
// TODO(crisbeto): uncomment this code once we've inverted the dependency on `MatChip`.
// this._chip._notifyInteraction();
},

// TODO(crisbeto): there's also a `key` parameter that the chip isn't
// handling yet. Consider passing it along once MDC start using it.
notifyNavigation: () => {
// TODO(crisbeto): uncomment this code once we've inverted the dependency on `MatChip`.
// this._chip._notifyNavigation();
}
};

constructor(
public _elementRef: ElementRef,
// TODO(crisbeto): currently the chip needs a reference to the trailing icon for the deprecated
// `setTrailingActionAttr` method. Until the method is removed, we can't use the chip here,
// because it causes a circular import.
// private _chip: MatChip
) {
this._foundation = new MDCChipTrailingActionFoundation(this._adapter);
}

ngOnDestroy() {
this._foundation.destroy();
}

focus() {
this._elementRef.nativeElement.focus();
Expand All @@ -75,8 +111,8 @@ export class MatChipTrailingIcon {
* @docs-private
*/
class MatChipRemoveBase extends MatChipTrailingIcon {
constructor(_elementRef: ElementRef) {
super(_elementRef);
constructor(elementRef: ElementRef) {
super(elementRef);
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/material-experimental/mdc-chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,12 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
return (target && (target as Element).classList) ?
(target as Element).classList.contains(className) : false;
},
notifyInteraction: () => this.interaction.emit(this.id),
notifyInteraction: () => this._notifyInteraction(),
notifySelection: () => {
// No-op. We call dispatchSelectionEvent ourselves in MatChipOption, because we want to
// specify whether selection occurred via user input.
},
notifyNavigation: () => {
// TODO: This is a new feature added by MDC; consider exposing this event to users in the
// future.
},
notifyNavigation: () => this._notifyNavigation(),
notifyTrailingIconInteraction: () => this.removeIconInteraction.emit(this.id),
notifyRemoval: () => {
this.removed.emit({ chip: this });
Expand Down Expand Up @@ -408,6 +405,14 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
return this.disabled || this.disableRipple || this._isBasicChip;
}

_notifyInteraction() {
this.interaction.emit(this.id);
}

_notifyNavigation() {
// TODO: This is a new feature added by MDC. Consider exposing it to users in the future.
}

static ngAcceptInputType_disabled: BooleanInput;
static ngAcceptInputType_removable: BooleanInput;
static ngAcceptInputType_highlighted: BooleanInput;
Expand Down