Skip to content

Commit 65e25ab

Browse files
LouisLau-artLouisLau-artLouisLau-artOrbisK
authored
fix(useMagicKeys): handle undefined key in keyboard events (#5225)
Co-authored-by: LouisLau-art <louis.shawn@qq.com> Co-authored-by: LouisLau-art <louislau.art@gmail.com> Co-authored-by: Robin <robin.kehl@singular-it.de>
1 parent 17ea288 commit 65e25ab

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/core/useMagicKeys/index.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,30 @@ describe('useMagicKeys', () => {
155155
expect(alt_tab.value).toBe(false)
156156
})
157157

158-
it('should handle empty key events without errors', async () => {
158+
it('should handle empty or undefined key events without errors', async () => {
159159
const { a } = useMagicKeys({ target })
160160

161+
// Test empty key
161162
expect(() => {
162163
target.dispatchEvent(new KeyboardEvent('keyup', {}))
163164
}).not.toThrow()
164165

166+
// Test empty string key
167+
expect(() => {
168+
target.dispatchEvent(new KeyboardEvent('keyup', { key: '' }))
169+
}).not.toThrow()
170+
165171
expect(a.value).toBe(false)
166172
})
173+
174+
it('should be robust when key is explicitly undefined', () => {
175+
useMagicKeys({ target })
176+
177+
const event = new KeyboardEvent('keyup', {})
178+
Object.defineProperty(event, 'key', { value: undefined })
179+
180+
expect(() => {
181+
target.dispatchEvent(event)
182+
}).not.toThrow()
183+
})
167184
})

packages/core/useMagicKeys/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ 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 === '')
143+
if (!key)
144144
return
145145

146146
// current set

0 commit comments

Comments
 (0)