forked from vkbansal/react-contextmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultipleTargets.js
89 lines (75 loc) · 2.56 KB
/
MultipleTargets.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
import React, { Component } from 'react';
import ContextMenuTrigger from 'src/ContextMenuTrigger';
import ContextMenu from 'src/ContextMenu';
import MenuItem from 'src/MenuItem';
const MENU_TYPE = 'MULTI';
const targets = [{
name: 'Banana'
}, {
name: 'Apple'
}, {
name: 'Papaya'
}, {
name: 'Mango'
}, {
name: 'Orange'
}, {
name: 'Pineapple'
}];
function collect(props) {
return { name: props.name };
}
export default class MultipleTargets 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]
}));
}
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>Multiple Menus</h3>
<p>This demo shows usage of multiple menus on multiple targets.</p>
<div className='row'>
{targets.map((item, i) => (
<div key={i} className='col-sm-2 text-center'>
<ContextMenuTrigger id={MENU_TYPE} name={item.name}
holdToDisplay={1000}
collect={collect} attributes={attributes}>
{item.name}
</ContextMenuTrigger>
</div>
))}
</div>
<div>
{this.state.logs.map((log, i) => (<p key={i}>{log}</p>))}
</div>
<ContextMenu id={MENU_TYPE}>
<MenuItem onClick={this.handleClick} data={{action: 'Added'}}>Add 1 count</MenuItem>
<MenuItem onClick={this.handleClick} data={{action: 'Removed'}}>Remove 1 count</MenuItem>
</ContextMenu>
</div>
);
}
}