mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-02 12:07:39 +08:00
fix v-on chained keyCodes (fix #3478)
This commit is contained in:
parent
ba5fb31188
commit
13ce3a2559
@ -41,25 +41,40 @@ function genHandler (
|
||||
? handler.value
|
||||
: `function($event){${handler.value}}`
|
||||
} else {
|
||||
let code = 'function($event){'
|
||||
let code = ''
|
||||
const keys = []
|
||||
for (const key in handler.modifiers) {
|
||||
code += modifierCode[key] || genKeyFilter(key)
|
||||
if (modifierCode[key]) {
|
||||
code += modifierCode[key]
|
||||
} else {
|
||||
keys.push(key)
|
||||
}
|
||||
}
|
||||
if (keys.length) {
|
||||
code = genKeyFilter(keys) + code
|
||||
}
|
||||
const handlerCode = simplePathRE.test(handler.value)
|
||||
? handler.value + '($event)'
|
||||
: handler.value
|
||||
return code + handlerCode + '}'
|
||||
return 'function($event){' + code + handlerCode + '}'
|
||||
}
|
||||
}
|
||||
|
||||
function genKeyFilter (key: string): string {
|
||||
const code =
|
||||
parseInt(key, 10) || // number keyCode
|
||||
keyCodes[key] || // built-in alias
|
||||
`_k(${JSON.stringify(key)})` // custom alias
|
||||
function genKeyFilter (keys: Array<string>): string {
|
||||
const code = keys.length === 1
|
||||
? normalizeKeyCode(keys[0])
|
||||
: Array.prototype.concat.apply([], keys.map(normalizeKeyCode))
|
||||
if (Array.isArray(code)) {
|
||||
return `if(${code.map(c => `$event.keyCode!==${c}`).join('&&')})return;`
|
||||
} else {
|
||||
return `if($event.keyCode!==${code})return;`
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeKeyCode (key) {
|
||||
return (
|
||||
parseInt(key, 10) || // number keyCode
|
||||
keyCodes[key] || // built-in alias
|
||||
`_k(${JSON.stringify(key)})` // custom alias
|
||||
)
|
||||
}
|
||||
|
@ -191,6 +191,11 @@ describe('codegen', () => {
|
||||
'<input @input.delete="onInput">',
|
||||
`with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}})}`
|
||||
)
|
||||
// multiple keycodes (chained)
|
||||
assertCodegen(
|
||||
'<input @keydown.enter.delete="onInput">',
|
||||
`with(this){return _h('input',{on:{"keydown":function($event){if($event.keyCode!==13&&$event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}})}`
|
||||
)
|
||||
// number keycode
|
||||
assertCodegen(
|
||||
'<input @input.13="onInput">',
|
||||
|
Loading…
Reference in New Issue
Block a user