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

Commit

Permalink
Allow className property in MenuItem and extend it instead of overrid…
Browse files Browse the repository at this point in the history
…ing (#294)

* fix: linter errors and warnings

* feat: extend MenuItem className correctly

Closes #293
  • Loading branch information
wuppious authored and vkbansal committed Sep 3, 2019
1 parent 22d299e commit 2874903
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 27 deletions.
10 changes: 5 additions & 5 deletions src/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export default class ContextMenu extends AbstractMenu {
const { x, y } = this.state;

const { top, left } = this.props.rtl
? this.getRTLMenuPosition(x, y)
: this.getMenuPosition(x, y);
? this.getRTLMenuPosition(x, y)
: this.getMenuPosition(x, y);

wrapper(() => {
if (!this.menu) return;
Expand Down Expand Up @@ -229,9 +229,9 @@ export default class ContextMenu extends AbstractMenu {
const { children, className, style } = this.props;
const { isVisible } = this.state;
const inlineStyle = assign(
{},
style,
{ position: 'fixed', opacity: 0, pointerEvents: 'none' }
{},
style,
{ position: 'fixed', opacity: 0, pointerEvents: 'none' }
);
const menuClassnames = cx(cssClasses.menu, className, {
[cssClasses.menuVisible]: isVisible
Expand Down
47 changes: 31 additions & 16 deletions src/MenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ import { callIfExists, cssClasses, store } from './helpers';

export default class MenuItem extends Component {
static propTypes = {
children: PropTypes.node,
attributes: PropTypes.object,
children: PropTypes.node,
className: PropTypes.string,
data: PropTypes.object,
disabled: PropTypes.bool,
divider: PropTypes.bool,
preventClose: PropTypes.bool,
onClick: PropTypes.func,
selected: PropTypes.bool,
onMouseLeave: PropTypes.func,
onMouseMove: PropTypes.func,
onMouseLeave: PropTypes.func
preventClose: PropTypes.bool,
selected: PropTypes.bool
};

static defaultProps = {
disabled: false,
attributes: {},
children: null,
className: '',
data: {},
disabled: false,
divider: false,
attributes: {},
preventClose: false,
onClick() { return null; },
children: null,
selected: false,
onMouseMove: () => null,
onMouseLeave: () => null
onMouseLeave: () => null,
preventClose: false,
selected: false
};

handleClick = (event) => {
Expand All @@ -53,12 +55,25 @@ export default class MenuItem extends Component {
}

render() {
const { disabled, divider, children, attributes, selected } = this.props;
const menuItemClassNames = cx(cssClasses.menuItem, attributes.className, {
[cx(cssClasses.menuItemDisabled, attributes.disabledClassName)]: disabled,
[cx(cssClasses.menuItemDivider, attributes.dividerClassName)]: divider,
[cx(cssClasses.menuItemSelected, attributes.selectedClassName)]: selected
});
const {
attributes,
children,
className,
disabled,
divider,
selected
} = this.props;

const menuItemClassNames = cx(
className,
cssClasses.menuItem,
attributes.className,
{
[cx(cssClasses.menuItemDisabled, attributes.disabledClassName)]: disabled,
[cx(cssClasses.menuItemDivider, attributes.dividerClassName)]: divider,
[cx(cssClasses.menuItemSelected, attributes.selectedClassName)]: selected
}
);

return (
<div
Expand Down
4 changes: 2 additions & 2 deletions src/SubMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export default class SubMenu extends AbstractMenu {
const wrapper = window.requestAnimationFrame || setTimeout;
wrapper(() => {
const styles = this.props.rtl
? this.getRTLMenuPosition()
: this.getMenuPosition();
? this.getRTLMenuPosition()
: this.getMenuPosition();

this.subMenu.style.removeProperty('top');
this.subMenu.style.removeProperty('bottom');
Expand Down
4 changes: 2 additions & 2 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const MENU_HIDE = 'REACT_CONTEXTMENU_HIDE';


export function dispatchGlobalEvent(eventName, opts, target = window) {
// Compatibale with IE
// @see http://stackoverflow.com/questions/26596123/internet-explorer-9-10-11-event-constructor-doesnt-work
// Compatibale with IE
// @see http://stackoverflow.com/questions/26596123/internet-explorer-9-10-11-event-constructor-doesnt-work
let event;

if (typeof window.CustomEvent === 'function') {
Expand Down
2 changes: 1 addition & 1 deletion src/connectMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ignoredTriggerProps = [...Object.keys(ContextMenuTrigger.propTypes), 'chil
export default function (menuId) {
// expect menu component to connect as inner parameter
// <Child/> is presumably a wrapper of <ContextMenu/>
return function (Child) {
return function connect(Child) {
// return wrapper for <Child/> that forwards the ContextMenuTrigger's additional props
return class ConnectMenu extends Component {
constructor(props) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export const cssClasses = {
export const store = {};

export const canUseDOM = Boolean(
typeof window !== 'undefined' && window.document && window.document.createElement
typeof window !== 'undefined' && window.document && window.document.createElement
);
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare module "react-contextmenu" {

interface MenuItemProps {
attributes?: React.HTMLAttributes<HTMLDivElement>,
className?: string;
data?: Object,
disabled?: boolean,
divider?: boolean,
Expand Down
20 changes: 20 additions & 0 deletions tests/MenuItem.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import { shallow } from 'enzyme';

import MenuItem from '../src/MenuItem';

describe('MenuItem tests', () => {
test('extends className correctly', () => {
const className = 'CLASSNAME_PROP';
const attributes = {
className: 'CLASSNAME_ATTRIBUTE'
};

const wrapper = shallow(
<MenuItem className={className} attributes={attributes} />
);

expect(wrapper.hasClass(className)).toBe(true);
expect(wrapper.hasClass(attributes.className)).toBe(true);
});
});

0 comments on commit 2874903

Please sign in to comment.