File tree Expand file tree Collapse file tree 8 files changed +58
-7
lines changed Expand file tree Collapse file tree 8 files changed +58
-7
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ declare interface Component {
5757 _isBeingDestroyed: boolean ;
5858 _vnode: ?VNode ;
5959 _staticTrees: ?Array < VNode > ;
60+ _keyCode: ( key : string ) => ?number ;
6061
6162 // private methods
6263 // lifecycle
Original file line number Diff line number Diff line change @@ -53,10 +53,13 @@ function genHandler (
5353}
5454
5555function genKeyFilter ( key : string ) : string {
56- const code = keyCodes [ key ] || JSON . stringify ( key )
56+ const code =
57+ parseInt ( key , 10 ) || // number keyCode
58+ keyCodes [ key ] || // built-in alias
59+ `_keyCode(${ JSON . stringify ( key ) } )` // custom alias
5760 if ( Array . isArray ( code ) ) {
58- return `if(${ code . map ( c => `$event.keyCode!=${ c } ` ) . join ( '&&' ) } )return;`
61+ return `if(${ code . map ( c => `$event.keyCode!== ${ c } ` ) . join ( '&&' ) } )return;`
5962 } else {
60- return `if($event.keyCode!=${ code } )return;`
63+ return `if($event.keyCode!== ${ code } )return;`
6164 }
6265}
Original file line number Diff line number Diff line change 33import { no } from 'shared/util'
44
55export type Config = {
6+ // user
67 optionMergeStrategies : { [ key : string ] : Function } ,
78 silent : boolean ,
89 errorHandler : ?Function ,
910 ignoredElements : ?Array < string > ,
11+ keyCodes : { [ key : string ] : number } ,
12+ // platform
1013 isReservedTag : ( x ? : string ) => boolean ,
1114 isUnknownElement : ( x ? : string ) => boolean ,
1215 mustUseProp : ( x ? : string ) => boolean ,
16+ // internal
1317 _assetTypes : Array < string > ,
1418 _lifecycleHooks : Array < string > ,
1519 _maxUpdateCount : number ,
@@ -38,6 +42,11 @@ const config: Config = {
3842 */
3943 ignoredElements : null ,
4044
45+ /**
46+ * Custom user key aliases for v-on
47+ */
48+ keyCodes : Object . create ( null ) ,
49+
4150 /**
4251 * Check if a tag is reserved so that it cannot be registered as a
4352 * component. This is platform-dependent and may be overwritten.
Original file line number Diff line number Diff line change @@ -10,7 +10,17 @@ import { set, del } from '../observer/index'
1010import builtInComponents from '../components/index'
1111
1212export function initGlobalAPI ( Vue : GlobalAPI ) {
13- Vue . config = config
13+ // config
14+ const configDef = { }
15+ configDef . get = ( ) => config
16+ if ( process . env . NODE_ENV !== 'production' ) {
17+ configDef . set = ( ) => {
18+ util . warn (
19+ 'Do not replace the Vue.config object, set individual fields instead.'
20+ )
21+ }
22+ }
23+ Object . defineProperty ( Vue , 'config' , configDef )
1424 Vue . util = util
1525 Vue . set = set
1626 Vue . delete = del
Original file line number Diff line number Diff line change @@ -152,6 +152,9 @@ export function renderMixin (Vue: Class<Component>) {
152152 }
153153 }
154154 }
155+
156+ // expose v-on keyCodes
157+ Vue . prototype . _keyCode = key => config . keyCodes [ key ]
155158}
156159
157160function resolveSlots (
Original file line number Diff line number Diff line change @@ -126,6 +126,19 @@ describe('Directive v-on', () => {
126126 expect ( spy ) . toHaveBeenCalled ( )
127127 } )
128128
129+ it ( 'should support custom keyCode' , ( ) => {
130+ Vue . config . keyCodes . test = 1
131+ vm = new Vue ( {
132+ el,
133+ template : `<input @keyup.test="foo">` ,
134+ methods : { foo : spy }
135+ } )
136+ triggerEvent ( vm . $el , 'keyup' , e => {
137+ e . keyCode = 1
138+ } )
139+ expect ( spy ) . toHaveBeenCalled ( )
140+ } )
141+
129142 it ( 'should bind to a child component' , ( ) => {
130143 Vue . component ( 'bar' , {
131144 template : '<span>Hello</span>'
Original file line number Diff line number Diff line change 11import Vue from 'vue'
22
33describe ( 'Global config' , ( ) => {
4+ it ( 'should warn replacing config object' , ( ) => {
5+ const originalConfig = Vue . config
6+ Vue . config = { }
7+ expect ( Vue . config ) . toBe ( originalConfig )
8+ expect ( 'Do not replace the Vue.config object' ) . toHaveBeenWarned ( )
9+ } )
10+
411 describe ( 'silent' , ( ) => {
512 it ( 'should be false by default' , ( ) => {
613 Vue . util . warn ( 'foo' )
Original file line number Diff line number Diff line change @@ -216,17 +216,22 @@ describe('codegen', () => {
216216 it ( 'generate events with keycode' , ( ) => {
217217 assertCodegen (
218218 '<input @input.enter="onInput">' ,
219- `with(this){return _h(_e('input',{on:{"input":function($event){if($event.keyCode!=13)return;onInput($event)}}}))}`
219+ `with(this){return _h(_e('input',{on:{"input":function($event){if($event.keyCode!== 13)return;onInput($event)}}}))}`
220220 )
221221 // multiple keycodes (delete)
222222 assertCodegen (
223223 '<input @input.delete="onInput">' ,
224- `with(this){return _h(_e('input',{on:{"input":function($event){if($event.keyCode!=8&&$event.keyCode!=46)return;onInput($event)}}}))}`
224+ `with(this){return _h(_e('input',{on:{"input":function($event){if($event.keyCode!== 8&&$event.keyCode!= =46)return;onInput($event)}}}))}`
225225 )
226226 // number keycode
227227 assertCodegen (
228228 '<input @input.13="onInput">' ,
229- `with(this){return _h(_e('input',{on:{"input":function($event){if($event.keyCode!="13")return;onInput($event)}}}))}`
229+ `with(this){return _h(_e('input',{on:{"input":function($event){if($event.keyCode!==13)return;onInput($event)}}}))}`
230+ )
231+ // custom keycode
232+ assertCodegen (
233+ '<input @input.custom="onInput">' ,
234+ `with(this){return _h(_e('input',{on:{"input":function($event){if($event.keyCode!==_keyCode("custom"))return;onInput($event)}}}))}`
230235 )
231236 } )
232237
You can’t perform that action at this time.
0 commit comments