Skip to content

feat(material/chips): add (optional) edit icon to input chips #31041

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions src/dev-app/chips/chips-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ <h4>Multi selection</h4>

<mat-checkbox [(ngModel)]="disableInputs">Disabled</mat-checkbox>
<mat-checkbox [(ngModel)]="editable">Editable</mat-checkbox>
<mat-checkbox [(ngModel)]="peopleWithAvatar">Show Avatar</mat-checkbox>
<mat-checkbox [(ngModel)]="showEditIcon">Show Edit Icon</mat-checkbox>
<mat-checkbox [(ngModel)]="disabledInteractive">Disabled Interactive</mat-checkbox>

<h4>Input is last child of chip grid</h4>
Expand All @@ -172,6 +174,14 @@ <h4>Input is last child of chip grid</h4>
[editable]="editable"
(removed)="remove(person)"
(edited)="edit(person, $event)">
@if (showEditIcon) {
<button matChipEdit aria-label="Edit contributor">
<mat-icon>edit</mat-icon>
</button>
}
@if (peopleWithAvatar && person.avatar) {
<mat-chip-avatar>{{person.avatar}}</mat-chip-avatar>
}
{{person.name}}
<button matChipRemove aria-label="Remove contributor">
<mat-icon>close</mat-icon>
Expand Down
15 changes: 9 additions & 6 deletions src/dev-app/chips/chips-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {MatIconModule} from '@angular/material/icon';

export interface Person {
name: string;
avatar?: string;
}

