Skip to content

feat(cdk/menu): Allow setting cdkMenuTriggerFor null #25818

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 5 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion src/cdk/menu/menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ export class CdkMenuItem implements FocusableOption, FocusableElement, Toggler,
@Output('cdkMenuItemTriggered') readonly triggered: EventEmitter<void> = new EventEmitter();

/** Whether the menu item opens a menu. */
readonly hasMenu = !!this._menuTrigger;
get hasMenu() {
if (this._menuTrigger?.menuTemplateRef == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this can be simplified to return this._menuTrigger?.menuTemplateRef != null.

return false;
}

return true;
}

/**
* The tabindex for this menu item managed internally and used for implementing roving a
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/menu/menu-trigger-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export abstract class CdkMenuTriggerBase implements OnDestroy {
readonly closed: EventEmitter<void> = new EventEmitter();

/** Template reference variable to the menu this trigger opens */
menuTemplateRef: TemplateRef<unknown>;
menuTemplateRef: TemplateRef<unknown> | null;

/** Context data to be passed along to the menu template */
menuData: unknown;
Expand Down
95 changes: 92 additions & 3 deletions src/cdk/menu/menu-trigger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {CdkContextMenuTrigger} from './context-menu-trigger';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the linter is complaining that this import is unused

import {Component, ViewChildren, QueryList, ElementRef, ViewChild, Type} from '@angular/core';
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
Expand Down Expand Up @@ -47,12 +48,36 @@ describe('MenuTrigger', () => {
expect(menuItemElement.getAttribute('aria-disabled')).toBe('true');
});

it('should set aria-haspopup to menu', () => {
it('should set aria-haspopup based on whether a menu is assigned', () => {
expect(menuItemElement.getAttribute('aria-haspopup')).toEqual('menu');

fixture.componentInstance.trigger.menuTemplateRef = null;
fixture.detectChanges();

expect(menuItemElement.hasAttribute('aria-haspopup')).toBe(false);
});

it('should have a menu', () => {
it('should have a menu based on whether a menu is assigned', () => {
expect(menuItem.hasMenu).toBeTrue();

fixture.componentInstance.trigger.menuTemplateRef = null;
fixture.detectChanges();

expect(menuItem.hasMenu).toBeFalse();
});

it('should set aria-controls based on whether a menu is assigned', () => {
expect(menuItemElement.hasAttribute('aria-controls')).toBeFalse();
});

it('should set aria-expanded based on whether a menu is assigned', () => {
expect(menuItemElement.hasAttribute('aria-expanded')).toBeTrue();
expect(menuItemElement.getAttribute('aria-expanded')).toBe('false');

fixture.componentInstance.trigger.menuTemplateRef = null;
fixture.detectChanges();

expect(menuItemElement.hasAttribute('aria-expanded')).toBeFalse();
});
});

Expand Down Expand Up @@ -469,6 +494,50 @@ describe('MenuTrigger', () => {

expect(document.querySelector('.test-menu')?.textContent).toBe('Hello!');
});

xdescribe('null triggerFor', () => {
let fixture: ComponentFixture<TriggerWithNullValue>;

let nativeTrigger: HTMLElement;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [CdkMenuModule],
declarations: [TriggerWithNullValue],
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(TriggerWithNullValue);
nativeTrigger = fixture.componentInstance.nativeTrigger.nativeElement
});

it('should not set aria-haspopup', () => {
expect(nativeTrigger.hasAttribute('aria-haspopup')).toBeFalse();
});

it('should not set aria-controls', () => {
expect(nativeTrigger.hasAttribute('aria-controls')).toBeFalse();
});

it('should not toggle the menu on trigger', () => {
expect(fixture.componentInstance.trigger.isOpen()).toBeFalse();

nativeTrigger.click();
fixture.detectChanges();

expect(fixture.componentInstance.trigger.isOpen()).toBeFalse();
});

it('should not toggle the menu on keyboard events', () => {
expect(fixture.componentInstance.trigger.isOpen()).toBeFalse();

dispatchKeyboardEvent(nativeTrigger, 'keydown', SPACE);
fixture.detectChanges();

expect(fixture.componentInstance.trigger.isOpen()).toBeFalse();
});
});
});

@Component({
Expand All @@ -477,7 +546,10 @@ describe('MenuTrigger', () => {
<ng-template #noop><div cdkMenu></div></ng-template>
`,
})
class TriggerForEmptyMenu {}
class TriggerForEmptyMenu {
@ViewChild(CdkMenuTrigger) trigger: CdkMenuTrigger;
@ViewChild(CdkMenuTrigger, { read: ElementRef }) nativeTrigger: ElementRef;
}

@Component({
template: `
Expand Down Expand Up @@ -602,3 +674,20 @@ class StandaloneTriggerWithInlineMenu {
class TriggerWithData {
menuData: unknown;
}

@Component({
template: `
<button [cdkMenuTriggerFor]="null">First</button>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we need an entirely separate fixture where it is set to null. Instead we should adapt an existing fixture so we can also test that the DOM is updated on dynamic changes. See how it's done for MatMenu https://github.com/angular/components/blob/main/src/material/menu/menu.spec.ts#L92.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good idea, I updated tests.

<button [cdkContextMenuTriggerFor]="null">First</button>
`,
})
class TriggerWithNullValue {
@ViewChild(CdkMenuTrigger)
trigger: CdkMenuTrigger;

@ViewChild(CdkMenuTrigger, {read: ElementRef})
nativeTrigger: ElementRef<HTMLElement>;

@ViewChild(CdkContextMenuTrigger, {read: ElementRef})
contextMenuNativeTrigger: ElementRef<HTMLElement>;
}
6 changes: 3 additions & 3 deletions src/cdk/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import {CdkMenuTriggerBase, MENU_TRIGGER} from './menu-trigger-base';
standalone: true,
host: {
'class': 'cdk-menu-trigger',
'aria-haspopup': 'menu',
'[attr.aria-expanded]': 'isOpen()',
'[attr.aria-haspopup]': 'menuTemplateRef ? "menu" : null',
'[attr.aria-expanded]': 'menuTemplateRef == null ? null : isOpen()',
'(focusin)': '_setHasFocus(true)',
'(focusout)': '_setHasFocus(false)',
'(keydown)': '_toggleOnKeydown($event)',
Expand Down Expand Up @@ -99,7 +99,7 @@ export class CdkMenuTrigger extends CdkMenuTriggerBase implements OnDestroy {

/** Open the attached menu. */
open() {
if (!this.isOpen()) {
if (!this.isOpen() && this.menuTemplateRef != null) {
this.opened.next();

this.overlayRef = this.overlayRef || this._overlay.create(this._getOverlayConfig());
Expand Down