Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: input in menu can not move cursor by arrow keys #434

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/hooks/useAccessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,11 @@ export default function useAccessibility<T extends HTMLElement>(
return;
}

// Arrow prevent default to avoid page scroll
if (ArrowKeys.includes(which) || [HOME, END].includes(which)) {
// Arrow prevent default to avoid page scroll, not apply for input and textarea
if (
(ArrowKeys.includes(which) || [HOME, END].includes(which)) &&
!['INPUT', 'TEXTAREA'].includes((e.target as Element).nodeName)
) {
e.preventDefault();
}

Expand Down
31 changes: 31 additions & 0 deletions tests/Menu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,37 @@ describe('Menu', () => {
expect(wrapper.isActive(1)).toBeTruthy();
});

it('input and textarea cursor can be moved by arrow', () => {
const wrapper = mount(
<Menu activeKey="1">
<MenuItem key="1">
<input value="123" />
</MenuItem>
<MenuItem key="2">
<textarea value="123" />
</MenuItem>
</Menu>,
);

const input = wrapper.find('input');
input.simulate('focus');
const inputNode = input.getDOMNode();
expect(inputNode.selectionStart).toEqual(0);
input.simulate('keyDown', { which: KeyCode.RIGHT });
expect(inputNode.selectionStart).toEqual(1);
input.simulate('keyDown', { which: KeyCode.LEFT });
expect(inputNode.selectionStart).toEqual(0);

const textarea = wrapper.find('textarea');
textarea.simulate('focus');
const textareaNode = textarea.getDOMNode();
expect(textarea.selectionStart).toEqual(0);
textareaNode.simulate('keyDown', { which: KeyCode.RIGHT });
expect(textarea.selectionStart).toEqual(1);
textareaNode.simulate('keyDown', { which: KeyCode.LEFT });
expect(textarea.selectionStart).toEqual(0);
});

it('defaultActiveFirst', () => {
const wrapper = mount(
<Menu selectedKeys={['foo']} defaultActiveFirst>
Expand Down