|
1 | 1 | import { fireEvent } from "@testing-library/dom";
|
2 | 2 |
|
3 | 3 | function wait(time) {
|
4 |
| - return new Promise(function(resolve) { |
| 4 | + return new Promise(function (resolve) { |
5 | 5 | setTimeout(() => resolve(), time);
|
6 | 6 | });
|
7 | 7 | }
|
@@ -232,21 +232,24 @@ const userEvent = {
|
232 | 232 | const focusableElements = focusTrap.querySelectorAll(
|
233 | 233 | "input, button, select, textarea, a[href], [tabindex]"
|
234 | 234 | );
|
235 |
| - const list = Array.prototype.filter |
236 |
| - .call(focusableElements, function(item) { |
237 |
| - return item.getAttribute("tabindex") !== "-1"; |
238 |
| - }) |
| 235 | + let list = Array.prototype.filter.call(focusableElements, function (item) { |
| 236 | + return item.getAttribute("tabindex") !== "-1"; |
| 237 | + }).map((el, idx) => ({ el, idx })) |
239 | 238 | .sort((a, b) => {
|
240 |
| - const tabIndexA = a.getAttribute("tabindex"); |
241 |
| - const tabIndexB = b.getAttribute("tabindex"); |
242 |
| - return tabIndexA < tabIndexB ? -1 : tabIndexA > tabIndexB ? 1 : 0; |
243 |
| - }); |
244 |
| - const index = list.indexOf(document.activeElement); |
| 239 | + const tabIndexA = a.el.getAttribute("tabindex"); |
| 240 | + const tabIndexB = b.el.getAttribute("tabindex"); |
| 241 | + |
| 242 | + const diff = tabIndexA - tabIndexB; |
| 243 | + |
| 244 | + return diff !== 0 ? diff : a.idx - b.idx; |
| 245 | + }) |
| 246 | + |
| 247 | + const index = list.findIndex(({ el }) => el === document.activeElement); |
245 | 248 |
|
246 | 249 | let nextIndex = shift ? index - 1 : index + 1;
|
247 | 250 | let defaultIndex = shift ? list.length - 1 : 0;
|
248 | 251 |
|
249 |
| - const next = list[nextIndex] || list[defaultIndex]; |
| 252 | + const { el: next } = (list[nextIndex] || list[defaultIndex]); |
250 | 253 |
|
251 | 254 | if (next.getAttribute("tabindex") === null) {
|
252 | 255 | next.setAttribute("tabindex", "0"); // jsdom requires tabIndex=0 for an item to become 'document.activeElement' (the browser does not)
|
|
0 commit comments