// @flow import * as React from "react"; import { Link } from "react-router-dom"; import DropdownMenu from "./DropdownMenu"; import DropdownMenuItem from "./DropdownMenuItem"; type MenuItem = | {| title: React.Node, to: string, visible?: boolean, disabled?: boolean, |} | {| title: React.Node, onClick: (event: SyntheticEvent<>) => void | Promise, visible?: boolean, disabled?: boolean, |} | {| title: React.Node, href: string, visible?: boolean, disabled?: boolean, |} | {| title: React.Node, visible?: boolean, disabled?: boolean, style?: Object, hover?: boolean, items: MenuItem[], |} | {| type: "separator", visible?: boolean, |} | {| type: "heading", visible?: boolean, title: React.Node, |}; type Props = {| items: MenuItem[], |}; export default function DropdownMenuItems({ items }: Props): React.Node { let filtered = items.filter((item) => item.visible !== false); // this block literally just trims unneccessary separators filtered = filtered.reduce((acc, item, index) => { // trim separators from start / end if (item.type === "separator" && index === 0) return acc; if (item.type === "separator" && index === filtered.length - 1) return acc; // trim double separators looking ahead / behind const prev = filtered[index - 1]; if (prev && prev.type === "separator" && item.type === "separator") return acc; // otherwise, continue return [...acc, item]; }, []); return filtered.map((item, index) => { if (item.to) { return ( {item.title} ); } if (item.href) { return ( {item.title} ); } if (item.onClick) { return ( {item.title} ); } if (item.items) { return ( {item.title} } hover={item.hover} key={index} > ); } if (item.type === "separator") { return
; } return null; }); }