Ver Fonte

Merge pull request #1756 from yulon/dev-pr1

Added letter keys filter
Evan You há 10 anos atrás
pai
commit
4e0974adf0
2 ficheiros alterados com 31 adições e 4 exclusões
  1. 10 4
      src/directives/public/on.js
  2. 21 0
      test/unit/specs/directives/public/on_spec.js

+ 10 - 4
src/directives/public/on.js

@@ -15,11 +15,17 @@ var keyCodes = {
 
 function keyFilter (handler, keys) {
   var codes = keys.map(function (key) {
-    var code = keyCodes[key]
-    if (!code) {
-      code = parseInt(key, 10)
+    var charCode = key.charCodeAt(0)
+    if (charCode > 47 && charCode < 58) {
+      return parseInt(key, 10)
     }
-    return code
+    if (key.length === 1) {
+      charCode = key.toUpperCase().charCodeAt(0)
+      if (charCode > 64 && charCode < 91) {
+        return charCode
+      }
+    }
+    return keyCodes[key]
   })
   return function keyHandler (e) {
     if (codes.indexOf(e.keyCode) > -1) {

+ 21 - 0
test/unit/specs/directives/public/on_spec.js

@@ -110,6 +110,27 @@ if (_.inBrowser) {
       })
     })
 
+    it('with key modifier (letter)', function (done) {
+      new Vue({
+        el: el,
+        template: '<a v-on:keyup.a="test">{{a}}</a>',
+        data: {a: 1},
+        methods: {
+          test: function () {
+            this.a++
+          }
+        }
+      })
+      var a = el.firstChild
+      trigger(a, 'keyup', function (e) {
+        e.keyCode = 65
+      })
+      _.nextTick(function () {
+        expect(a.textContent).toBe('2')
+        done()
+      })
+    })
+
     it('stop modifier', function () {
       var outer = jasmine.createSpy('outer')
       var inner = jasmine.createSpy('inner')