Skip to content
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

fix broken megamenu keyboard naviagation #15407

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
82 changes: 79 additions & 3 deletions src/app/components/megamenu/megamenu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('MegaMenu', () => {
fixture.detectChanges();

expect(itemClickSpy).toHaveBeenCalled();
expect(megamenu.activeItem).toEqual(null);
expect(megamenu.activeItem()).toEqual(null);
});

it("shouldn't call itemClick ", () => {
Expand Down Expand Up @@ -366,7 +366,7 @@ describe('MegaMenu', () => {
expect(tv1Div.className).toContain('p-megamenu-col-4');
});

it('should item get p-megamenu-col-3', () => {
it('should item get p-megamenu-col-6', () => {
megamenu.model = [
{
label: 'TVV',
Expand Down Expand Up @@ -405,7 +405,7 @@ describe('MegaMenu', () => {
let tv1Div = fixture.debugElement.query(By.css('.p-megamenu-grid')).query(By.css('div')).nativeElement;
fixture.detectChanges();

expect(tv1Div.className).toContain('p-megamenu-col-3');
expect(tv1Div.className).toContain('p-megamenu-col-6');
});

it('should item get p-megamenu-col-2', () => {
Expand Down Expand Up @@ -465,4 +465,80 @@ describe('MegaMenu', () => {

expect(tv1Div.className).toContain('p-megamenu-col-2');
});

it('should move to the next item onKeyboardArrowKeyDown', () => {
megamenu.model = [
{
label: 'TV',
icon: 'pi pi-fw pi-check',
items: [
[
{
label: 'TV 1',
items: [{ label: 'TV 1.1' }, { label: 'TV 1.2' }]
},
{
label: 'TV 2',
items: [{ label: 'TV 2.1' }, { label: 'TV 2.2' }]
}
]
]
},
{
label: 'Sports',
icon: 'pi pi-fw pi-soccer-ball-o',
items: [
[
{
label: 'Sports 1',
items: [{ label: 'Sports 1.1' }, { label: 'Sports 1.2' }]
},
{
label: 'Sports 2',
items: [{ label: 'Sports 2.1' }, { label: 'Sports 2.2' }]
}
]
]
}
];

const onArrowDownKeySpy = spyOn(megamenu, 'onArrowDownKey').and.callThrough();

//focus on menu
megamenu.onMenuFocus(new Event(''));
fixture.detectChanges();

const parentMenuEl = fixture.debugElement.query(By.css('.p-megamenu-root-list'));
const tvEl = parentMenuEl.children[0];

expect(megamenu.focusedItemInfo().index).toBe(0);
expect(megamenu.focusedItemInfo().parentKey).toBe('');
expect(megamenu.focusedItemInfo().item.label).toBe('TV');
expect(tvEl.attributes['aria-expanded']).toBe('false');

//simulate keyboard arrow down key press
const event = new KeyboardEvent('keydown', { key: 'ArrowDown', code: 'ArrowDown' });
parentMenuEl.nativeElement.dispatchEvent(event);
fixture.detectChanges();

expect(onArrowDownKeySpy).toHaveBeenCalled();
expect(megamenu.focusedItemInfo().index).toBe(0);
expect(megamenu.focusedItemInfo().parentKey).toBe('0_0_0');
expect(megamenu.focusedItemInfo().item.label).toBe('TV 1.1');
expect(tvEl.attributes['aria-expanded']).toBe('true');

//keyboard arrow down key press again
parentMenuEl.nativeElement.dispatchEvent(event);
fixture.detectChanges();
expect(megamenu.focusedItemInfo().index).toBe(1);
expect(megamenu.focusedItemInfo().parentKey).toBe('0_0_0');
expect(megamenu.focusedItemInfo().item.label).toBe('TV 1.2');

//keyboard arrow down key press again
parentMenuEl.nativeElement.dispatchEvent(event);
fixture.detectChanges();
expect(megamenu.focusedItemInfo().index).toBe(2);
expect(megamenu.focusedItemInfo().parentKey).toBe('0_0_1');
expect(megamenu.focusedItemInfo().item.label).toBe('TV 2.1');
});
});
2 changes: 1 addition & 1 deletion src/app/components/megamenu/megamenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ export class MegaMenu implements AfterContentInit, OnDestroy, OnInit {
get visibleItems() {
const processedItem = ObjectUtils.isNotEmpty(this.activeItem()) ? this.activeItem() : null;

return processedItem && processedItem.key === this.focusedItemInfo().parentKey
return processedItem
? processedItem.items.reduce((items, col) => {
col.forEach((submenu) => {
submenu.items.forEach((a) => {
Expand Down
Loading