9
9
import { dispatchFakeEvent , dispatchKeyboardEvent } from './dispatch-events' ;
10
10
import { triggerFocus } from './element-focus' ;
11
11
12
+ /** Modifier keys that may be held while typing. */
13
+ export interface KeyModifiers {
14
+ control ?: boolean ;
15
+ alt ?: boolean ;
16
+ shift ?: boolean ;
17
+ meta ?: boolean ;
18
+ }
19
+
12
20
/**
13
21
* Represents a special key that does not result in a character being inputed in a text field.
14
22
* @docs -private
@@ -30,15 +38,40 @@ export function isTextInput(element: Element): element is HTMLInputElement | HTM
30
38
/**
31
39
* Focuses an input, sets its value and dispatches
32
40
* the `input` event, simulating the user typing.
33
- * @param value Value to be set on the input.
34
41
* @param element Element onto which to set the value.
42
+ * @param keys The keys to send to the element.
43
+ * @docs -private
44
+ */
45
+ export function typeInElement ( element : HTMLElement , ...keys : ( string | SpecialKey ) [ ] ) : void ;
46
+
47
+ /**
48
+ * Focuses an input, sets its value and dispatches
49
+ * the `input` event, simulating the user typing.
50
+ * @param element Element onto which to set the value.
51
+ * @param modifiers Modifier keys that are held while typing.
52
+ * @param keys The keys to send to the element.
35
53
* @docs -private
36
54
*/
37
- export function typeInElement ( element : HTMLElement , ...value : ( string | SpecialKey ) [ ] ) {
55
+ export function typeInElement (
56
+ element : HTMLElement , modifiers : KeyModifiers , ...keys : ( string | SpecialKey ) [ ] ) : void ;
57
+
58
+ export function typeInElement ( element : HTMLElement , ...modifiersAndKeys : any ) {
59
+ const first = modifiersAndKeys [ 0 ] ;
60
+ let modifiers : KeyModifiers ;
61
+ let keys : ( string | SpecialKey ) [ ] ;
62
+ if ( typeof first !== 'string' && first . keyCode === undefined ) {
63
+ modifiers = first ;
64
+ keys = modifiersAndKeys . slice ( 1 ) ;
65
+ } else {
66
+ modifiers = { } ;
67
+ keys = modifiersAndKeys ;
68
+ }
69
+
70
+ // TODO: pass through modifiers
38
71
triggerFocus ( element ) ;
39
- for ( const keys of value ) {
40
- if ( typeof keys === 'string' ) {
41
- for ( const key of keys ) {
72
+ for ( const keyOrStr of keys ) {
73
+ if ( typeof keyOrStr === 'string' ) {
74
+ for ( const key of keyOrStr ) {
42
75
const keyCode = key . charCodeAt ( 0 ) ;
43
76
dispatchKeyboardEvent ( element , 'keydown' , keyCode ) ;
44
77
dispatchKeyboardEvent ( element , 'keypress' , keyCode ) ;
@@ -49,9 +82,9 @@ export function typeInElement(element: HTMLElement, ...value: (string | SpecialK
49
82
dispatchKeyboardEvent ( element , 'keyup' , keyCode ) ;
50
83
}
51
84
} else {
52
- dispatchKeyboardEvent ( element , 'keydown' , keys . keyCode , keys . key ) ;
53
- dispatchKeyboardEvent ( element , 'keypress' , keys . keyCode , keys . key ) ;
54
- dispatchKeyboardEvent ( element , 'keyup' , keys . keyCode , keys . key ) ;
85
+ dispatchKeyboardEvent ( element , 'keydown' , keyOrStr . keyCode , keyOrStr . key ) ;
86
+ dispatchKeyboardEvent ( element , 'keypress' , keyOrStr . keyCode , keyOrStr . key ) ;
87
+ dispatchKeyboardEvent ( element , 'keyup' , keyOrStr . keyCode , keyOrStr . key ) ;
55
88
}
56
89
}
57
90
}
0 commit comments