Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit 468c324

Browse files
author
Vivek Kumar Bansal
committed
refactor attributes code
1 parent b5e3e54 commit 468c324

File tree

11 files changed

+48
-39
lines changed

11 files changed

+48
-39
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ All the changes can be found below. Order used:
99
- Fixed
1010
- Security
1111

12+
## v1.5.0
13+
- Added `attributes` prop to `MenuItem` and `ContextMenuLayer` for further customization.
14+
1215
## v1.4.0
1316

1417
### Added

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ Default: `false`
168168

169169
By default, the context menu is closed as soon as an item is clicked. Set this prop to control this behavior.
170170

171+
**props.attributes**
172+
173+
Type: `Object` optional
174+
175+
The attributes will be passed directly passed to the root element of `MenuItem`. Use this to customize it like adding custom classes, etc.
176+
171177
### SubMenu(props)
172178

173179
Type: React Component
@@ -247,6 +253,12 @@ Type: React Element (optional)
247253

248254
The element inside which the Component must be wrapped. By default `div` is used. But this prop can used to customize it.
249255

256+
**attributes**
257+
258+
Type: `Object` optional
259+
260+
The attributes will be passed directly passed to the root element of component. Use this to customize it like adding custom classes, adding `colspan` etc.
261+
250262
```js
251263
// following examples shows usage of `tr` instead of `div`
252264

File renamed without changes.

examples/custom-wrappers/container.js renamed to examples/customization/container.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const Container = React.createClass({
2424
<table className="table table-bordered">
2525
<tbody>
2626
{targets.map((item, i) => (
27-
<Target renderTag="tr" name={item.name} key={i}/>
27+
<Target renderTag="tr" name={item.name} key={i}
28+
attributes={{className: "my-custom-row-class"}}/>
2829
))}
2930
</tbody>
3031
</table>

examples/custom-wrappers/index.js renamed to examples/customization/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import React from "react";
44
import Container from "./container";
55
import Menu from "./menu";
66

7-
const CustomWrappers = React.createClass({
8-
displayName: "CustomWrappers",
7+
const Customization = React.createClass({
8+
displayName: "Customization",
99
getInitialState() {
1010
return { logs: [] };
1111
},
@@ -18,7 +18,7 @@ const CustomWrappers = React.createClass({
1818
return (
1919
<div>
2020
<h3>Custom Wrappers</h3>
21-
<p>This demo shows usage of custom wrappers. Instead of using <code>div</code>s by default, we are using <code>tr</code>s</p>
21+
<p>This demo shows usage of customization. Instead of using <code>div</code>s by default, we are using <code>tr</code>s</p>
2222
<Container/>
2323
<div>
2424
{this.state.logs.map((log, i) => (<p key={i}>{log}</p>))}
@@ -29,4 +29,4 @@ const CustomWrappers = React.createClass({
2929
}
3030
});
3131

32-
export default CustomWrappers;
32+
export default Customization;

examples/custom-wrappers/menu.js renamed to examples/customization/menu.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@ const Menu = React.createClass({
1515
render() {
1616
let { name } = this.props.item;
1717

18+
const attributes = {
19+
className: "my-custom-menu-item-class"
20+
};
21+
1822
return (
1923
<ContextMenu identifier={MenuTypes.multi}>
20-
<MenuItem onClick={this.handleClick} data={{item: "item 1"}}>Menu Item 1</MenuItem>
21-
<MenuItem onClick={this.handleClick} data={{item: "item 2"}}>Menu Item 2</MenuItem>
24+
<MenuItem onClick={this.handleClick} attributes={attributes}
25+
data={{item: "item 1"}}>
26+
Menu Item 1
27+
</MenuItem>
28+
<MenuItem onClick={this.handleClick} attributes={attributes}
29+
data={{item: "item 2"}}>
30+
Menu Item 2
31+
</MenuItem>
2232
<MenuItem onClick={this.handleEat}>Eat {name}</MenuItem>
2333
</ContextMenu>
2434
);
File renamed without changes.

examples/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import SimpleMenu from "./simple-menu";
77
import MultipleTargets from "./multiple-targets";
88
import MultipleMenus from "./multiple-menus";
99
import SubMenus from "./submenus";
10-
import CustomWrappers from "./custom-wrappers";
10+
import Customization from "./customization";
1111

1212
const App = React.createClass({
1313
displayName: "App",
@@ -32,7 +32,7 @@ const App = React.createClass({
3232
<Link to="/submenus">Sub Menus</Link>
3333
</li>
3434
<li>
35-
<Link to="/custom-wrappers">Custom Wrappers</Link>
35+
<Link to="/customization">Customization</Link>
3636
</li>
3737
</ul>
3838
</div>
@@ -52,7 +52,7 @@ const Routes = (
5252
<Route path="multiple-targets" component={MultipleTargets}/>
5353
<Route path="multiple-menus" component={MultipleMenus}/>
5454
<Route path="submenus" component={SubMenus}/>
55-
<Route path="custom-wrappers" component={CustomWrappers}/>
55+
<Route path="customization" component={Customization}/>
5656
</Route>
5757
</Router>
5858
);

src/contextmenu-layer.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import invariant from "invariant";
55
import _isObject from "lodash.isobject";
66

77
import store from "./redux/store";
8-
import tagAttributes from "./tag-attributes.js";
98

109
export default function(identifier, configure) {
1110
return function(Component) {
@@ -61,16 +60,17 @@ export default function(identifier, configure) {
6160
}
6261
});
6362
},
64-
render: function() {
63+
render() {
64+
let { attributes: {className = "", ...attributes}, renderTag, ...props } = this.props;
6565

66-
var attributes = this.props.attributes;
67-
var classNames = tagAttributes.getClassNames(this.props.attributes);
66+
attributes.className = `react-context-menu-wrapper ${className}`;
67+
attributes.onContextMenu = this.handleContextClick;
6868

69-
//Make sure to add the react-context-menu-wrapper information
70-
attributes["className"] = "react-context-menu-wrapper" + classNames;
71-
attributes["onContextMenu"] = this.handleContextClick;
72-
73-
return React.createElement(this.props.renderTag, attributes, React.createElement(Component, this.props));
69+
return React.createElement(
70+
renderTag,
71+
attributes,
72+
React.createElement(Component, props)
73+
);
7474
}
7575
});
7676
};

src/menu-item.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import classnames from "classnames";
55
import assign from "object-assign";
66
import monitor from "./monitor";
77

8-
import tagAttributes from "./tag-attributes.js";
9-
108
let { PropTypes } = React;
119

1210
const MenuItem = React.createClass({
@@ -42,20 +40,17 @@ const MenuItem = React.createClass({
4240
monitor.hideMenu();
4341
},
4442
render() {
45-
let { disabled, children } = this.props;
43+
let { disabled, children, attributes: { className = "", ...props } } = this.props,
44+
menuItemClassNames = `react-context-menu-item ${className}`;
4645

4746
const classes = classnames({
4847
"react-context-menu-link": true,
4948
disabled
5049
});
5150

52-
var menuItemClassNames = tagAttributes.getClassNames(this.props.attributes);
53-
54-
//Make sure to add the react-context-menu-item information
55-
menuItemClassNames = "react-context-menu-item" + menuItemClassNames;
5651

5752
return (
58-
<div className={menuItemClassNames}>
53+
<div className={menuItemClassNames} {...props}>
5954
<a href="#" className={classes} onClick={this.handleClick}>
6055
{children}
6156
</a>

0 commit comments

Comments
 (0)