From 692bb3a9262f7953eb06b54e054b941204abfac6 Mon Sep 17 00:00:00 2001 From: YangLGggggggggg <46318880+yangliguo7@users.noreply.github.com> Date: Thu, 26 Dec 2024 09:23:43 +0000 Subject: [PATCH] fix(useKeyPress): event should be triggered when only one event and use exactMatch --- .../hooks/src/useKeyPress/__tests__/index.test.tsx | 10 ++++++++++ packages/hooks/src/useKeyPress/index.ts | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/packages/hooks/src/useKeyPress/__tests__/index.test.tsx b/packages/hooks/src/useKeyPress/__tests__/index.test.tsx index 3a06846f8b..57dcbe4f61 100644 --- a/packages/hooks/src/useKeyPress/__tests__/index.test.tsx +++ b/packages/hooks/src/useKeyPress/__tests__/index.test.tsx @@ -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(); + }); }); diff --git a/packages/hooks/src/useKeyPress/index.ts b/packages/hooks/src/useKeyPress/index.ts index 9e804b4016..9fd42e135b 100644 --- a/packages/hooks/src/useKeyPress/index.ts +++ b/packages/hooks/src/useKeyPress/index.ts @@ -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) {