forked from vkbansal/react-contextmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectMenu.js
More file actions
56 lines (46 loc) · 1.97 KB
/
Copy pathconnectMenu.js
File metadata and controls
56 lines (46 loc) · 1.97 KB
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
import React, { Component } from 'react';
import ContextMenuTrigger from './ContextMenuTrigger';
import listener from './globalEventListener';
// collect ContextMenuTrigger's expected props to NOT pass them on as part of the context
const ignoredTriggerProps = [...Object.keys(ContextMenuTrigger.propTypes), 'children'];
// expect the id of the menu to be responsible for as outer parameter
export default function (menuId) {
// expect menu component to connect as inner parameter
// <Child/> is presumably a wrapper of <ContextMenu/>
return function connect(Child) {
// return wrapper for <Child/> that forwards the ContextMenuTrigger's additional props
return class ConnectMenu extends Component {
constructor(props) {
super(props);
this.state = { trigger: null };
}
componentDidMount() {
this.listenId = listener.register(this.handleShow, this.handleHide);
}
componentWillUnmount() {
if (this.listenId) {
listener.unregister(this.listenId);
}
}
handleShow = (e) => {
if (e.detail.id !== menuId) return;
// the onShow event's detail.data object holds all ContextMenuTrigger props
const { data } = e.detail;
const filteredData = {};
for (const key in data) {
// exclude props the ContextMenuTrigger is expecting itself
if (!ignoredTriggerProps.includes(key)) {
filteredData[key] = data[key];
}
}
this.setState({ trigger: filteredData });
}
handleHide = () => {
this.setState({ trigger: null });
}
render() {
return <Child {...this.props} id={menuId} trigger={this.state.trigger} />;
}
};
};
}