export interface DemoColor {
Expand Down Expand Up @@ -52,6 +53,8 @@ export class ChipsDemo {
listboxesWithAvatar = false;
disableInputs = false;
editable = false;
peopleWithAvatar = false;
showEditIcon = false;
disabledInteractive = false;
message = '';

Expand All @@ -75,12 +78,12 @@ export class ChipsDemo {
selectedPeople = null;

people: Person[] = [
{name: 'Kara'},
{name: 'Jeremy'},
{name: 'Topher'},
{name: 'Elad'},
{name: 'Kristiyan'},
{name: 'Paul'},
{name: 'Kara', avatar: 'K'},
{name: 'Jeremy', avatar: 'J'},
{name: 'Topher', avatar: 'T'},
{name: 'Elad', avatar: 'E'},
{name: 'Kristiyan', avatar: 'K'},
{name: 'Paul', avatar: 'P'},
];

availableColors: DemoColor[] = [
Expand Down
1 change: 1 addition & 0 deletions src/material/chips/chip-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class MatChipAction {
_handlePrimaryActionInteraction(): void;
remove(): void;
disabled: boolean;
_edit(): void;
_isEditing?: boolean;
}>(MAT_CHIP);

Expand Down
49 changes: 48 additions & 1 deletion src/material/chips/chip-icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {ENTER, SPACE} from '@angular/cdk/keycodes';
import {Directive} from '@angular/core';
import {MatChipAction} from './chip-action';
import {MAT_CHIP_AVATAR, MAT_CHIP_REMOVE, MAT_CHIP_TRAILING_ICON} from './tokens';
import {MAT_CHIP_AVATAR, MAT_CHIP_EDIT, MAT_CHIP_REMOVE, MAT_CHIP_TRAILING_ICON} from './tokens';

/** Avatar image within a chip. */
@Directive({
Expand Down Expand Up @@ -42,6 +42,53 @@ export class MatChipTrailingIcon extends MatChipAction {
override _isPrimary = false;
}

/**
* Directive to remove the parent chip when the trailing icon is clicked or
* when the ENTER key is pressed on it.
*
* Recommended for use with the Material Design "cancel" icon
* available at https://material.io/icons/#ic_cancel.
*
* Example:
*
* ```
* <mat-chip>
* <mat-icon matChipEdit>cancel</mat-icon>
* </mat-chip>
* ```
*/

@Directive({
selector: '[matChipEdit]',
host: {
'class':
'mat-mdc-chip-edit mat-mdc-chip-avatar mat-focus-indicator ' +
'mdc-evolution-chip__icon mdc-evolution-chip__icon--primary',
'role': 'button',
'[attr.aria-hidden]': 'null',
},
providers: [{provide: MAT_CHIP_EDIT, useExisting: MatChipEdit}],
})
export class MatChipEdit extends MatChipAction {
override _isPrimary = false;

override _handleClick(event: MouseEvent): void {
if (!this.disabled) {
event.stopPropagation();
event.preventDefault();
this._parentChip._edit();
}
}

override _handleKeydown(event: KeyboardEvent) {
if ((event.keyCode === ENTER || event.keyCode === SPACE) && !this.disabled) {
event.stopPropagation();
event.preventDefault();
this._parentChip._edit();
}
}
}

/**
* Directive to remove the parent chip when the trailing icon is clicked or
* when the ENTER key is pressed on it.
Expand Down
9 changes: 8 additions & 1 deletion src/material/chips/chip-row.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
<span class="mat-mdc-chip-focus-overlay"></span>
}

@if (!_isEditing && editIcon) {
<span
class="mdc-evolution-chip__cell mdc-evolution-chip__cell--primary"
role="gridcell">
<ng-content select="[matChipEdit]"></ng-content>
</span>
}
<span class="mdc-evolution-chip__cell mdc-evolution-chip__cell--primary" role="gridcell"
matChipAction
[disabled]="disabled"
[attr.aria-label]="ariaLabel"
[attr.aria-describedby]="_ariaDescriptionId">
@if (leadingIcon) {
@if (!_isEditing && leadingIcon) {
<span class="mdc-evolution-chip__graphic mat-mdc-chip-graphic">
<ng-content select="mat-chip-avatar, [matChipAvatar]"></ng-content>
</span>
Expand Down
29 changes: 21 additions & 8 deletions src/material/chips/chip-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export interface MatChipEditedEvent extends MatChipEvent {
styleUrl: 'chip.css',
host: {
'class': 'mat-mdc-chip mat-mdc-chip-row mdc-evolution-chip',
'[class.mat-mdc-chip-with-avatar]': 'leadingIcon',
'[class.mat-mdc-chip-with-avatar]': '_hasLeadingIcon()',
'[class.mat-mdc-chip-disabled]': 'disabled',
'[class.mat-mdc-chip-editing]': '_isEditing',
'[class.mat-mdc-chip-editable]': 'editable',
'[class.mdc-evolution-chip--disabled]': 'disabled',
'[class.mdc-evolution-chip--with-trailing-action]': '_hasTrailingIcon()',
'[class.mdc-evolution-chip--with-primary-graphic]': 'leadingIcon',
'[class.mdc-evolution-chip--with-primary-icon]': 'leadingIcon',
'[class.mdc-evolution-chip--with-avatar]': 'leadingIcon',
'[class.mdc-evolution-chip--with-primary-graphic]': '_hasLeadingIcon()',
'[class.mdc-evolution-chip--with-primary-icon]': '_hasLeadingIcon()',
'[class.mdc-evolution-chip--with-avatar]': '_hasLeadingIcon()',
'[class.mat-mdc-chip-highlighted]': 'highlighted',
'[class.mat-mdc-chip-with-trailing-icon]': '_hasTrailingIcon()',
'[id]': 'id',
Expand Down Expand Up @@ -107,6 +107,11 @@ export class MatChipRow extends MatChip implements AfterViewInit {
});
}

/** Returns whether the chip has a leading icon. */
_hasLeadingIcon() {
return !this._isEditing && !!(this.editIcon || this.leadingIcon);
}

override _hasTrailingIcon() {
// The trailing icon is hidden while editing.
return !this._isEditing && super._hasTrailingIcon();
Expand Down Expand Up @@ -135,16 +140,24 @@ export class MatChipRow extends MatChip implements AfterViewInit {
}
}

_handleDoubleclick(event: MouseEvent) {
_handleDoubleclick(event: Event) {
if (!this.disabled && this.editable) {
this._startEditing(event);
}
}

private _startEditing(event: Event) {
override _edit(): void {
// markForCheck necessary for edit input to be rendered
this._changeDetectorRef.markForCheck();
this._startEditing();
}

private _startEditing(event?: Event) {
if (
!this.primaryAction ||
(this.removeIcon && this._getSourceAction(event.target as Node) === this.removeIcon)
(this.removeIcon &&
!!event &&
this._getSourceAction(event.target as Node) === this.removeIcon)
) {
return;
}
Expand All @@ -158,7 +171,7 @@ export class MatChipRow extends MatChip implements AfterViewInit {
afterNextRender(
() => {
this._getEditInput().initialize(value);
this._editStartPending = false;
setTimeout(() => this._ngZone.run(() => (this._editStartPending = false)));
},
{injector: this._injector},
);
Expand Down
6 changes: 3 additions & 3 deletions src/material/chips/chip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ $token-slots: m2-chip.get-token-slots();
}
}

.mat-mdc-chip-remove {
.mat-mdc-chip-edit, .mat-mdc-chip-remove {
opacity: token-utils.slot(trailing-action-opacity);

&:focus {
Expand Down Expand Up @@ -680,7 +680,7 @@ $token-slots: m2-chip.get-token-slots();
}
}

.mat-mdc-chip-remove {
.mat-mdc-chip-edit, .mat-mdc-chip-remove {
&::before {
$default-border-width: focus-indicators-private.$default-border-width;
$offset: var(--mat-focus-indicator-border-width, #{$default-border-width});
Expand Down Expand Up @@ -744,6 +744,6 @@ $token-slots: m2-chip.get-token-slots();
// Prevents icon from being cut off when text spacing is increased.
// .mat-mdc-chip-remove selector necessary for remove button with icon.
// Fixes b/250063405.
.mdc-evolution-chip__icon, .mat-mdc-chip-remove .mat-icon {
.mdc-evolution-chip__icon, .mat-mdc-chip-edit .mat-icon, .mat-mdc-chip-remove .mat-icon {
min-height: fit-content;
}
27 changes: 25 additions & 2 deletions src/material/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ import {
} from '../core';
import {Subject, Subscription, merge} from 'rxjs';
import {MatChipAction} from './chip-action';
import {MatChipAvatar, MatChipRemove, MatChipTrailingIcon} from './chip-icons';
import {MAT_CHIP, MAT_CHIP_AVATAR, MAT_CHIP_REMOVE, MAT_CHIP_TRAILING_ICON} from './tokens';
import {MatChipAvatar, MatChipEdit, MatChipRemove, MatChipTrailingIcon} from './chip-icons';
import {
MAT_CHIP,
MAT_CHIP_AVATAR,
MAT_CHIP_EDIT,
MAT_CHIP_REMOVE,
MAT_CHIP_TRAILING_ICON,
} from './tokens';

/** Represents an event fired on an individual `mat-chip`. */
export interface MatChipEvent {
Expand Down Expand Up @@ -133,6 +139,10 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
@ContentChildren(MAT_CHIP_TRAILING_ICON, {descendants: true})
protected _allTrailingIcons: QueryList<MatChipTrailingIcon>;

/** All edit icons present in the chip. */
@ContentChildren(MAT_CHIP_EDIT, {descendants: true})
protected _allEditIcons: QueryList<MatChipEdit>;

/** All remove icons present in the chip. */
@ContentChildren(MAT_CHIP_REMOVE, {descendants: true})
protected _allRemoveIcons: QueryList<MatChipRemove>;
Expand Down Expand Up @@ -225,6 +235,9 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
/** The chip's leading icon. */
@ContentChild(MAT_CHIP_AVATAR) leadingIcon: MatChipAvatar;

/** The chip's leading edit icon. */
@ContentChild(MAT_CHIP_EDIT) editIcon: MatChipEdit;

/** The chip's trailing icon. */
@ContentChild(MAT_CHIP_TRAILING_ICON) trailingIcon: MatChipTrailingIcon;

Expand Down Expand Up @@ -279,6 +292,7 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
this._actionChanges = merge(
this._allLeadingIcons.changes,
this._allTrailingIcons.changes,
this._allEditIcons.changes,
this._allRemoveIcons.changes,
).subscribe(() => this._changeDetectorRef.markForCheck());
}
Expand Down Expand Up @@ -358,6 +372,10 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
_getActions(): MatChipAction[] {
const result: MatChipAction[] = [];

if (this.editIcon) {
result.push(this.editIcon);
}

if (this.primaryAction) {
result.push(this.primaryAction);
}
Expand All @@ -378,6 +396,11 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
// Empty here, but is overwritten in child classes.
}

/** Handles interactions with the edit action of the chip. */
_edit(event: Event) {
// Empty here, but is overwritten in child classes.
}

/** Starts the focus monitoring process on the chip. */
private _monitorFocus() {
this._focusMonitor.monitor(this._elementRef, true).subscribe(origin => {
Expand Down
3 changes: 2 additions & 1 deletion src/material/chips/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {MatChip} from './chip';
import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './tokens';
import {MatChipEditInput} from './chip-edit-input';
import {MatChipGrid} from './chip-grid';
import {MatChipAvatar, MatChipRemove, MatChipTrailingIcon} from './chip-icons';
import {MatChipAvatar, MatChipEdit, MatChipRemove, MatChipTrailingIcon} from './chip-icons';
import {MatChipInput} from './chip-input';
import {MatChipListbox} from './chip-listbox';
import {MatChipRow} from './chip-row';
Expand All @@ -24,6 +24,7 @@ import {MatChipAction} from './chip-action';
const CHIP_DECLARATIONS = [
MatChip,
MatChipAvatar,
MatChipEdit,
MatChipEditInput,
MatChipGrid,
MatChipInput,
Expand Down
7 changes: 7 additions & 0 deletions src/material/chips/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export const MAT_CHIP_AVATAR = new InjectionToken('MatChipAvatar');
*/
export const MAT_CHIP_TRAILING_ICON = new InjectionToken('MatChipTrailingIcon');

/**
* Injection token that can be used to reference instances of `MatChipEdit`. It serves as
* alternative token to the actual `MatChipEdit` class which could cause unnecessary
* retention of the class and its directive metadata.
*/
export const MAT_CHIP_EDIT = new InjectionToken('MatChipEdit');

/**
* Injection token that can be used to reference instances of `MatChipRemove`. It serves as
* alternative token to the actual `MatChipRemove` class which could cause unnecessary
Expand Down
Loading