Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 930b4e2

Browse files
authored
Fix roving tab index crash compareDocumentPosition (#12594)
* Fix roving tab index crash `compareDocumentPosition` Signed-off-by: Michael Telatynski <[email protected]> * Iterate Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 3e7511c commit 930b4e2

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

src/accessibility/RovingTabIndex.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import React, {
1818
createContext,
1919
useCallback,
2020
useContext,
21-
useLayoutEffect,
21+
useEffect,
2222
useMemo,
2323
useRef,
2424
useReducer,
@@ -144,7 +144,7 @@ export const reducer: Reducer<IState, Action> = (state: IState, action: Action)
144144
}
145145
if (document.activeElement === document.body) {
146146
// if the focus got reverted to the body then the user was likely focused on the unmounted element
147-
state.activeRef?.current?.focus();
147+
setTimeout(() => state.activeRef?.current?.focus(), 0);
148148
}
149149
}
150150

@@ -362,7 +362,7 @@ export const useRovingTabIndex = <T extends HTMLElement>(
362362
}
363363

364364
// setup (after refs)
365-
useLayoutEffect(() => {
365+
useEffect(() => {
366366
context.dispatch({
367367
type: Type.Register,
368368
payload: { ref },

test/accessibility/RovingTabIndex-test.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ const Button = (props: HTMLAttributes<HTMLButtonElement>) => {
3333
};
3434

3535
const checkTabIndexes = (buttons: NodeListOf<HTMLElement>, expectations: number[]) => {
36-
expect(buttons.length).toBe(expectations.length);
37-
for (let i = 0; i < buttons.length; i++) {
38-
expect(buttons[i].tabIndex).toBe(expectations[i]);
39-
}
36+
expect([...buttons].map((b) => b.tabIndex)).toStrictEqual(expectations);
4037
};
4138

4239
// give the buttons keys for the fibre reconciler to not treat them all as the same

test/components/views/emojipicker/EmojiPicker-test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import React, { createRef } from "react";
18-
import { render } from "@testing-library/react";
18+
import { render, waitFor } from "@testing-library/react";
1919
import userEvent from "@testing-library/user-event";
2020

2121
import EmojiPicker from "../../../../src/components/views/emojipicker/EmojiPicker";
@@ -24,7 +24,7 @@ import { stubClient } from "../../../test-utils";
2424
describe("EmojiPicker", function () {
2525
stubClient();
2626

27-
it("should not mangle default order after filtering", () => {
27+
it("should not mangle default order after filtering", async () => {
2828
const ref = createRef<EmojiPicker>();
2929
const { container } = render(
3030
<EmojiPicker ref={ref} onChoose={(str: string) => false} onFinished={jest.fn()} />,
@@ -41,7 +41,7 @@ describe("EmojiPicker", function () {
4141
// Clear the filter and assert that the HTML matches what it was before filtering
4242
//@ts-ignore private access
4343
ref.current!.onChangeFilter("");
44-
expect(beforeHtml).toEqual(container.innerHTML);
44+
await waitFor(() => expect(beforeHtml).toEqual(container.innerHTML));
4545
});
4646

4747
it("sort emojis by shortcode and size", function () {

0 commit comments

Comments
 (0)