-
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. 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 suspect that may be confusing for developers using the component since the only way left to conditionally disable editing is by completely removing the edit icon. 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 thnk with the edit button, it will be confusing for it to be present and not editable. They can still disable the chip however, or remove or disable the icon button. I think those are preferable to having an edit button present but not functional. |
||
} | ||
} | ||
} | ||
|
||
/** | ||
* Directive to remove the parent chip when the trailing icon is clicked or | ||
* when the ENTER key is pressed on it. | ||
|
@@ -71,6 +120,7 @@ export class MatChipTrailingIcon extends MatChipAction { | |
}) | ||
export class MatChipRemove extends MatChipAction { | ||
override _isPrimary = false; | ||
override _isTrailing = true; | ||
|
||
override _handleClick(event: MouseEvent): void { | ||
if (!this.disabled) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.