import React, { ReactNode } from 'react' import { identity } from 'fp-ts/lib/function' import { Colors } from '@monorail/helpers/color' import { Sizes } from '@monorail/helpers/size' import { PopOver, PopOverToggleProps, } from '@monorail/metaComponents/popOver/PopOver' import { ButtonDisplay } from '@monorail/visualComponents/buttons/buttonTypes' import { IconButton } from '@monorail/visualComponents/buttons/IconButton' import { Divider } from '@monorail/visualComponents/divider/Divider' import { IconType } from '@monorail/visualComponents/icon/IconType' import { SimpleListItem } from '@monorail/visualComponents/list/List' import { Menu } from '@monorail/visualComponents/menu/Menu' /* * Styles */ /* * Types */ export type MenuActionOnClick = ( onClickParent: () => void, event: React.MouseEvent, ) => R export type MenuAction = | { type: 'divider' } | { type?: 'action' label: ReactNode iconName?: IconType iconColor?: Colors chromeless?: boolean /** * TODO: get rid of the need to have to pass a callback to close the popover. * This onClick should match the signature of all other react onClick. * If we weren't depending on asynchronous behavior in components that are consuming * ActionsMenu we would just be able to stop propagation on the SyntheticEvent */ onClick: MenuActionOnClick isFeaturedAction?: boolean children?: ReactNode disabled?: boolean } export type ActionsMenuProps = { actions: Array> document?: Document toggle?: (props: PopOverToggleProps) => ReactNode onActionComplete?: (r: R) => void } /* * Component */ export const ActionsMenu = (props: ActionsMenuProps) => { const { document, actions, toggle = (toggleProps: PopOverToggleProps) => ( ), onActionComplete = identity, ...domProps } = props return ( <> {actions.length > 0 && ( ( {actions.reduce>((filtered, action, idx) => { if (action.type === 'divider') { return filtered.concat() } /** * Setting a field on a menu item to `isFeaturedAction: true` * does not have the intended effect it used to. Better to not * use this field when creating buttons to be used with the * ActionsMenu component. * - AR - 20200716 */ if (!action.isFeaturedAction) { return filtered.concat( onActionComplete(action.onClick(() => onClick(e), e)) } disabled={action.disabled} > {action.children} , ) } return filtered }, [])} )} toggle={toggleProps => toggle({ ...toggleProps, ...domProps })} /> )} ) }