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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 26 additions & 3 deletions goldens/material/chips/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export const MAT_CHIP: InjectionToken<unknown>;
// @public
export const MAT_CHIP_AVATAR: InjectionToken<unknown>;

// @public
export const MAT_CHIP_EDIT: InjectionToken<unknown>;

// @public
export const MAT_CHIP_LISTBOX_CONTROL_VALUE_ACCESSOR: any;

Expand All @@ -50,6 +53,7 @@ export const MAT_CHIPS_DEFAULT_OPTIONS: InjectionToken<MatChipsDefaultOptions>;
// @public
export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck, OnDestroy {
constructor(...args: unknown[]);
protected _allEditIcons: QueryList<MatChipEdit>;
protected _allLeadingIcons: QueryList<MatChipAvatar>;
protected _allRemoveIcons: QueryList<MatChipRemove>;
protected _allTrailingIcons: QueryList<MatChipTrailingIcon>;
Expand All @@ -68,6 +72,8 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
disableRipple: boolean;
// (undocumented)
protected _document: Document;
_edit(event: Event): void;
editIcon: MatChipEdit;
// (undocumented)
_elementRef: ElementRef<HTMLElement>;
focus(): void;
Expand Down Expand Up @@ -119,7 +125,7 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
// (undocumented)
protected _value: any;
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatChip, "mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]", ["matChip"], { "role": { "alias": "role"; "required": false; }; "id": { "alias": "id"; "required": false; }; "ariaLabel": { "alias": "aria-label"; "required": false; }; "ariaDescription": { "alias": "aria-description"; "required": false; }; "value": { "alias": "value"; "required": false; }; "color": { "alias": "color"; "required": false; }; "removable": { "alias": "removable"; "required": false; }; "highlighted": { "alias": "highlighted"; "required": false; }; "disableRipple": { "alias": "disableRipple"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; }, { "removed": "removed"; "destroyed": "destroyed"; }, ["leadingIcon", "trailingIcon", "removeIcon", "_allLeadingIcons", "_allTrailingIcons", "_allRemoveIcons"], ["mat-chip-avatar, [matChipAvatar]", "*", "mat-chip-trailing-icon,[matChipRemove],[matChipTrailingIcon]"], true, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatChip, "mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]", ["matChip"], { "role": { "alias": "role"; "required": false; }; "id": { "alias": "id"; "required": false; }; "ariaLabel": { "alias": "aria-label"; "required": false; }; "ariaDescription": { "alias": "aria-description"; "required": false; }; "value": { "alias": "value"; "required": false; }; "color": { "alias": "color"; "required": false; }; "removable": { "alias": "removable"; "required": false; }; "highlighted": { "alias": "highlighted"; "required": false; }; "disableRipple": { "alias": "disableRipple"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; }, { "removed": "removed"; "destroyed": "destroyed"; }, ["leadingIcon", "editIcon", "trailingIcon", "removeIcon", "_allLeadingIcons", "_allTrailingIcons", "_allEditIcons", "_allRemoveIcons"], ["mat-chip-avatar, [matChipAvatar]", "*", "mat-chip-trailing-icon,[matChipRemove],[matChipTrailingIcon]"], true, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatChip, never>;
}
Expand All @@ -132,6 +138,20 @@ export class MatChipAvatar {
static ɵfac: i0.ɵɵFactoryDeclaration<MatChipAvatar, never>;
}

// @public
export class MatChipEdit extends MatChipAction {
// (undocumented)
_handleClick(event: MouseEvent): void;
// (undocumented)
_handleKeydown(event: KeyboardEvent): void;
// (undocumented)
_isPrimary: boolean;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MatChipEdit, "[matChipEdit]", never, {}, {}, never, never, true, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatChipEdit, never>;
}

// @public
export interface MatChipEditedEvent extends MatChipEvent {
value: string;
Expand Down Expand Up @@ -420,21 +440,24 @@ export class MatChipRow extends MatChip implements AfterViewInit {
contentEditInput?: MatChipEditInput;
defaultEditInput?: MatChipEditInput;
// (undocumented)
_edit(): void;
// (undocumented)
editable: boolean;
readonly edited: EventEmitter<MatChipEditedEvent>;
// (undocumented)
_handleDoubleclick(event: MouseEvent): void;
_handleFocus(): void;
// (undocumented)
_handleKeydown(event: KeyboardEvent): void;
protected _hasLeadingIcon(): boolean;
// (undocumented)
_hasTrailingIcon(): boolean;
// (undocumented)
_isEditing: boolean;
// (undocumented)
_isRippleDisabled(): boolean;
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatChipRow, "mat-chip-row, [mat-chip-row], mat-basic-chip-row, [mat-basic-chip-row]", never, { "editable": { "alias": "editable"; "required": false; }; }, { "edited": "edited"; }, ["contentEditInput"], ["mat-chip-avatar, [matChipAvatar]", "[matChipEditInput]", "*", "mat-chip-trailing-icon,[matChipRemove],[matChipTrailingIcon]"], true, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatChipRow, "mat-chip-row, [mat-chip-row], mat-basic-chip-row, [mat-basic-chip-row]", never, { "editable": { "alias": "editable"; "required": false; }; }, { "edited": "edited"; }, ["contentEditInput"], ["[matChipEdit]", "mat-chip-avatar, [matChipAvatar]", "[matChipEditInput]", "*", "mat-chip-trailing-icon,[matChipRemove],[matChipTrailingIcon]"], true, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatChipRow, never>;
}
Expand Down Expand Up @@ -511,7 +534,7 @@ export class MatChipsModule {
// (undocumented)
static ɵinj: i0.ɵɵInjectorDeclaration<MatChipsModule>;
// (undocumented)
static ɵmod: i0.ɵɵNgModuleDeclaration<MatChipsModule, never, [typeof MatCommonModule, typeof MatRippleModule, typeof MatChipAction, typeof MatChip, typeof MatChipAvatar, typeof MatChipEditInput, typeof MatChipGrid, typeof MatChipInput, typeof MatChipListbox, typeof MatChipOption, typeof MatChipRemove, typeof MatChipRow, typeof MatChipSet, typeof MatChipTrailingIcon], [typeof MatCommonModule, typeof MatChip, typeof MatChipAvatar, typeof MatChipEditInput, typeof MatChipGrid, typeof MatChipInput, typeof MatChipListbox, typeof MatChipOption, typeof MatChipRemove, typeof MatChipRow, typeof MatChipSet, typeof MatChipTrailingIcon]>;
static ɵmod: i0.ɵɵNgModuleDeclaration<MatChipsModule, never, [typeof MatCommonModule, typeof MatRippleModule, typeof MatChipAction, typeof MatChip, typeof MatChipAvatar, typeof MatChipEdit, typeof MatChipEditInput, typeof MatChipGrid, typeof MatChipInput, typeof MatChipListbox, typeof MatChipOption, typeof MatChipRemove, typeof MatChipRow, typeof MatChipSet, typeof MatChipTrailingIcon], [typeof MatCommonModule, typeof MatChip, typeof MatChipAvatar, typeof MatChipEdit, typeof MatChipEditInput, typeof MatChipGrid, typeof MatChipInput, typeof MatChipListbox, typeof MatChipOption, typeof MatChipRemove, typeof MatChipRow, typeof MatChipSet, typeof MatChipTrailingIcon]>;
}

// @public
Expand Down
13 changes: 13 additions & 0 deletions goldens/material/chips/testing/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { TestKey } from '@angular/cdk/testing';
export interface ChipAvatarHarnessFilters extends BaseHarnessFilters {
}

// @public (undocumented)
export interface ChipEditHarnessFilters extends BaseHarnessFilters {
}

// @public (undocumented)
export interface ChipEditInputHarnessFilters extends BaseHarnessFilters {
}
Expand Down Expand Up @@ -67,6 +71,14 @@ export class MatChipAvatarHarness extends ComponentHarness {
static with<T extends MatChipAvatarHarness>(this: ComponentHarnessConstructor<T>, options?: ChipAvatarHarnessFilters): HarnessPredicate<T>;
}

// @public
export class MatChipEditHarness extends ComponentHarness {
click(): Promise<void>;
// (undocumented)
static hostSelector: string;
static with<T extends MatChipEditHarness>(this: ComponentHarnessConstructor<T>, options?: ChipEditHarnessFilters): HarnessPredicate<T>;
}

// @public
export class MatChipEditInputHarness extends ComponentHarness {
// (undocumented)
Expand All @@ -89,6 +101,7 @@ export class MatChipGridHarness extends ComponentHarness {

// @public
export class MatChipHarness extends ContentContainerComponentHarness {
geEditButton(filter?: ChipEditHarnessFilters): Promise<MatChipEditHarness>;
getAvatar(filter?: ChipAvatarHarnessFilters): Promise<MatChipAvatarHarness | null>;
getRemoveButton(filter?: ChipRemoveHarnessFilters): Promise<MatChipRemoveHarness>;
getText(): Promise<string>;
Expand Down
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
51 changes: 50 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,55 @@ export class MatChipTrailingIcon extends MatChipAction {
override _isPrimary = false;
}

/**
* Directive to edit the parent chip when the leading action icon is clicked or
* when the ENTER key is pressed on it.
*
* Recommended for use with the Material Design "edit" icon
* available at https://material.io/icons/#ic_edit.
*
* Example:
*
* ```
* <mat-chip>
* <button matChipEdit aria-label="Edit">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the component show matIconButton?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't in the demo's and actually adding it breaks the styling, so don't want to put in example here. Something to clean up later perhaps though.

* <mat-icon>edit</mat-icon>
* </button>
* </mat-chip>
* ```
*/

@Directive({
selector: '[matChipEdit]',
host: {
'class':
'mat-mdc-chip-edit mat-mdc-chip-avatar mat-focus-indicator ' +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • We shouldn't reuse the avatar class here since this isn't the avatar.
  • The focus indicator class already comes from whatever is used as the edit trigger (e.g. matButton).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let me clean up the avatar usage. I added the leading-action concept later and think makes sense to keep them distinct all the way through now.

'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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to allow editing even if the chip isn't marked as editable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. Editable controls the double-click and focus-enter, the button logic is independent.

}
}
}

/**
* Directive to remove the parent chip when the trailing icon is clicked or
* when the ENTER key is pressed on it.
Expand Down
7 changes: 6 additions & 1 deletion src/material/chips/chip-row.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
<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
19 changes: 19 additions & 0 deletions src/material/chips/chip-row.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,21 @@ describe('Row Chips', () => {
}));
});

describe('with edit icon', () => {
beforeEach(async () => {
testComponent.showEditIcon = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
});

it('should begin editing on edit click', () => {
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
dispatchFakeEvent(chipNativeElement.querySelector('.mat-mdc-chip-edit')!, 'click');
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeTruthy();
});
});

describe('a11y', () => {
it('should apply `ariaLabel` and `ariaDesciption` to the primary gridcell', () => {
fixture.componentInstance.ariaLabel = 'chip name';
Expand Down Expand Up @@ -403,6 +418,9 @@ describe('Row Chips', () => {
(destroyed)="chipDestroy($event)"
(removed)="chipRemove($event)" (edited)="chipEdit($event)"
[aria-label]="ariaLabel" [aria-description]="ariaDescription">
@if (showEditIcon) {
<button matChipEdit>edit</button>
}
{{name}}
<button matChipRemove>x</button>
@if (useCustomEditInput) {
Expand All @@ -424,6 +442,7 @@ class SingleChip {
removable: boolean = true;
shouldShow: boolean = true;
editable: boolean = false;
showEditIcon: boolean = false;
useCustomEditInput: boolean = true;
ariaLabel: string | null = null;
ariaDescription: string | null = null;
Expand Down
30 changes: 23 additions & 7 deletions src/material/chips/chip-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ 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-leading-action]': '!!editIcon',
'[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 +108,11 @@ export class MatChipRow extends MatChip implements AfterViewInit {
});
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this end up hiding the avatar if the chip is being edited?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was intentional, but I'm not actually not sure if that logic was omitted as no demo cases for editable chips with avatar or intentionally kept visibile (unlike remove icon). But that can be handled seperately, so I'll pull this out.

}

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

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 +172,9 @@ export class MatChipRow extends MatChip implements AfterViewInit {
afterNextRender(
() => {
this._getEditInput().initialize(value);
this._editStartPending = false;

// Necessary when using edit icon to prevent edit from aborting
setTimeout(() => this._ngZone.run(() => (this._editStartPending = false)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a timeout here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a comment. When using the edit icon, need to have this timeout as otherwise subsequent blur will cancel the edit action.

},
{injector: this._injector},
);
Expand Down
Loading
Loading