From 1c8e2e88ed2d74a02178217b318564b73a096c18 Mon Sep 17 00:00:00 2001 From: JK Date: Thu, 8 Mar 2018 03:38:41 +0800 Subject: [PATCH] fix: support KeyboardEvent.key in built-in keyboard event modifiers (#7121) fix #6900 --- .../instance/render-helpers/check-keycodes.js | 31 +++++++++++++++---- test/unit/features/directives/on.spec.js | 29 +++++++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/core/instance/render-helpers/check-keycodes.js b/src/core/instance/render-helpers/check-keycodes.js index 8e77f29b9e4..27596fde8bf 100644 --- a/src/core/instance/render-helpers/check-keycodes.js +++ b/src/core/instance/render-helpers/check-keycodes.js @@ -3,6 +3,26 @@ import config from 'core/config' import { hyphenate } from 'shared/util' +const keyNames: { [key: string]: string | Array } = { + esc: 'Escape', + tab: 'Tab', + enter: 'Enter', + space: ' ', + up: 'ArrowUp', + left: 'ArrowLeft', + right: 'ArrowRight', + down: 'ArrowDown', + 'delete': ['Backspace', 'Delete'] +} + +function isKeyNotMatch (expect: T | Array, actual: T): boolean { + if (Array.isArray(expect)) { + return expect.indexOf(actual) === -1 + } else { + return expect !== actual + } +} + /** * Runtime helper for checking keyCodes from config. * exposed as Vue.prototype._k @@ -15,12 +35,11 @@ export function checkKeyCodes ( eventKeyName?: string ): ?boolean { const keyCodes = config.keyCodes[key] || builtInAlias - if (keyCodes) { - if (Array.isArray(keyCodes)) { - return keyCodes.indexOf(eventKeyCode) === -1 - } else { - return keyCodes !== eventKeyCode - } + const builtInName: string | Array = keyNames[key] + if (builtInName && keyCodes === builtInAlias && eventKeyName) { + return isKeyNotMatch(builtInName, eventKeyName) + } else if (keyCodes) { + return isKeyNotMatch(keyCodes, eventKeyCode) } else if (eventKeyName) { return hyphenate(eventKeyName) !== key } diff --git a/test/unit/features/directives/on.spec.js b/test/unit/features/directives/on.spec.js index 4ef3c699fe0..3973c41cb26 100644 --- a/test/unit/features/directives/on.spec.js +++ b/test/unit/features/directives/on.spec.js @@ -362,6 +362,35 @@ describe('Directive v-on', () => { expect(spyMiddle).toHaveBeenCalled() }) + it('should support KeyboardEvent.key for built in aliases', () => { + vm = new Vue({ + el, + template: ` +
+ + + + + +
+ `, + methods: { foo: spy } + }) + + triggerEvent(vm.$refs.enter, 'keyup', e => { e.key = 'Enter' }) + expect(spy.calls.count()).toBe(1) + triggerEvent(vm.$refs.space, 'keyup', e => { e.key = ' ' }) + expect(spy.calls.count()).toBe(2) + triggerEvent(vm.$refs.esc, 'keyup', e => { e.key = 'Escape' }) + expect(spy.calls.count()).toBe(3) + triggerEvent(vm.$refs.left, 'keyup', e => { e.key = 'ArrowLeft' }) + expect(spy.calls.count()).toBe(4) + triggerEvent(vm.$refs.delete, 'keyup', e => { e.key = 'Backspace' }) + expect(spy.calls.count()).toBe(5) + triggerEvent(vm.$refs.delete, 'keyup', e => { e.key = 'Delete' }) + expect(spy.calls.count()).toBe(6) + }) + it('should support custom keyCode', () => { Vue.config.keyCodes.test = 1 vm = new Vue({