Skip to content

Commit

Permalink
[fixed] Respect onClick on MenuItem
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Oct 16, 2015
1 parent 44182b7 commit 8612b91
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/MenuItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import classnames from 'classnames';
import React from 'react';
import all from 'react-prop-types/lib/all';

import SafeAnchor from './SafeAnchor';
import createChainedFunction from './utils/createChainedFunction';

export default class MenuItem extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -35,27 +37,25 @@ export default class MenuItem extends React.Component {
);
}

const {className, style, href, onClick, ...props} = this.props;

const classes = {
disabled: this.props.disabled,
active: this.props.active
};

return (
<li role="presentation"
className={classnames(this.props.className, classes)}
style={this.props.style}
className={classnames(className, classes)}
style={style}
>
<SafeAnchor
{...props}
role="menuitem"
tabIndex="-1"
id={this.props.id}
target={this.props.target}
title={this.props.title}
href={this.props.href || ''}
onKeyDown={this.props.onKeyDown}
onClick={this.handleClick}>
{this.props.children}
</SafeAnchor>
href={href || ''}
onClick={createChainedFunction(onClick, this.handleClick)}
/>
</li>
);
}
Expand All @@ -80,6 +80,7 @@ MenuItem.propTypes = {
href: React.PropTypes.string,
target: React.PropTypes.string,
title: React.PropTypes.string,
onClick: React.PropTypes.func,
onKeyDown: React.PropTypes.func,
onSelect: React.PropTypes.func,
id: React.PropTypes.oneOfType([
Expand Down
15 changes: 15 additions & 0 deletions test/MenuItemSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ describe('MenuItem', () => {
ReactTestUtils.Simulate.click(anchor);
});

it('should call custom onClick', () => {
const handleClick = sinon.spy();
const handleSelect = sinon.spy();

const instance = ReactTestUtils.renderIntoDocument(
<MenuItem onClick={handleClick} onSelect={handleSelect}>Item</MenuItem>
);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');

ReactTestUtils.Simulate.click(anchor);

expect(handleClick).to.have.been.called;
expect(handleSelect).to.have.been.called;
});

it('does not fire onSelect when divider is clicked', () => {
const handleSelect = () => {
throw new Error('Should not invoke onSelect with divider flag applied');
Expand Down

0 comments on commit 8612b91

Please sign in to comment.