functional.js
4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.preKeyupBehavior = exports.preKeydownBehavior = exports.postKeyupBehavior = exports.keyupBehavior = exports.keypressBehavior = exports.keydownBehavior = void 0;
var _dom = require("@testing-library/dom");
var _utils = require("../../utils");
var _getEventProps = require("../getEventProps");
var _shared = require("../shared");
/**
* This file should contain behavior for functional keys as described here:
* https://w3c.github.io/uievents-code/#key-alphanumeric-functional
*/
const modifierKeys = {
Alt: 'alt',
Control: 'ctrl',
Shift: 'shift',
Meta: 'meta'
};
const preKeydownBehavior = [// modifierKeys switch on the modifier BEFORE the keydown event
...Object.entries(modifierKeys).map(([key, modKey]) => ({
matches: keyDef => keyDef.key === key,
handle: (keyDef, element, options, state) => {
state.modifiers[modKey] = true;
}
})), // AltGraph produces an extra keydown for Control
// The modifier does not change
{
matches: keyDef => keyDef.key === 'AltGraph',
handle: (keyDef, element, options, state) => {
var _options$keyboardMap$;
const ctrlKeyDef = (_options$keyboardMap$ = options.keyboardMap.find(k => k.key === 'Control')) != null ? _options$keyboardMap$ :
/* istanbul ignore next */
{
key: 'Control',
code: 'Control'
};
_dom.fireEvent.keyDown(element, (0, _getEventProps.getKeyEventProps)(ctrlKeyDef, state));
}
}];
exports.preKeydownBehavior = preKeydownBehavior;
const keydownBehavior = [{
matches: keyDef => keyDef.key === 'CapsLock',
handle: (keyDef, element, options, state) => {
state.modifiers.caps = !state.modifiers.caps;
}
}, {
matches: (keyDef, element) => keyDef.key === 'Backspace' && (0, _utils.isEditable)(element) && !(0, _utils.isCursorAtStart)(element),
handle: (keyDef, element, options, state) => {
const {
newValue,
newSelectionStart
} = (0, _utils.calculateNewValue)('', element, state.carryValue, undefined, 'backward');
(0, _shared.fireInputEvent)(element, {
newValue,
newSelectionStart,
eventOverrides: {
inputType: 'deleteContentBackward'
}
});
(0, _shared.carryValue)(element, state, newValue);
}
}];
exports.keydownBehavior = keydownBehavior;
const keypressBehavior = [{
matches: (keyDef, element) => keyDef.key === 'Enter' && (0, _utils.isElementType)(element, 'input') && ['checkbox', 'radio'].includes(element.type),
handle: (keyDef, element) => {
const form = element.form;
if ((0, _utils.hasFormSubmit)(form)) {
_dom.fireEvent.submit(form);
}
}
}, {
matches: (keyDef, element) => keyDef.key === 'Enter' && ((0, _utils.isClickableInput)(element) || // Links with href defined should handle Enter the same as a click
(0, _utils.isElementType)(element, 'a') && Boolean(element.href)),
handle: (keyDef, element, options, state) => {
_dom.fireEvent.click(element, (0, _getEventProps.getMouseEventProps)(state));
}
}, {
matches: (keyDef, element) => keyDef.key === 'Enter' && (0, _utils.isElementType)(element, 'input'),
handle: (keyDef, element) => {
const form = element.form;
if (form && (form.querySelectorAll('input').length === 1 || (0, _utils.hasFormSubmit)(form))) {
_dom.fireEvent.submit(form);
}
}
}];
exports.keypressBehavior = keypressBehavior;
const preKeyupBehavior = [// modifierKeys switch off the modifier BEFORE the keyup event
...Object.entries(modifierKeys).map(([key, modKey]) => ({
matches: keyDef => keyDef.key === key,
handle: (keyDef, element, options, state) => {
state.modifiers[modKey] = false;
}
}))];
exports.preKeyupBehavior = preKeyupBehavior;
const keyupBehavior = [{
matches: (keyDef, element) => keyDef.key === ' ' && (0, _utils.isClickableInput)(element),
handle: (keyDef, element, options, state) => {
_dom.fireEvent.click(element, (0, _getEventProps.getMouseEventProps)(state));
}
}];
exports.keyupBehavior = keyupBehavior;
const postKeyupBehavior = [// AltGraph produces an extra keyup for Control
// The modifier does not change
{
matches: keyDef => keyDef.key === 'AltGraph',
handle: (keyDef, element, options, state) => {
var _options$keyboardMap$2;
const ctrlKeyDef = (_options$keyboardMap$2 = options.keyboardMap.find(k => k.key === 'Control')) != null ? _options$keyboardMap$2 :
/* istanbul ignore next */
{
key: 'Control',
code: 'Control'
};
_dom.fireEvent.keyUp(element, (0, _getEventProps.getKeyEventProps)(ctrlKeyDef, state));
}
}];
exports.postKeyupBehavior = postKeyupBehavior;