Skip to content

Commit 3b4e496

Browse files
crisbetommalerba
authored andcommitted
fix(chips): avoid accidental form submits when pressing remove… (#18095)
Sets a `type` on chip remove button so that parent forms don't get submitted by accident.
1 parent 2fd862d commit 3b4e496

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

src/material-experimental/mdc-chips/chip-icons.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ const _MatChipRemoveMixinBase:
112112
'(click)': 'interaction.next($event)',
113113
'(keydown)': 'interaction.next($event)',
114114

115+
// Prevent accidental form submissions.
116+
'type': 'button',
117+
115118
// We need to remove this explicitly, because it gets inherited from MatChipTrailingIcon.
116119
'[attr.aria-hidden]': 'null',
117120
}

src/material-experimental/mdc-chips/chip-remove.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ describe('MDC-based Chip Remove', () => {
3737
expect(buttonElement.classList).toContain('mat-mdc-chip-remove');
3838
});
3939

40+
it('should ensure that the button cannot submit its parent form', () => {
41+
const buttonElement = chipNativeElement.querySelector('button')!;
42+
43+
expect(buttonElement.getAttribute('type')).toBe('button');
44+
});
45+
4046
it('should start MDC exit animation on click', () => {
4147
let buttonElement = chipNativeElement.querySelector('button')!;
4248

src/material/chips/chip-remove.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ describe('Chip Remove', () => {
3030
}));
3131

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

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

39+
it('should ensure that the button cannot submit its parent form', () => {
40+
const buttonElement = chipNativeElement.querySelector('button')!;
41+
42+
expect(buttonElement.getAttribute('type')).toBe('button');
43+
});
44+
3945
it('should emits (removed) on click', () => {
4046
let buttonElement = chipNativeElement.querySelector('button')!;
4147

src/material/chips/chip.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
415415
host: {
416416
'class': 'mat-chip-remove mat-chip-trailing-icon',
417417
'(click)': '_handleClick($event)',
418+
419+
// Prevent accidental form submissions.
420+
'type': 'button',
418421
}
419422
})
420423
export class MatChipRemove {

0 commit comments

Comments
 (0)