Skip to content

Commit c0499b8

Browse files
authored
fix(cdk-experimental/menu): fix double state toggle in radio and checkbox menu items (#20259)
Fixes an issue where clicking a CdkMenuItemRadio or CdkMenuItemCheckbox causes the click handler to fire twice resulting in toggling state twice and an unchecked checkbox.
1 parent dedb799 commit c0499b8

File tree

4 files changed

+5
-23
lines changed

4 files changed

+5
-23
lines changed

src/cdk-experimental/menu/menu-item-checkbox.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('MenuItemCheckbox', () => {
5353
it('should toggle the aria checked attribute', () => {
5454
expect(checkboxElement.getAttribute('aria-checked')).toBeNull();
5555

56-
checkbox.trigger();
56+
checkboxElement.click();
5757
fixture.detectChanges();
5858

5959
expect(checkboxElement.getAttribute('aria-checked')).toBe('true');

src/cdk-experimental/menu/menu-item-checkbox.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, HostListener} from '@angular/core';
9+
import {Directive} from '@angular/core';
1010
import {CdkMenuItemSelectable} from './menu-item-selectable';
1111
import {CdkMenuItem} from './menu-item';
1212

@@ -29,11 +29,7 @@ import {CdkMenuItem} from './menu-item';
2929
],
3030
})
3131
export class CdkMenuItemCheckbox extends CdkMenuItemSelectable {
32-
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
33-
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
34-
// can move this back into `host`.
35-
// tslint:disable:no-host-decorator-in-concrete
36-
@HostListener('click')
32+
/** Toggle the checked state of the checkbox. */
3733
trigger() {
3834
super.trigger();
3935

src/cdk-experimental/menu/menu-item-radio.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('MenuItemRadio', () => {
5151
it('should toggle the aria checked attribute', () => {
5252
expect(radioElement.getAttribute('aria-checked')).toBeNull();
5353

54-
radioButton.trigger();
54+
radioElement.click();
5555
fixture.detectChanges();
5656

5757
expect(radioElement.getAttribute('aria-checked')).toBe('true');

src/cdk-experimental/menu/menu-item-radio.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {UniqueSelectionDispatcher} from '@angular/cdk/collections';
9-
import {
10-
Directive,
11-
OnDestroy,
12-
ElementRef,
13-
Self,
14-
Optional,
15-
Inject,
16-
NgZone,
17-
HostListener,
18-
} from '@angular/core';
9+
import {Directive, OnDestroy, ElementRef, Self, Optional, Inject, NgZone} from '@angular/core';
1910
import {Directionality} from '@angular/cdk/bidi';
2011
import {CdkMenuItemSelectable} from './menu-item-selectable';
2112
import {CdkMenuItem} from './menu-item';
@@ -68,11 +59,6 @@ export class CdkMenuItemRadio extends CdkMenuItemSelectable implements OnDestroy
6859
);
6960
}
7061

71-
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
72-
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
73-
// can move this back into `host`.
74-
// tslint:disable:no-host-decorator-in-concrete
75-
@HostListener('click')
7662
/** Toggles the checked state of the radio-button. */
7763
trigger() {
7864
super.trigger();

0 commit comments

Comments
 (0)