Skip to content

Commit

Permalink
Merge pull request #1422 from skbkontur/fix-1015
Browse files Browse the repository at this point in the history
fix(MenuItem): fix triggered `MouseEnter` from disabled button
  • Loading branch information
lossir authored May 24, 2019
2 parents 95c342a + 2fdd520 commit 4c39be7
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/react-ui-screenshot-tests/gemini/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ gemini.suite('Menu', wrapperSuite => {
gemini.suite('without Shadow', suite => {
applyTest(renderStory('Menu', 'without Shadow'), suite);
});

gemini.suite('with disabled MenuItem', suite => {
suite
.before(renderStory('Menu', 'with disabled MenuItem'))
.setCaptureElements(TEST_CONTAINER)
.capture('mouseenter', (actions, find) => {
actions.click(find('[data-tid="menuitem-notdisabled"]'));
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ storiesOf('Menu', module)
<MenuItem>MenuItem2</MenuItem>
<MenuItem>MenuItem3</MenuItem>
</Menu>
))
.add('with disabled MenuItem', () => (
<Menu hasShadow={false}>
<MenuItem disabled>MenuItem1</MenuItem>
<MenuItem data-tid="menuitem-notdisabled">MenuItem2</MenuItem>
</Menu>
));

class MoveControls extends React.Component {
Expand Down
28 changes: 27 additions & 1 deletion packages/retail-ui/components/MenuItem/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export default class MenuItem extends React.Component<MenuItemProps> {
onClick: PropTypes.func,
};

private mouseEntered: boolean = false;

public render() {
const {
alkoLink,
Expand All @@ -73,6 +75,8 @@ export default class MenuItem extends React.Component<MenuItemProps> {
children,
_enableIconPadding,
component,
onMouseEnter,
onMouseLeave,
...rest
} = this.props;

Expand Down Expand Up @@ -103,7 +107,13 @@ export default class MenuItem extends React.Component<MenuItemProps> {
const Component = this.getComponent();

return (
<Component {...rest} className={className} tabIndex={-1}>
<Component
{...rest}
onMouseOver={this.handleMouseEnterFix}
onMouseLeave={this.handleMouseLeave}
className={className}
tabIndex={-1}
>
{iconElement}
{content}
{this.props.comment && (
Expand All @@ -120,6 +130,22 @@ export default class MenuItem extends React.Component<MenuItemProps> {
);
}

// https://github.com/facebook/react/issues/10109
// Mouseenter event not triggered when cursor moves from disabled button
private handleMouseEnterFix = (e: React.MouseEvent<HTMLElement>) => {
if (!this.mouseEntered && this.props.onMouseEnter) {
this.mouseEntered = true;
this.props.onMouseEnter(e);
}
};

private handleMouseLeave = (e: React.MouseEvent<HTMLElement>) => {
this.mouseEntered = false;
if (this.props.onMouseLeave) {
this.props.onMouseLeave(e);
}
};

private getComponent = () => {
const { disabled, component, href } = this.props;

Expand Down
31 changes: 31 additions & 0 deletions packages/retail-ui/components/MenuItem/__tests__/MenuItem-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,35 @@ describe('MenuItem', () => {

expect(wrapper.contains(<span>http:test.href</span>)).toEqual(true);
});

describe('onMouseEnter', () => {
it('calls once', () => {
const onMouseEnter = jest.fn();
const wrapper = mount(
<MenuItem onMouseEnter={onMouseEnter}>
<span>MenuItem</span>
</MenuItem>,
);
const button = wrapper.find('button');

button.simulate('mouseover');
wrapper.find('span').simulate('mouseover');
button.simulate('mouseover');

expect(onMouseEnter.mock.calls.length).toBe(1);
});

it('calls again after onMouseLeave', () => {
const onMouseEnter = jest.fn();
const wrapper = mount(<MenuItem onMouseEnter={onMouseEnter}>MenuItem</MenuItem>);

wrapper
.find('button')
.simulate('mouseover')
.simulate('mouseleave')
.simulate('mouseover');

expect(onMouseEnter.mock.calls.length).toBe(2);
});
});
});

0 comments on commit 4c39be7

Please sign in to comment.