Skip to content

fix(menu): allow alternate roles to be set on menu item #14165

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
Nov 27, 2018
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
6 changes: 5 additions & 1 deletion src/lib/menu/menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ViewEncapsulation,
Inject,
Optional,
Input,
} from '@angular/core';
import {
CanDisable, CanDisableCtor,
Expand Down Expand Up @@ -42,7 +43,7 @@ export const _MatMenuItemMixinBase: CanDisableRippleCtor & CanDisableCtor & type
exportAs: 'matMenuItem',
inputs: ['disabled', 'disableRipple'],
host: {
'role': 'menuitem',
'[attr.role]': 'role',
'class': 'mat-menu-item',
'[class.mat-menu-item-highlighted]': '_highlighted',
'[class.mat-menu-item-submenu-trigger]': '_triggersSubmenu',
Expand All @@ -59,6 +60,9 @@ export const _MatMenuItemMixinBase: CanDisableRippleCtor & CanDisableCtor & type
export class MatMenuItem extends _MatMenuItemMixinBase
implements FocusableOption, CanDisable, CanDisableRipple, OnDestroy {

/** ARIA role for the menu item. */
@Input() role: 'menuitem' | 'menuitemradio' | 'menuitemcheckbox' = 'menuitem';

private _document: Document;

/** Stream that emits when the menu item is hovered. */
Expand Down
39 changes: 39 additions & 0 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,30 @@ describe('MatMenu', () => {
expect(role).toBe('menu', 'Expected panel to have the "menu" role.');
});

it('should set the "menuitem" role on the items by default', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();

const items = Array.from(overlayContainerElement.querySelectorAll('.mat-menu-item'));

expect(items.length).toBeGreaterThan(0);
expect(items.every(item => item.getAttribute('role') === 'menuitem')).toBe(true);
});

it('should be able to set an alternate role on the menu items', () => {
const fixture = createComponent(MenuWithCheckboxItems);
fixture.detectChanges();
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();

const items = Array.from(overlayContainerElement.querySelectorAll('.mat-menu-item'));

expect(items.length).toBeGreaterThan(0);
expect(items.every(item => item.getAttribute('role') === 'menuitemcheckbox')).toBe(true);
});

it('should not throw an error on destroy', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
expect(fixture.destroy.bind(fixture)).not.toThrow();
Expand Down Expand Up @@ -2100,3 +2124,18 @@ class DynamicPanelMenu {
@ViewChild('one') firstMenu: MatMenu;
@ViewChild('two') secondMenu: MatMenu;
}


@Component({
template: `
<button [matMenuTriggerFor]="menu">Toggle menu</button>

<mat-menu #menu="matMenu">
<button mat-menu-item role="menuitemcheckbox" aria-checked="true">Checked</button>
<button mat-menu-item role="menuitemcheckbox" aria-checked="false">Not checked</button>
</mat-menu>
`
})
class MenuWithCheckboxItems {
@ViewChild(MatMenuTrigger) trigger: MatMenuTrigger;
}