Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6947f7

Browse files
authored
fix(useMagicKeys): fix key order issue on first use (#4505)
1 parent d08eb2c commit b6947f7

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { beforeEach, describe, expect, it } from 'vitest'
2+
import { useMagicKeys } from '.'
3+
4+
describe('useMagicKeys', () => {
5+
let target: HTMLInputElement
6+
beforeEach(() => {
7+
target = document.createElement('input')
8+
})
9+
it('single key', async () => {
10+
const { A } = useMagicKeys({ target })
11+
12+
target.dispatchEvent(new KeyboardEvent('keydown', {
13+
key: 'A',
14+
}))
15+
16+
expect(A.value).toBe(true)
17+
})
18+
it('multiple keys', async () => {
19+
const { Ctrl_Shift_Period } = useMagicKeys({ target })
20+
expect(Ctrl_Shift_Period.value).toBe(false)
21+
22+
target.dispatchEvent(new KeyboardEvent('keydown', {
23+
key: 'control',
24+
ctrlKey: true,
25+
}))
26+
expect(Ctrl_Shift_Period.value).toBe(false)
27+
28+
target.dispatchEvent(new KeyboardEvent('keydown', {
29+
key: 'shift',
30+
ctrlKey: true,
31+
shiftKey: true,
32+
}))
33+
expect(Ctrl_Shift_Period.value).toBe(false)
34+
35+
target.dispatchEvent(new KeyboardEvent('keydown', {
36+
key: 'Period',
37+
ctrlKey: true,
38+
shiftKey: true,
39+
}))
40+
expect(Ctrl_Shift_Period.value).toBe(true)
41+
})
42+
it('multiple keys(in a different order)', async () => {
43+
const { Ctrl_Shift_Period } = useMagicKeys({ target })
44+
expect(Ctrl_Shift_Period.value).toBe(false)
45+
46+
target.dispatchEvent(new KeyboardEvent('keydown', {
47+
key: 'shift',
48+
shiftKey: true,
49+
}))
50+
expect(Ctrl_Shift_Period.value).toBe(false)
51+
52+
target.dispatchEvent(new KeyboardEvent('keydown', {
53+
key: 'control',
54+
ctrlKey: true,
55+
shiftKey: true,
56+
}))
57+
expect(Ctrl_Shift_Period.value).toBe(false)
58+
59+
target.dispatchEvent(new KeyboardEvent('keydown', {
60+
key: 'Period',
61+
ctrlKey: true,
62+
shiftKey: true,
63+
}))
64+
expect(Ctrl_Shift_Period.value).toBe(true)
65+
})
66+
})

packages/core/useMagicKeys/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export function useMagicKeys(options: UseMagicKeysOptions<boolean> = {}): any {
165165
if (!(prop in refs)) {
166166
if (/[+_-]/.test(prop)) {
167167
const keys = prop.split(/[+_-]/g).map(i => i.trim())
168-
refs[prop] = computed(() => keys.every(key => toValue(proxy[key])))
168+
refs[prop] = computed(() => keys.map(key => toValue(proxy[key])).every(Boolean))
169169
}
170170
else {
171171
refs[prop] = ref(false)

0 commit comments

Comments
 (0)