const el = document.createElement('div')
const fn = jest.fn()
// <div @keyup.ctrl.esc="test"/>
- const nextValue = withKeys(withModifiers(fn, ['ctrl']), ['esc'])
+ const nextValue = withKeys(withModifiers(fn, ['ctrl']), [
+ 'esc',
+ 'arrow-left'
+ ])
patchEvent(el, 'keyup', null, nextValue, null)
+
triggerEvent(el, 'keyup', e => (e.key = 'a'))
expect(fn).not.toBeCalled()
+
triggerEvent(el, 'keyup', e => {
e.ctrlKey = false
e.key = 'esc'
})
expect(fn).not.toBeCalled()
+
triggerEvent(el, 'keyup', e => {
e.ctrlKey = true
e.key = 'Escape'
})
- expect(fn).toBeCalled()
+ expect(fn).toBeCalledTimes(1)
+
+ triggerEvent(el, 'keyup', e => {
+ e.ctrlKey = true
+ e.key = 'ArrowLeft'
+ })
+ expect(fn).toBeCalledTimes(2)
})
test('it should support "exact" modifier', () => {
+import { hyphenate } from '@vue/shared'
+
const systemModifiers = ['ctrl', 'shift', 'alt', 'meta']
type KeyedEvent = KeyboardEvent | MouseEvent | TouchEvent
const keyNames: Record<string, string | string[]> = {
esc: 'escape',
space: ' ',
- up: 'arrowup',
- left: 'arrowleft',
- right: 'arrowright',
- down: 'arrowdown',
+ up: 'arrow-up',
+ left: 'arrow-left',
+ right: 'arrow-right',
+ down: 'arrow-down',
delete: 'backspace'
}
export const withKeys = (fn: Function, modifiers: string[]) => {
return (event: KeyboardEvent) => {
if (!('key' in event)) return
- const eventKey = event.key.toLowerCase()
+ const eventKey = hyphenate(event.key)
if (
// None of the provided key modifiers match the current event key
!modifiers.some(k => k === eventKey || keyNames[k] === eventKey)