-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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({ | ||
|
@@ -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"> | ||
* <mat-icon>edit</mat-icon> | ||
* </button> | ||
* </mat-chip> | ||
* ``` | ||
*/ | ||
|
||
@Directive({ | ||
selector: '[matChipEdit]', | ||
host: { | ||
'class': | ||
'mat-mdc-chip-edit mat-mdc-chip-avatar mat-focus-indicator ' + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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; | ||
} | ||
|
@@ -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))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need a timeout here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}, | ||
); | ||
|
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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.