Skip to content

Commit

Permalink
Merge pull request #140 from react-component/fix-click
Browse files Browse the repository at this point in the history
fix: Menu[onClick] should not be triggered by propagated onClick event
  • Loading branch information
picodoth committed May 7, 2018
2 parents 8c23c8c + b24f77b commit 7bd1e63
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
6 changes: 3 additions & 3 deletions examples/antd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import Menu, { SubMenu, Item as MenuItem, Divider } from 'rc-menu';
import 'rc-menu/assets/index.less';
import animate from 'css-animation';

function handleSelect(info) {
function handleClick(info) {
console.log(`clicked ${info.key}`);
console.log(info);
console.log(`selected ${info.key}`);
}

const animation = {
Expand Down Expand Up @@ -77,7 +77,7 @@ const nestSubMenu = (<SubMenu title={<span>offset sub menu 2</span>} key="4" pop
function onOpenChange(value) {
console.log('onOpenChange', value);
}
const commonMenu = (<Menu onSelect={handleSelect} onOpenChange={onOpenChange}>
const commonMenu = (<Menu onClick={handleClick} onOpenChange={onOpenChange}>
<SubMenu title={<span>sub menu</span>} key="1">
<MenuItem key="1-1">0-1</MenuItem>
<MenuItem key="1-2">0-2</MenuItem>
Expand Down
14 changes: 6 additions & 8 deletions src/MenuItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,12 @@ export class MenuItem extends React.Component {
// <li><a role='menuitem'>Link</a></li> would be a good example
delete attrs.role;
}
let mouseEvent = {};
if (!props.disabled) {
mouseEvent = {
onClick: this.onClick,
onMouseLeave: this.onMouseLeave,
onMouseEnter: this.onMouseEnter,
};
}
// In case that onClick/onMouseLeave/onMouseEnter is passed down from owner
const mouseEvent = {
onClick: props.disabled ? null : this.onClick,
onMouseLeave: props.disabled ? null : this.onMouseLeave,
onMouseEnter: props.disabled ? null : this.onMouseEnter,
};
const style = {
...props.style,
};
Expand Down
10 changes: 9 additions & 1 deletion src/SubMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,16 @@ export class SubMenu extends React.Component {
subMenuCloseDelay,
} = props;
menuAllProps.forEach(key => delete props[key]);
// Set onClick to null, to ignore propagated onClick event
delete props.onClick;

return (
<li {...props} {...mouseEvents} className={className} role="menuitem">
<li
{...props}
{...mouseEvents}
className={className}
role="menuitem"
>
{isInlineMode && title}
{isInlineMode && children}
{!isInlineMode && (
Expand Down
3 changes: 3 additions & 0 deletions src/SubPopupMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ export class SubPopupMenu extends React.Component {
}
const { prefixCls, eventKey, visible } = props;
menuAllProps.forEach(key => delete props[key]);

// Otherwise, the propagated click event will trigger another onClick
delete props.onClick;
return (
// ESLint is not smart enough to know that the type of `children` was checked.
/* eslint-disable */
Expand Down
4 changes: 1 addition & 3 deletions tests/MenuItem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ describe('MenuItem', () => {
expect(wrapper.render()).toMatchSnapshot();
wrapper.find('MenuItem').at(0).simulate('click');
expect(onClick).toHaveBeenCalledTimes(1);
wrapper.find('SubMenu').at(0).simulate('click');
expect(onClick).toHaveBeenCalledTimes(2);
wrapper.find('MenuItemGroup').at(0).simulate('click');
expect(onClick).toHaveBeenCalledTimes(3);
expect(onClick).toHaveBeenCalledTimes(2);
});
});

Expand Down

0 comments on commit 7bd1e63

Please sign in to comment.