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(useKeyPress): event should be triggered when only one event and use exactMatch #2708

Open
wants to merge 1 commit 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
10 changes: 10 additions & 0 deletions packages/hooks/src/useKeyPress/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,14 @@ describe('useKeyPress ', () => {
hook2.unmount();
hook1.unmount();
});

it('test single special key when use exactMatch', async () => {
const { unmount } = renderHook(() => useKeyPress(['shift'], callback,{
events: ["keyup"],
exactMatch: true,
}));
fireEvent.keyDown(document, { key: 'shift', keyCode: 16 });
expect(callback.mock.calls.length).toBe(1);
unmount();
});
});
6 changes: 6 additions & 0 deletions packages/hooks/src/useKeyPress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ function genFilterKey(event: KeyboardEvent, keyFilter: KeyType, exactMatch: bool

// 字符串依次判断是否有组合键
const genArr = keyFilter.split('.');

// 如果只有一个则不需要判断是不是组合按键
if (genArr.length === 1){
return aliasKeyCodeMap[keyFilter.toLowerCase()]=== event.keyCode
}

let genLen = 0;

for (const key of genArr) {
Expand Down
Loading