Skip to content

fix(chips): only add type attribute to button remove icons #18477

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

Merged
merged 1 commit into from
Feb 26, 2020
Merged
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
11 changes: 6 additions & 5 deletions src/material-experimental/mdc-chips/chip-icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ const _MatChipRemoveMixinBase:
'(click)': 'interaction.next($event)',
'(keydown)': 'interaction.next($event)',

// Prevent accidental form submissions.
'type': 'button',

// We need to remove this explicitly, because it gets inherited from MatChipTrailingIcon.
'[attr.aria-hidden]': 'null',
}
Expand All @@ -126,8 +123,12 @@ export class MatChipRemove extends _MatChipRemoveMixinBase implements CanDisable
*/
interaction: Subject<MouseEvent | KeyboardEvent> = new Subject<MouseEvent | KeyboardEvent>();

constructor(_elementRef: ElementRef) {
super(_elementRef);
constructor(elementRef: ElementRef) {
super(elementRef);

if (elementRef.nativeElement.nodeName === 'BUTTON') {
elementRef.nativeElement.setAttribute('type', 'button');
}
}

static ngAcceptInputType_disabled: BooleanInput;
Expand Down
11 changes: 10 additions & 1 deletion src/material-experimental/mdc-chips/chip-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe('MDC-based Chip Remove', () => {
expect(buttonElement.getAttribute('type')).toBe('button');
});

it('should not set the `type` attribute on non-button elements', () => {
const buttonElement = chipNativeElement.querySelector('span.mat-mdc-chip-remove')!;

expect(buttonElement.hasAttribute('type')).toBe(false);
});

it('should start MDC exit animation on click', () => {
let buttonElement = chipNativeElement.querySelector('button')!;

Expand Down Expand Up @@ -158,7 +164,10 @@ describe('MDC-based Chip Remove', () => {
<mat-chip
[removable]="removable"
[disabled]="disabled"
(removed)="didRemove()"><button matChipRemove></button></mat-chip>
(removed)="didRemove()">
<button matChipRemove></button>
<span matChipRemove></span>
</mat-chip>
`
})
class TestChip {
Expand Down
11 changes: 10 additions & 1 deletion src/material/chips/chip-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ describe('Chip Remove', () => {
expect(buttonElement.getAttribute('type')).toBe('button');
});

it('should not set the `type` attribute on non-button elements', () => {
const buttonElement = chipNativeElement.querySelector('span.mat-chip-remove')!;

expect(buttonElement.hasAttribute('type')).toBe(false);
});

it('should emits (removed) on click', () => {
let buttonElement = chipNativeElement.querySelector('button')!;

Expand Down Expand Up @@ -79,7 +85,10 @@ describe('Chip Remove', () => {
<mat-chip
[removable]="removable"
[disabled]="disabled"
(removed)="didRemove()"><button matChipRemove></button></mat-chip>
(removed)="didRemove()">
<button matChipRemove></button>
<span matChipRemove></span>
</mat-chip>
`
})
class TestChip {
Expand Down
14 changes: 10 additions & 4 deletions src/material/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,19 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
host: {
'class': 'mat-chip-remove mat-chip-trailing-icon',
'(click)': '_handleClick($event)',

// Prevent accidental form submissions.
'type': 'button',
}
})
export class MatChipRemove {
constructor(protected _parentChip: MatChip) {}
constructor(
protected _parentChip: MatChip,
// @breaking-change 11.0.0 `elementRef` parameter to be made required.
elementRef?: ElementRef<HTMLElement>) {

// @breaking-change 11.0.0 Remove null check for `elementRef`.
if (elementRef && elementRef.nativeElement.nodeName === 'BUTTON') {
elementRef.nativeElement.setAttribute('type', 'button');
}
}

/** Calls the parent chip's public `remove()` method if applicable. */
_handleClick(event: Event): void {
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/chips.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export declare class MatChipListChange {

export declare class MatChipRemove {
protected _parentChip: MatChip;
constructor(_parentChip: MatChip);
constructor(_parentChip: MatChip, elementRef?: ElementRef<HTMLElement>);
_handleClick(event: Event): void;
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatChipRemove, "[matChipRemove]", never, {}, {}, never>;
static ɵfac: i0.ɵɵFactoryDef<MatChipRemove>;
Expand Down