import React, { Component } from 'react'; import PropTypes from 'prop-types'; import ContextMenuTrigger from 'src/ContextMenuTrigger'; import ContextMenu from 'src/ContextMenu'; import MenuItem from 'src/MenuItem'; import connectMenu from 'src/connectMenu'; const MENU_TYPE = 'DYNAMIC'; const targets = [{ name: 'Banana' }, { name: 'Apple' }, { name: 'Papaya' }, { name: 'Mango' }, { name: 'Orange' }, { name: 'Pineapple' }]; function collect(props) { return props; } const DynamicMenu = (props) => { const { id, trigger } = props; const handleItemClick = trigger ? trigger.onItemClick : null; return ( {trigger && {`Add 1 ${trigger.name}`}} {trigger && ( trigger.allowRemoval ? {`Remove 1 ${trigger.name}`} : {'Removal disabled'} )} ); }; DynamicMenu.propTypes = { id: PropTypes.string.isRequired, trigger: PropTypes.shape({ name: PropTypes.string.isRequired, onItemClick: PropTypes.func.isRequired, allowRemoval: PropTypes.bool }).isRequired }; const ConnectedMenu = connectMenu(MENU_TYPE)(DynamicMenu); export default class DynamicMenuExample extends Component { constructor(props) { super(props); this.state = { logs: [] }; } handleClick = (e, data, target) => { const count = parseInt(target.getAttribute('data-count'), 10); if (data.action === 'Added') { target.setAttribute('data-count', count + 1); return this.setState(({ logs }) => ({ logs: [`${data.action} 1 ${data.name}`, ...logs] })); } if (data.action === 'Removed' && count > 0) { target.setAttribute('data-count', count - 1); return this.setState(({ logs }) => ({ logs: [`${data.action} 1 ${data.name}`, ...logs] })); } return this.setState(({ logs }) => ({ logs: [` ${data.name} cannot be ${data.action.toLowerCase()}`, ...logs] })); } render() { const attributes = { 'data-count': 0, className: 'example-multiple-targets well' }; return (

Dynamic Menu

This demo shows usage of dynamically created menu on multiple targets.

{targets.map((item, i) => (
{item.name}
))}
{this.state.logs.map((log, i) =>

{log}

)}
); } }