@testing-library/user-event 12.2.1 → 12.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -16
- package/dist/blur.js +15 -0
- package/dist/clear.js +35 -0
- package/dist/click.js +140 -0
- package/dist/focus.js +15 -0
- package/dist/hover.js +73 -0
- package/dist/index.js +38 -0
- package/dist/keys/navigation-key.js +54 -0
- package/dist/paste.js +55 -0
- package/dist/select-options.js +97 -0
- package/dist/tab.js +137 -0
- package/dist/type.js +837 -0
- package/dist/upload.js +57 -0
- package/dist/utils.js +307 -0
- package/package.json +1 -1
package/dist/tab.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.tab = tab;
|
|
7
|
+
|
|
8
|
+
var _dom = require("@testing-library/dom");
|
|
9
|
+
|
|
10
|
+
var _utils = require("./utils");
|
|
11
|
+
|
|
12
|
+
var _focus = require("./focus");
|
|
13
|
+
|
|
14
|
+
var _blur = require("./blur");
|
|
15
|
+
|
|
16
|
+
function getNextElement(currentIndex, shift, elements, focusTrap) {
|
|
17
|
+
if (focusTrap === document && currentIndex === 0 && shift) {
|
|
18
|
+
return document.body;
|
|
19
|
+
} else if (focusTrap === document && currentIndex === elements.length - 1 && !shift) {
|
|
20
|
+
return document.body;
|
|
21
|
+
} else {
|
|
22
|
+
const nextIndex = shift ? currentIndex - 1 : currentIndex + 1;
|
|
23
|
+
const defaultIndex = shift ? elements.length - 1 : 0;
|
|
24
|
+
return elements[nextIndex] || elements[defaultIndex];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function tab({
|
|
29
|
+
shift = false,
|
|
30
|
+
focusTrap
|
|
31
|
+
} = {}) {
|
|
32
|
+
var _focusTrap$ownerDocum, _focusTrap;
|
|
33
|
+
|
|
34
|
+
const previousElement = (0, _utils.getActiveElement)((_focusTrap$ownerDocum = (_focusTrap = focusTrap) == null ? void 0 : _focusTrap.ownerDocument) != null ? _focusTrap$ownerDocum : document);
|
|
35
|
+
|
|
36
|
+
if (!focusTrap) {
|
|
37
|
+
focusTrap = document;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const focusableElements = focusTrap.querySelectorAll(_utils.FOCUSABLE_SELECTOR);
|
|
41
|
+
const enabledElements = [...focusableElements].filter(el => el === previousElement || el.getAttribute('tabindex') !== '-1' && !el.disabled);
|
|
42
|
+
if (enabledElements.length === 0) return;
|
|
43
|
+
const orderedElements = enabledElements.map((el, idx) => ({
|
|
44
|
+
el,
|
|
45
|
+
idx
|
|
46
|
+
})).sort((a, b) => {
|
|
47
|
+
// tabindex has no effect if the active element has tabindex="-1"
|
|
48
|
+
if (previousElement && previousElement.getAttribute('tabindex') === '-1') {
|
|
49
|
+
return a.idx - b.idx;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const tabIndexA = a.el.getAttribute('tabindex');
|
|
53
|
+
const tabIndexB = b.el.getAttribute('tabindex');
|
|
54
|
+
const diff = tabIndexA - tabIndexB;
|
|
55
|
+
return diff === 0 ? a.idx - b.idx : diff;
|
|
56
|
+
}).map(({
|
|
57
|
+
el
|
|
58
|
+
}) => el);
|
|
59
|
+
const checkedRadio = {};
|
|
60
|
+
let prunedElements = [];
|
|
61
|
+
orderedElements.forEach(el => {
|
|
62
|
+
// For radio groups keep only the active radio
|
|
63
|
+
// If there is no active radio, keep only the checked radio
|
|
64
|
+
// If there is no checked radio, treat like everything else
|
|
65
|
+
if (el.type === 'radio' && el.name) {
|
|
66
|
+
// If the active element is part of the group, add only that
|
|
67
|
+
if (previousElement && previousElement.type === el.type && previousElement.name === el.name) {
|
|
68
|
+
if (el === previousElement) {
|
|
69
|
+
prunedElements.push(el);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return;
|
|
73
|
+
} // If we stumble upon a checked radio, remove the others
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
if (el.checked) {
|
|
77
|
+
prunedElements = prunedElements.filter(e => e.type !== el.type || e.name !== el.name);
|
|
78
|
+
prunedElements.push(el);
|
|
79
|
+
checkedRadio[el.name] = el;
|
|
80
|
+
return;
|
|
81
|
+
} // If we already found the checked one, skip
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
if (checkedRadio[el.name]) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
prunedElements.push(el);
|
|
90
|
+
});
|
|
91
|
+
const index = prunedElements.findIndex(el => el === previousElement);
|
|
92
|
+
const nextElement = getNextElement(index, shift, prunedElements, focusTrap);
|
|
93
|
+
const shiftKeyInit = {
|
|
94
|
+
key: 'Shift',
|
|
95
|
+
keyCode: 16,
|
|
96
|
+
shiftKey: true
|
|
97
|
+
};
|
|
98
|
+
const tabKeyInit = {
|
|
99
|
+
key: 'Tab',
|
|
100
|
+
keyCode: 9,
|
|
101
|
+
shiftKey: shift
|
|
102
|
+
};
|
|
103
|
+
let continueToTab = true; // not sure how to make it so there's no previous element...
|
|
104
|
+
// istanbul ignore else
|
|
105
|
+
|
|
106
|
+
if (previousElement) {
|
|
107
|
+
// preventDefault on the shift key makes no difference
|
|
108
|
+
if (shift) _dom.fireEvent.keyDown(previousElement, { ...shiftKeyInit
|
|
109
|
+
});
|
|
110
|
+
continueToTab = _dom.fireEvent.keyDown(previousElement, { ...tabKeyInit
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const keyUpTarget = !continueToTab && previousElement ? previousElement : nextElement;
|
|
115
|
+
|
|
116
|
+
if (continueToTab) {
|
|
117
|
+
if (nextElement === document.body) {
|
|
118
|
+
(0, _blur.blur)(previousElement);
|
|
119
|
+
} else {
|
|
120
|
+
(0, _focus.focus)(nextElement);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_dom.fireEvent.keyUp(keyUpTarget, { ...tabKeyInit
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
if (shift) {
|
|
128
|
+
_dom.fireEvent.keyUp(keyUpTarget, { ...shiftKeyInit,
|
|
129
|
+
shiftKey: false
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/*
|
|
134
|
+
eslint
|
|
135
|
+
complexity: "off",
|
|
136
|
+
max-statements: "off",
|
|
137
|
+
*/
|