-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMenuItem.js
46 lines (39 loc) · 1.04 KB
/
MenuItem.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
import React from 'react';
import PropTypes from 'prop-types';
import createChainedFunction from '../../util/createChainedFunction';
const propTypes = {
children: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]),
itemMeta: PropTypes.any, // eslint-disable-line react/forbid-prop-types
onSelectItem: PropTypes.func.isRequired,
};
const MenuItem = (props) => {
const handleItemSelect = (e) => {
props.onSelectItem(props.itemMeta, e);
};
const collectClickHandlers = (child) => {
if (child.props) {
return createChainedFunction(child.props.onClick, handleItemSelect);
}
return handleItemSelect;
};
const renderChildren = () => React.Children.map(props.children, child => React.cloneElement(
child,
Object.assign(
{},
child.props,
{ onClick: collectClickHandlers(child) },
),
child.props.children,
));
return (
<div>
{renderChildren()}
</div>
);
};
MenuItem.propTypes = propTypes;
export default MenuItem;