Skip to content

fix(chips): avoid accidental form submits when pressing remove button #18095

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
Jan 8, 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
3 changes: 3 additions & 0 deletions src/material-experimental/mdc-chips/chip-icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ 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 Down
6 changes: 6 additions & 0 deletions src/material-experimental/mdc-chips/chip-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ describe('MDC-based Chip Remove', () => {
expect(buttonElement.classList).toContain('mat-mdc-chip-remove');
});

it('should ensure that the button cannot submit its parent form', () => {
const buttonElement = chipNativeElement.querySelector('button')!;

expect(buttonElement.getAttribute('type')).toBe('button');
});

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

Expand Down
8 changes: 7 additions & 1 deletion src/material/chips/chip-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ describe('Chip Remove', () => {
}));

describe('basic behavior', () => {
it('should applies the `mat-chip-remove` CSS class', () => {
it('should apply the `mat-chip-remove` CSS class', () => {
let buttonElement = chipNativeElement.querySelector('button')!;

expect(buttonElement.classList).toContain('mat-chip-remove');
});

it('should ensure that the button cannot submit its parent form', () => {
const buttonElement = chipNativeElement.querySelector('button')!;

expect(buttonElement.getAttribute('type')).toBe('button');
});

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

Expand Down
3 changes: 3 additions & 0 deletions src/material/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ 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 {
Expand Down