Skip to content

Commit

Permalink
Fix traversal order, remove focus mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpope committed Feb 25, 2025
1 parent 67efec9 commit 87d09a8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
11 changes: 6 additions & 5 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -1659,16 +1659,17 @@ function recursivelyGetFragmentInstanceChildren(
return;
}

if (child.tag === HostComponent) {
childElements.add(child.stateNode);
}

if (child.sibling !== null) {
recursivelyGetFragmentInstanceChildren(child.sibling, childElements);
}

if (child.tag === HostComponent) {
childElements.add(child.stateNode);
return;
if (child.tag !== HostComponent) {
recursivelyGetFragmentInstanceChildren(child.child, childElements);
}

recursivelyGetFragmentInstanceChildren(child.child, childElements);
}

function normalizeListenerOptions(
Expand Down
18 changes: 2 additions & 16 deletions packages/react-dom/src/__tests__/ReactDOMFragmentRefs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ describe('FragmentRefs', () => {
const parentRef = React.createRef();
const fragmentRef = React.createRef();
const root = ReactDOMClient.createRoot(container);
let focusedElement = null;

function Test() {
return (
<div ref={parentRef}>
<Fragment ref={fragmentRef}>
<div id="child-a" />
<style>{`#child-c {}`}</style>
<a id="child-b" href="/">
B
</a>
Expand All @@ -118,24 +118,10 @@ describe('FragmentRefs', () => {
root.render(<Test />);
});

// The test environment doesn't implement focus.
// Mock it here, along with a naive focusable query so we can assert
// that the first _focusable_ element is found.
const focusableChildren = parentRef.current.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])',
);
const focusMock = jest.spyOn(HTMLElement.prototype, 'focus');
focusMock.mockImplementation(function () {
if (Array.from(focusableChildren).includes(this)) {
focusedElement = this.id;
} else {
return false;
}
});
await act(() => {
fragmentRef.current.focus();
});
expect(focusedElement).toEqual('child-b');
expect(document.activeElement.id).toEqual('child-b');
});

// @gate enableFragmentRefs
Expand Down

0 comments on commit 87d09a8

Please sign in to comment.