Skip to content

Commit f8aecd8

Browse files
babu-ch9romise
andauthored
fix(useMagicKeys): handle empty key events to prevent errors (#5149)
Co-authored-by: Vida Xie <[email protected]>
1 parent ef0c4f8 commit f8aecd8

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

packages/core/useMagicKeys/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,14 @@ describe('useMagicKeys', () => {
154154
window.dispatchEvent(new Event('focus'))
155155
expect(alt_tab.value).toBe(false)
156156
})
157+
158+
it('should handle empty key events without errors', async () => {
159+
const { a } = useMagicKeys({ target })
160+
161+
expect(() => {
162+
target.dispatchEvent(new KeyboardEvent('keyup', {}))
163+
}).not.toThrow()
164+
165+
expect(a.value).toBe(false)
166+
})
157167
})

packages/core/useMagicKeys/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ export function useMagicKeys<T extends boolean = false>(options: UseMagicKeysOpt
140140
const code = e.code?.toLowerCase()
141141
const values = [code, key].filter(Boolean)
142142

143+
if (key === '')
144+
return
145+
143146
// current set
144147
if (key) {
145148
if (value)

0 commit comments

Comments
 (0)