@@ -32,14 +32,20 @@ const modifierGuards: Record<
32
32
/**
33
33
* @private
34
34
*/
35
- export const withModifiers = ( fn : Function , modifiers : string [ ] ) => {
36
- return ( event : Event , ...args : unknown [ ] ) => {
37
- for ( let i = 0 ; i < modifiers . length ; i ++ ) {
38
- const guard = modifierGuards [ modifiers [ i ] ]
39
- if ( guard && guard ( event , modifiers ) ) return
40
- }
41
- return fn ( event , ...args )
42
- }
35
+ export const withModifiers = (
36
+ fn : Function & { _withMods ?: Function } ,
37
+ modifiers : string [ ]
38
+ ) => {
39
+ return (
40
+ fn . _withMods ||
41
+ ( fn . _withMods = ( event : Event , ...args : unknown [ ] ) => {
42
+ for ( let i = 0 ; i < modifiers . length ; i ++ ) {
43
+ const guard = modifierGuards [ modifiers [ i ] ]
44
+ if ( guard && guard ( event , modifiers ) ) return
45
+ }
46
+ return fn ( event , ...args )
47
+ } )
48
+ )
43
49
}
44
50
45
51
// Kept for 2.x compat.
@@ -57,7 +63,10 @@ const keyNames: Record<string, string | string[]> = {
57
63
/**
58
64
* @private
59
65
*/
60
- export const withKeys = ( fn : Function , modifiers : string [ ] ) => {
66
+ export const withKeys = (
67
+ fn : Function & { _withKeys ?: Function } ,
68
+ modifiers : string [ ]
69
+ ) => {
61
70
let globalKeyCodes : LegacyConfig [ 'keyCodes' ]
62
71
let instance : ComponentInternalInstance | null = null
63
72
if ( __COMPAT__ ) {
@@ -77,40 +86,43 @@ export const withKeys = (fn: Function, modifiers: string[]) => {
77
86
}
78
87
}
79
88
80
- return ( event : KeyboardEvent ) => {
81
- if ( ! ( 'key' in event ) ) {
82
- return
83
- }
84
-
85
- const eventKey = hyphenate ( event . key )
86
- if ( modifiers . some ( k => k === eventKey || keyNames [ k ] === eventKey ) ) {
87
- return fn ( event )
88
- }
89
+ return (
90
+ fn . _withKeys ||
91
+ ( fn . _withKeys = ( event : KeyboardEvent ) => {
92
+ if ( ! ( 'key' in event ) ) {
93
+ return
94
+ }
89
95
90
- if ( __COMPAT__ ) {
91
- const keyCode = String ( event . keyCode )
92
- if (
93
- compatUtils . isCompatEnabled (
94
- DeprecationTypes . V_ON_KEYCODE_MODIFIER ,
95
- instance
96
- ) &&
97
- modifiers . some ( mod => mod == keyCode )
98
- ) {
96
+ const eventKey = hyphenate ( event . key )
97
+ if ( modifiers . some ( k => k === eventKey || keyNames [ k ] === eventKey ) ) {
99
98
return fn ( event )
100
99
}
101
- if ( globalKeyCodes ) {
102
- for ( const mod of modifiers ) {
103
- const codes = globalKeyCodes [ mod ]
104
- if ( codes ) {
105
- const matches = isArray ( codes )
106
- ? codes . some ( code => String ( code ) === keyCode )
107
- : String ( codes ) === keyCode
108
- if ( matches ) {
109
- return fn ( event )
100
+
101
+ if ( __COMPAT__ ) {
102
+ const keyCode = String ( event . keyCode )
103
+ if (
104
+ compatUtils . isCompatEnabled (
105
+ DeprecationTypes . V_ON_KEYCODE_MODIFIER ,
106
+ instance
107
+ ) &&
108
+ modifiers . some ( mod => mod == keyCode )
109
+ ) {
110
+ return fn ( event )
111
+ }
112
+ if ( globalKeyCodes ) {
113
+ for ( const mod of modifiers ) {
114
+ const codes = globalKeyCodes [ mod ]
115
+ if ( codes ) {
116
+ const matches = isArray ( codes )
117
+ ? codes . some ( code => String ( code ) === keyCode )
118
+ : String ( codes ) === keyCode
119
+ if ( matches ) {
120
+ return fn ( event )
121
+ }
110
122
}
111
123
}
112
124
}
113
125
}
114
- }
115
- }
126
+ } )
127
+ )
116
128
}
0 commit comments