import classnames from 'classnames'; import React, { PureComponent } from 'react'; import { PanelMenuItem } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { Menu } from '@grafana/ui'; import { PanelHeaderMenuItem } from './PanelHeaderMenuItem'; export interface Props { items: PanelMenuItem[]; style?: React.CSSProperties; itemsClassName?: string; className?: string; } export class PanelHeaderMenu extends PureComponent { renderItems = (menu: PanelMenuItem[], isSubMenu = false) => { return ( ); }; render() { return (
{this.renderItems(flattenGroups(this.props.items))}
); } } function flattenGroups(items: PanelMenuItem[]): PanelMenuItem[] { return items.reduce((all: PanelMenuItem[], item) => { if (Array.isArray(item.subMenu) && item.type === 'submenu') { all.push({ ...item, subMenu: flattenGroups(item.subMenu), }); return all; } if (Array.isArray(item.subMenu) && item.type === 'group') { const { subMenu, ...rest } = item; all.push(rest); all.push.apply(all, flattenGroups(subMenu)); return all; } all.push(item); return all; }, []); } export function PanelHeaderMenuNew({ items }: Props) { const renderItems = (items: PanelMenuItem[]) => { return items.map((item) => { switch (item.type) { case 'divider': return ; case 'group': return ( {item.subMenu ? renderItems(item.subMenu) : undefined} ); default: return ( ); } }); }; return {renderItems(items)}; }