import "./menu-actions.scss"; import React, { isValidElement } from "react"; import { observable } from "mobx"; import { observer } from "mobx-react"; import { autobind, cssNames } from "../../utils"; import { ConfirmDialog } from "../confirm-dialog"; import { Icon, IconProps } from "../icon"; import { Menu, MenuItem, MenuProps } from "../menu"; import uniqueId from "lodash/uniqueId"; import isString from "lodash/isString"; export interface MenuActionsProps extends Partial { className?: string; toolbar?: boolean; // display menu as toolbar with icons autoCloseOnSelect?: boolean; triggerIcon?: string | IconProps | React.ReactNode; removeConfirmationMessage?: React.ReactNode | (() => React.ReactNode); updateAction?(): void; removeAction?(): void; onOpen?(): void; } @observer export class MenuActions extends React.Component { static defaultProps: MenuActionsProps = { get removeConfirmationMessage() { return `Remove item?`; } }; public id = uniqueId("menu_actions_"); @observable isOpen = !!this.props.toolbar; toggle = () => { if (this.props.toolbar) return; this.isOpen = !this.isOpen; }; @autobind() remove() { const { removeAction } = this.props; let { removeConfirmationMessage } = this.props; if (typeof removeConfirmationMessage === "function") { removeConfirmationMessage = removeConfirmationMessage(); } ConfirmDialog.open({ ok: removeAction, labelOk: `Remove`, message:
{removeConfirmationMessage}
, }); } renderTriggerIcon() { if (this.props.toolbar) return; const { triggerIcon = "more_vert" } = this.props; let className: string; if (isValidElement(triggerIcon)) { className = cssNames(triggerIcon.props.className, { active: this.isOpen }); return React.cloneElement(triggerIcon, { id: this.id, className } as any); } const iconProps: Partial = { id: this.id, interactive: true, material: isString(triggerIcon) ? triggerIcon : undefined, active: this.isOpen, ...(typeof triggerIcon === "object" ? triggerIcon : {}), }; if (this.props.onOpen) { iconProps.onClick = this.props.onOpen; } if (iconProps.tooltip && this.isOpen) { delete iconProps.tooltip; // don't show tooltip for icon when menu is open } return ( ); } render() { const { className, toolbar, autoCloseOnSelect, children, updateAction, removeAction, triggerIcon, removeConfirmationMessage, ...menuProps } = this.props; const menuClassName = cssNames("MenuActions flex", className, { toolbar, gaps: toolbar, // add spacing for .flex }); const autoClose = !toolbar; return ( <> {this.renderTriggerIcon()} {children} {updateAction && ( Edit )} {removeAction && ( Remove )} ); } }