diff --git a/src/plugins/navigation/public/top_nav_menu/top_nav_menu_item.test.tsx b/src/plugins/navigation/public/top_nav_menu/top_nav_menu_item.test.tsx index e633ec4a70be1..430753074a958 100644 --- a/src/plugins/navigation/public/top_nav_menu/top_nav_menu_item.test.tsx +++ b/src/plugins/navigation/public/top_nav_menu/top_nav_menu_item.test.tsx @@ -8,13 +8,12 @@ */ import React from 'react'; -import { TopNavMenuItem } from './top_nav_menu_item'; -import { TopNavMenuData } from './top_nav_menu_data'; +import { TopNavMenuItem, TopNavMenuItemProps } from './top_nav_menu_item'; import { shallowWithIntl } from '@kbn/test-jest-helpers'; import { EuiButtonIcon } from '@elastic/eui'; describe('TopNavMenu', () => { - const ensureMenuItemDisabled = (data: TopNavMenuData) => { + const ensureMenuItemDisabled = (data: TopNavMenuItemProps) => { const component = shallowWithIntl(); expect(component.prop('isDisabled')).toEqual(true); @@ -24,10 +23,11 @@ describe('TopNavMenu', () => { }; it('Should render and click an item', () => { - const data: TopNavMenuData = { + const data: TopNavMenuItemProps = { id: 'test', label: 'test', run: jest.fn(), + closePopover: jest.fn(), }; const component = shallowWithIntl(); @@ -43,13 +43,14 @@ describe('TopNavMenu', () => { }); it('Should render item with all attributes', () => { - const data: TopNavMenuData = { + const data: TopNavMenuItemProps = { id: 'test', label: 'test', description: 'description', testId: 'test-class-name', disableButton: false, run: jest.fn(), + closePopover: jest.fn(), }; const component = shallowWithIntl(); @@ -61,13 +62,14 @@ describe('TopNavMenu', () => { }); it('Should render emphasized item which should be clickable', () => { - const data: TopNavMenuData = { + const data: TopNavMenuItemProps = { id: 'test', label: 'test', iconType: 'beaker', iconSide: 'right', emphasize: true, run: jest.fn(), + closePopover: jest.fn(), }; const component = shallowWithIntl(); @@ -78,12 +80,13 @@ describe('TopNavMenu', () => { }); it('Should render an icon-only item', () => { - const data: TopNavMenuData = { + const data: TopNavMenuItemProps = { id: 'test', label: 'test', iconType: 'share', iconOnly: true, run: jest.fn(), + closePopover: jest.fn(), }; const component = shallowWithIntl(); @@ -100,6 +103,7 @@ describe('TopNavMenu', () => { label: 'test', disableButton: true, run: jest.fn(), + closePopover: jest.fn(), }); }); @@ -109,6 +113,7 @@ describe('TopNavMenu', () => { label: 'test', disableButton: () => true, run: jest.fn(), + closePopover: jest.fn(), }); }); @@ -121,6 +126,7 @@ describe('TopNavMenu', () => { emphasize: true, disableButton: true, run: jest.fn(), + closePopover: jest.fn(), }); }); @@ -133,6 +139,26 @@ describe('TopNavMenu', () => { emphasize: true, disableButton: () => true, run: jest.fn(), + closePopover: jest.fn(), }); }); + + it('Should render emphasized item in mobile mode, which should be clickable and call closePopover on click', () => { + const data: TopNavMenuItemProps = { + id: 'test', + label: 'test', + iconType: 'beaker', + iconSide: 'right', + emphasize: true, + isMobileMenu: true, + run: jest.fn(), + closePopover: jest.fn(), + }; + + const component = shallowWithIntl(); + const event = { currentTarget: { value: 'a' } }; + component.simulate('click', event); + expect(data.run).toHaveBeenCalledTimes(1); + expect(data.closePopover).toHaveBeenCalledTimes(1); + }); }); diff --git a/src/plugins/navigation/public/top_nav_menu/top_nav_menu_item.tsx b/src/plugins/navigation/public/top_nav_menu/top_nav_menu_item.tsx index 84232d47fd337..f19d7d08d79d9 100644 --- a/src/plugins/navigation/public/top_nav_menu/top_nav_menu_item.tsx +++ b/src/plugins/navigation/public/top_nav_menu/top_nav_menu_item.tsx @@ -19,7 +19,12 @@ import { } from '@elastic/eui'; import { TopNavMenuData } from './top_nav_menu_data'; -export function TopNavMenuItem(props: TopNavMenuData & { isMobileMenu?: boolean }) { +export interface TopNavMenuItemProps extends TopNavMenuData { + closePopover: () => void; + isMobileMenu?: boolean; +} + +export function TopNavMenuItem(props: TopNavMenuItemProps) { function isDisabled(): boolean { const val = isFunction(props.disableButton) ? props.disableButton() : props.disableButton; return val!; @@ -46,6 +51,9 @@ export function TopNavMenuItem(props: TopNavMenuData & { isMobileMenu?: boolean function handleClick(e: MouseEvent) { if (isDisabled()) return; props.run(e.currentTarget); + if (props.isMobileMenu) { + props.closePopover(); + } } const commonButtonProps = { diff --git a/src/plugins/navigation/public/top_nav_menu/top_nav_menu_items.tsx b/src/plugins/navigation/public/top_nav_menu/top_nav_menu_items.tsx index 928749beb4477..e5150679bf1d5 100644 --- a/src/plugins/navigation/public/top_nav_menu/top_nav_menu_items.tsx +++ b/src/plugins/navigation/public/top_nav_menu/top_nav_menu_items.tsx @@ -35,9 +35,18 @@ export const TopNavMenuItems = ({ className={className} popoverBreakpoints={popoverBreakpoints} > - {config.map((menuItem: TopNavMenuData, i: number) => { - return ; - })} + {(closePopover) => + config.map((menuItem: TopNavMenuData, i: number) => { + return ( + + ); + }) + } ); };