1
1
import { hyphenate } from '@vue/shared'
2
-
3
- const systemModifiers = [ 'ctrl' , 'shift' , 'alt' , 'meta' ]
2
+ import { Directive } from 'test-dts'
4
3
5
4
type KeyedEvent = KeyboardEvent | MouseEvent | TouchEvent
6
5
7
- const modifierGuards : Record <
8
- string ,
9
- ( e : Event , modifiers : string [ ] ) => void | boolean
10
- > = {
11
- stop : e => e . stopPropagation ( ) ,
12
- prevent : e => e . preventDefault ( ) ,
13
- self : e => e . target !== e . currentTarget ,
14
- ctrl : e => ! ( e as KeyedEvent ) . ctrlKey ,
15
- shift : e => ! ( e as KeyedEvent ) . shiftKey ,
16
- alt : e => ! ( e as KeyedEvent ) . altKey ,
17
- meta : e => ! ( e as KeyedEvent ) . metaKey ,
18
- left : e => 'button' in e && ( e as MouseEvent ) . button !== 0 ,
19
- middle : e => 'button' in e && ( e as MouseEvent ) . button !== 1 ,
20
- right : e => 'button' in e && ( e as MouseEvent ) . button !== 2 ,
21
- exact : ( e , modifiers ) =>
6
+ type SystemModifiers = 'ctrl' | 'shift' | 'alt' | 'meta'
7
+ type CompatModifiers = keyof typeof keyNames
8
+
9
+ export type VOnModifiers = SystemModifiers | ModifierGuards | CompatModifiers
10
+
11
+ const systemModifiers : Array < SystemModifiers > = [ 'ctrl' , 'shift' , 'alt' , 'meta' ]
12
+
13
+ const modifierGuards = {
14
+ stop : ( e : Event ) => e . stopPropagation ( ) ,
15
+ prevent : ( e : Event ) => e . preventDefault ( ) ,
16
+ self : ( e : Event ) => e . target !== e . currentTarget ,
17
+ ctrl : ( e : Event ) => ! ( e as KeyedEvent ) . ctrlKey ,
18
+ shift : ( e : Event ) => ! ( e as KeyedEvent ) . shiftKey ,
19
+ alt : ( e : Event ) => ! ( e as KeyedEvent ) . altKey ,
20
+ meta : ( e : Event ) => ! ( e as KeyedEvent ) . metaKey ,
21
+ left : ( e : Event ) => 'button' in e && ( e as MouseEvent ) . button !== 0 ,
22
+ middle : ( e : Event ) => 'button' in e && ( e as MouseEvent ) . button !== 1 ,
23
+ right : ( e : Event ) => 'button' in e && ( e as MouseEvent ) . button !== 2 ,
24
+ exact : ( e : Event , modifiers : string [ ] ) =>
22
25
systemModifiers . some ( m => ( e as any ) [ `${ m } Key` ] && ! modifiers . includes ( m ) )
23
26
}
24
27
28
+ type ModifierGuards = keyof typeof modifierGuards
29
+
25
30
/**
26
31
* @private
27
32
*/
28
- export const withModifiers = ( fn : Function , modifiers : string [ ] ) => {
33
+ export const withModifiers = (
34
+ fn : Function ,
35
+ modifiers : Array < ModifierGuards | SystemModifiers >
36
+ ) => {
29
37
return ( event : Event , ...args : unknown [ ] ) => {
30
38
for ( let i = 0 ; i < modifiers . length ; i ++ ) {
31
39
const guard = modifierGuards [ modifiers [ i ] ]
@@ -37,7 +45,7 @@ export const withModifiers = (fn: Function, modifiers: string[]) => {
37
45
38
46
// Kept for 2.x compat.
39
47
// Note: IE11 compat for `spacebar` and `del` is removed for now.
40
- const keyNames : Record < string , string | string [ ] > = {
48
+ const keyNames = {
41
49
esc : 'escape' ,
42
50
space : ' ' ,
43
51
up : 'arrow-up' ,
@@ -56,10 +64,14 @@ export const withKeys = (fn: Function, modifiers: string[]) => {
56
64
const eventKey = hyphenate ( event . key )
57
65
if (
58
66
// None of the provided key modifiers match the current event key
59
- ! modifiers . some ( k => k === eventKey || keyNames [ k ] === eventKey )
67
+ ! modifiers . some (
68
+ k => k === eventKey || keyNames [ k as CompatModifiers ] === eventKey
69
+ )
60
70
) {
61
71
return
62
72
}
63
73
return fn ( event )
64
74
}
65
75
}
76
+
77
+ export type VOnDirective = Directive < any , any , Modifiers >
0 commit comments