-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Context menu upgrade from v1.6.2 to v2.9.2 #1081
Changes from 4 commits
bbce3f9
6ff153f
874819f
c9f0e55
c792018
5045270
19c825d
c82e21a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import {MenuItem, monitor, SubMenu, connect, ContextMenuLayer} from 'react-contextmenu'; | ||
import ContextMenu from './ContextMenu'; | ||
import {ContextMenu, MenuItem, monitor, SubMenu, connect, ContextMenuTrigger} from 'react-contextmenu'; | ||
import MenuHeader from './MenuHeader'; | ||
|
||
export {ContextMenu, MenuHeader, MenuItem, monitor, SubMenu, connect, ContextMenuLayer}; | ||
export {ContextMenu, MenuHeader, MenuItem, monitor, SubMenu, connect, ContextMenuTrigger}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,78 @@ | ||
.react-context-menu { | ||
.react-contextmenu { | ||
min-width: 160px; | ||
padding: 5px 0; | ||
margin: 2px 0 0; | ||
font-size: 14px; | ||
font-size: 16px; | ||
color: #373a3c; | ||
text-align: left; | ||
background-color: #fff; | ||
-webkit-background-clip: padding-box; | ||
background-clip: padding-box; | ||
border: 1px solid rgba(0,0,0,.15); | ||
border-radius: .25rem; | ||
outline: none; | ||
opacity: 0; | ||
pointer-events: none; | ||
transition: opacity 250ms ease !important; | ||
} | ||
|
||
.react-context-menu-link { | ||
display: inline-block; | ||
width: 100%; | ||
.react-contextmenu.react-contextmenu--visible { | ||
opacity: 1; | ||
pointer-events: auto; | ||
} | ||
|
||
.react-contextmenu-item { | ||
padding: 3px 20px; | ||
clear: both; | ||
font-weight: 400; | ||
line-height: 1.5; | ||
color: #373a3c; | ||
text-align: inherit; | ||
white-space: nowrap; | ||
background: 0 0; | ||
border: 0; | ||
cursor: pointer; | ||
} | ||
|
||
.react-context-menu-link.active, | ||
.react-context-menu-link:hover { | ||
.react-contextmenu-item.react-contextmenu-item--active, | ||
.react-contextmenu-item.react-contextmenu-item--selected { | ||
color: #fff; | ||
background-color: #0275d8; | ||
border-color: #0275d8; | ||
background-color: #20a0ff; | ||
border-color: #20a0ff; | ||
text-decoration: none; | ||
} | ||
|
||
.react-context-menu-item.submenu > a { | ||
padding-right: 27px; | ||
.react-contextmenu-item.react-contextmenu-item--disabled, | ||
.react-contextmenu-item.react-contextmenu-item--disabled:hover { | ||
color: #878a8c; | ||
background-color: transparent; | ||
border-color: rgba(0,0,0,.15); | ||
} | ||
|
||
.react-contextmenu-item--divider { | ||
margin-bottom: 3px; | ||
padding: 2px 0; | ||
border-bottom: 1px solid rgba(0,0,0,.15); | ||
cursor: inherit; | ||
} | ||
.react-contextmenu-item--divider:hover { | ||
background-color: transparent; | ||
border-color: rgba(0,0,0,.15); | ||
} | ||
|
||
.react-context-menu-item.submenu > a:after { | ||
.react-contextmenu-item.react-contextmenu-submenu { | ||
padding: 0; | ||
} | ||
|
||
.react-contextmenu-item.react-contextmenu-submenu > .react-contextmenu-item { | ||
} | ||
|
||
.react-contextmenu-item.react-contextmenu-submenu > .react-contextmenu-item:after { | ||
content: "▶"; | ||
display: inline-block; | ||
position: absolute; | ||
right: 7px; | ||
} | ||
|
||
.example-multiple-targets::after { | ||
content: attr(data-count); | ||
display: block; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ const Canvas = createReactClass({ | |
rowScrollTimeout: PropTypes.number, | ||
scrollToRowIndex: PropTypes.number, | ||
contextMenu: PropTypes.element, | ||
contextMenuId: PropTypes.string, | ||
getSubRowDetails: PropTypes.func, | ||
rowSelection: PropTypes.oneOfType([ | ||
PropTypes.shape({ | ||
|
@@ -333,6 +334,7 @@ const Canvas = createReactClass({ | |
width={this.props.width} | ||
rows={rows} | ||
contextMenu={this.props.contextMenu} | ||
contextMenuId={this.props.contextMenuId} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on the above comment |
||
rowIdx={this.props.cellMetaData.selected.rowIdx} | ||
idx={this.props.cellMetaData.selected.idx} /> | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const SimpleRowsContainer = (props) => { | ||
return ( | ||
<div key="rows-container"> | ||
{props.rows} | ||
</div> | ||
); | ||
}; | ||
export const DEFAULT_CONTEXT_MENU_ID = 'rgdContextMenu'; | ||
|
||
const SimpleRowsContainer = (props) => <div key="rows-container">{props.rows}</div>; | ||
|
||
SimpleRowsContainer.propTypes = { | ||
width: PropTypes.number, | ||
rows: PropTypes.array | ||
}; | ||
|
||
export const getNewContextMenuProps = ({ contextMenuId, rowIdx, idx }) => ({ | ||
rowIdx, idx, id: contextMenuId || DEFAULT_CONTEXT_MENU_ID | ||
}); | ||
|
||
class RowsContainer extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.plugins = props.window ? props.window.ReactDataGridPlugins : window.ReactDataGridPlugins; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an old code but is there a use case of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @amanmahajan7 can it be server side rendering? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure but that does seem like a workaround for SSR. We can revisit this code once RDG officially supports server side rendering. |
||
this.hasContextMenu = this.hasContextMenu.bind(this); | ||
this.renderRowsWithContextMenu = this.renderRowsWithContextMenu.bind(this); | ||
this.getContextMenuContainer = this.getContextMenuContainer.bind(this); | ||
this.state = {ContextMenuContainer: this.getContextMenuContainer(props)}; | ||
this.validateContextMenu = this.validateContextMenu.bind(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These methods are not used as event handlers and do not need to be bound. They are used in the constructor and in the render method where |
||
|
||
this.validateContextMenu(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if
@malonecj @diogofcunha thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @amanmahajan7 we shouldn't add any side effects in the constructor even when not using react, creating a new object should never affect other parts of an application, you are correct, in react the constructor is used only to set the initial state |
||
} | ||
|
||
getContextMenuContainer() { | ||
if (this.hasContextMenu()) { | ||
if (!this.plugins) { | ||
throw new Error('You need to include ReactDataGrid UiPlugins in order to initialise context menu'); | ||
} | ||
return this.plugins.Menu.ContextMenuLayer('reactDataGridContextMenu')(SimpleRowsContainer); | ||
validateContextMenu() { | ||
if (this.hasContextMenu() && !this.plugins) { | ||
throw new Error('You need to include ReactDataGrid UiPlugins in order to initialise context menu'); | ||
} | ||
} | ||
|
||
|
@@ -38,11 +36,18 @@ class RowsContainer extends React.Component { | |
} | ||
|
||
renderRowsWithContextMenu() { | ||
let ContextMenuRowsContainer = this.state.ContextMenuContainer; | ||
let newProps = {rowIdx: this.props.rowIdx, idx: this.props.idx}; | ||
let contextMenu = React.cloneElement(this.props.contextMenu, newProps); | ||
const { ContextMenuTrigger } = this.plugins.Menu; | ||
const newProps = getNewContextMenuProps(this.props); | ||
const contextMenu = React.cloneElement(this.props.contextMenu, newProps); | ||
// Initialise the context menu if it is available | ||
return (<div><ContextMenuRowsContainer {...this.props} />{contextMenu}</div>); | ||
return ( | ||
<div> | ||
<ContextMenuTrigger id={newProps.id}> | ||
<SimpleRowsContainer {...this.props} /> | ||
</ContextMenuTrigger> | ||
{contextMenu} | ||
</div> | ||
); | ||
} | ||
|
||
render() { | ||
|
@@ -52,6 +57,7 @@ class RowsContainer extends React.Component { | |
|
||
RowsContainer.propTypes = { | ||
contextMenu: PropTypes.element, | ||
contextMenuId: PropTypes.string, | ||
rowIdx: PropTypes.number, | ||
idx: PropTypes.number, | ||
window: PropTypes.object | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,67 @@ | ||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
import ReactTestUtils from 'react-addons-test-utils'; | ||
import RowsContainer from '../RowsContainer'; | ||
import RowsContainer, { getNewContextMenuProps, SimpleRowsContainer, DEFAULT_CONTEXT_MENU_ID } from '../RowsContainer'; | ||
|
||
const FakeContextMenuTrigger = () => <div id="fakeContextMenuTrigger" />; | ||
|
||
const FakeContextMenu = () => <div id="fakeContextMenu" />; | ||
|
||
const ReactDataGridPlugins = { | ||
Menu: { | ||
ContextMenuTrigger: FakeContextMenuTrigger | ||
} | ||
}; | ||
|
||
const props = { | ||
contextMenu: <FakeContextMenu />, | ||
contextMenuId: 'fakeContextMenu', | ||
rowIdx: 5, | ||
idx: 8, | ||
window: { ReactDataGridPlugins }, | ||
rows: [ | ||
{ id: 'row_1' }, | ||
{ id: 'row_2' } | ||
] | ||
}; | ||
|
||
describe('Rows Container', () => { | ||
describe('without context menu', () => { | ||
let componentWithoutContextMenu = {}; | ||
describe('getNewContextMenuProps()', () => { | ||
it('should populate correct newProps for contextMenu with customized menu id', () => { | ||
const newProps = getNewContextMenuProps(props); | ||
expect(newProps.id).toBe(props.contextMenuId); | ||
}); | ||
|
||
beforeEach(() => { | ||
componentWithoutContextMenu = ReactTestUtils.renderIntoDocument(<RowsContainer window={{ ReactDataGridPlugins: undefined }} />); | ||
it('should populate correct newProps for contextMenu with default menu id', () => { | ||
const newProps = getNewContextMenuProps(Object.assign({}, props, { contextMenuId: undefined })); | ||
expect(newProps.id).toBe(DEFAULT_CONTEXT_MENU_ID); | ||
}); | ||
}); | ||
|
||
describe('with context menu', () => { | ||
it('should create a new RowsContainer instance', () => { | ||
expect(componentWithoutContextMenu).toBeDefined(); | ||
const wrapper = shallow(<RowsContainer {...props} />); | ||
expect(wrapper.find(FakeContextMenuTrigger).length).toBe(1); | ||
}); | ||
|
||
it('should throw exception for no context menu plugin', () => { | ||
const newProp = Object.assign({}, props, { window: {}}); | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
shallow(<RowsContainer {...newProp} />); | ||
} catch (e) { | ||
expect(e.message).toContain('You need to include ReactDataGrid UiPlugins in order to initialise context menu'); | ||
} | ||
}); | ||
}); | ||
|
||
describe('without context menu', () => { | ||
it('should create a SimpleRowsContainer', () => { | ||
const newProps = Object.assign({}, props, { | ||
contextMenu: undefined, | ||
contextMenuId: undefined | ||
}); | ||
const wrapper = shallow(<RowsContainer {...newProps}/>); | ||
expect(wrapper.find(SimpleRowsContainer).length).toBe(1); | ||
expect(wrapper.find(FakeContextMenuTrigger).length).toBe(0); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does grid need to specify
contextMenuId
prop.id
can be specified while creatingcontextMenu
element so the context menu example can be written asThis way
contextMenuId
prop does not need to pass down fromReactDataGrid
toRowsContainer
Thoughts?