Skip to content

Commit 66ad013

Browse files
committed
Prevent menu click/mousedown from blurring the menu (#579)
1 parent cc3d78f commit 66ad013

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/__tests__/components/Menu.test.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,32 @@ describe('<Menu>', () => {
9191
expect(scheduleUpdate).toHaveBeenCalledTimes(1);
9292
});
9393

94-
test('<Menu.Divider>', () => {
95-
const wrapper = shallow(<Menu.Divider />);
96-
97-
expect(wrapper.type()).toBe('div');
98-
expect(wrapper.hasClass('dropdown-divider')).toBe(true);
99-
expect(wrapper.prop('role')).toBe('separator');
94+
test('prevents the input from blurring on mousedown', () => {
95+
const e = { preventDefault: jest.fn() };
96+
menu.simulate('mousedown', e);
97+
expect(e.preventDefault).toHaveBeenCalledTimes(1);
10098
});
99+
});
101100

102-
test('<Menu.Header>', () => {
103-
const children = 'This is a menu header';
101+
test('<Menu.Divider>', () => {
102+
const wrapper = shallow(<Menu.Divider />);
104103

105-
const wrapper = shallow(
106-
<Menu.Header>
107-
{children}
108-
</Menu.Header>
109-
);
104+
expect(wrapper.type()).toBe('div');
105+
expect(wrapper.hasClass('dropdown-divider')).toBe(true);
106+
expect(wrapper.prop('role')).toBe('separator');
107+
});
110108

111-
expect(wrapper.type()).toBe('div');
112-
expect(wrapper.hasClass('dropdown-header')).toBe(true);
113-
expect(wrapper.prop('role')).toBe('heading');
114-
expect(wrapper.text()).toBe(children);
115-
});
109+
test('<Menu.Header>', () => {
110+
const children = 'This is a menu header';
111+
112+
const wrapper = shallow(
113+
<Menu.Header>
114+
{children}
115+
</Menu.Header>
116+
);
117+
118+
expect(wrapper.type()).toBe('div');
119+
expect(wrapper.hasClass('dropdown-header')).toBe(true);
120+
expect(wrapper.prop('role')).toBe('heading');
121+
expect(wrapper.text()).toBe(children);
116122
});

src/components/Menu.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React, { Children, type Node } from 'react';
66

77
import { BaseMenuItem } from './MenuItem';
88

9+
import { preventInputBlur } from '../utils';
910
import { checkPropType, isRequiredForA11y } from '../propTypes';
1011
import type { Id, MenuProps } from '../types';
1112

@@ -90,6 +91,7 @@ class Menu extends React.Component<MenuComponentProps> {
9091
children;
9192

9293
return (
94+
/* eslint-disable jsx-a11y/interactive-supports-focus */
9395
<div
9496
aria-label={this.props['aria-label']}
9597
className={cx('rbt-menu', 'dropdown-menu', 'show', className)}
@@ -99,6 +101,10 @@ class Menu extends React.Component<MenuComponentProps> {
99101
// positioning updates correctly.
100102
text
101103
}
104+
onMouseDown={
105+
// Prevent input from blurring when clicking on the menu scrollbar.
106+
preventInputBlur
107+
}
102108
ref={innerRef}
103109
role="listbox"
104110
style={{
@@ -109,6 +115,7 @@ class Menu extends React.Component<MenuComponentProps> {
109115
}}>
110116
{contents}
111117
</div>
118+
/* eslint-enable jsx-a11y/interactive-supports-focus */
112119
);
113120
}
114121
}

0 commit comments

Comments
 (0)