Skip to content

Commit 8612b91

Browse files
committed
[fixed] Respect onClick on MenuItem
1 parent 44182b7 commit 8612b91

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/MenuItem.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import React from 'react';
21
import classnames from 'classnames';
2+
import React from 'react';
33
import all from 'react-prop-types/lib/all';
4+
45
import SafeAnchor from './SafeAnchor';
6+
import createChainedFunction from './utils/createChainedFunction';
57

68
export default class MenuItem extends React.Component {
79
constructor(props) {
@@ -35,27 +37,25 @@ export default class MenuItem extends React.Component {
3537
);
3638
}
3739

40+
const {className, style, href, onClick, ...props} = this.props;
41+
3842
const classes = {
3943
disabled: this.props.disabled,
4044
active: this.props.active
4145
};
4246

4347
return (
4448
<li role="presentation"
45-
className={classnames(this.props.className, classes)}
46-
style={this.props.style}
49+
className={classnames(className, classes)}
50+
style={style}
4751
>
4852
<SafeAnchor
53+
{...props}
4954
role="menuitem"
5055
tabIndex="-1"
51-
id={this.props.id}
52-
target={this.props.target}
53-
title={this.props.title}
54-
href={this.props.href || ''}
55-
onKeyDown={this.props.onKeyDown}
56-
onClick={this.handleClick}>
57-
{this.props.children}
58-
</SafeAnchor>
56+
href={href || ''}
57+
onClick={createChainedFunction(onClick, this.handleClick)}
58+
/>
5959
</li>
6060
);
6161
}
@@ -80,6 +80,7 @@ MenuItem.propTypes = {
8080
href: React.PropTypes.string,
8181
target: React.PropTypes.string,
8282
title: React.PropTypes.string,
83+
onClick: React.PropTypes.func,
8384
onKeyDown: React.PropTypes.func,
8485
onSelect: React.PropTypes.func,
8586
id: React.PropTypes.oneOfType([

test/MenuItemSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ describe('MenuItem', () => {
8282
ReactTestUtils.Simulate.click(anchor);
8383
});
8484

85+
it('should call custom onClick', () => {
86+
const handleClick = sinon.spy();
87+
const handleSelect = sinon.spy();
88+
89+
const instance = ReactTestUtils.renderIntoDocument(
90+
<MenuItem onClick={handleClick} onSelect={handleSelect}>Item</MenuItem>
91+
);
92+
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
93+
94+
ReactTestUtils.Simulate.click(anchor);
95+
96+
expect(handleClick).to.have.been.called;
97+
expect(handleSelect).to.have.been.called;
98+
});
99+
85100
it('does not fire onSelect when divider is clicked', () => {
86101
const handleSelect = () => {
87102
throw new Error('Should not invoke onSelect with divider flag applied');

0 commit comments

Comments
 (0)