forked from vkbansal/react-contextmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicMenu.js
115 lines (99 loc) · 3.4 KB
/
DynamicMenu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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 (
<ContextMenu id={id}>
{trigger && <MenuItem onClick={handleItemClick} data={{ action: 'Added' }}>{`Add 1 ${trigger.name}`}</MenuItem>}
{trigger && (
trigger.allowRemoval
? <MenuItem onClick={handleItemClick} data={{ action: 'Removed' }}>{`Remove 1 ${trigger.name}`}</MenuItem>
: <MenuItem disabled>{'Removal disabled'}</MenuItem>
)}
</ContextMenu>
);
};
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 (
<div>
<h3>Dynamic Menu</h3>
<p>This demo shows usage of dynamically created menu on multiple targets.</p>
<div className='pure-g'>
{targets.map((item, i) => (
<div key={i} className='pure-u-1-6'>
<ContextMenuTrigger
id={MENU_TYPE} holdToDisplay={1000}
name={item.name} onItemClick={this.handleClick}
allowRemoval={i % 2 === 0}
collect={collect} attributes={attributes}>
{item.name}
</ContextMenuTrigger>
</div>
))}
</div>
<div>
{this.state.logs.map((log, i) => <p key={i}>{log}</p>)}
</div>
<ConnectedMenu />
</div>
);
}
}