fix(focus): a removed control's focus skips a neighbour the CSS has hidden
A display:none button refuses focus(), so handing the focus to a hidden column's header button dropped it to <body>.
This commit is contained in:
@@ -3,7 +3,7 @@ import { afterEach, describe, expect, it } from "vitest";
|
||||
import { act, cleanup, render } from "@testing-library/react";
|
||||
import { useRef, useState } from "react";
|
||||
|
||||
import { useFocusKeeper } from "./useFocusKeeper";
|
||||
import { focusNeighbour, useFocusKeeper } from "./useFocusKeeper";
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
@@ -77,3 +77,30 @@ describe("useFocusKeeper", () => {
|
||||
expect(document.activeElement).toBe(document.body);
|
||||
});
|
||||
});
|
||||
|
||||
describe("focusNeighbour", () => {
|
||||
// E28 F2: the neighbour of the hidden-filter cue's chip is the table's first header button — the
|
||||
// hidden column's own, `display: none`, which refuses focus(): the focus fell to <body>. jsdom paints
|
||||
// nothing, so `checkVisibility` is stood in for, answering for the one hidden button.
|
||||
it("passes over a focusable the CSS has taken off screen", () => {
|
||||
render(
|
||||
<div>
|
||||
<div>
|
||||
<button>chip</button>
|
||||
</div>
|
||||
<button data-off="">hidden header</button>
|
||||
<button>shown header</button>
|
||||
</div>,
|
||||
);
|
||||
Object.defineProperty(HTMLElement.prototype, "checkVisibility", {
|
||||
configurable: true,
|
||||
value(this: HTMLElement) {
|
||||
return !this.hasAttribute("data-off");
|
||||
},
|
||||
});
|
||||
btn("chip").focus();
|
||||
expect(focusNeighbour(btn("chip"))).toBe(true);
|
||||
expect(document.activeElement).toBe(btn("shown header"));
|
||||
delete (HTMLElement.prototype as Partial<HTMLElement>).checkVisibility;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user