Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -3262,9 +3262,9 @@ function blurActiveElementWithinFragment(
return false;
}
const instance = getInstanceFromHostFiber<Instance>(child);
if (instance === activeElement) {
if (instance === activeElement || instance.contains(activeElement)) {
// $FlowFixMe[prop-missing]
instance.blur();
activeElement.blur();
return true;
}
return false;
Expand Down
30 changes: 30 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFragmentRefs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,36 @@ describe('FragmentRefs', () => {
expect(document.activeElement).toEqual(document.body);
});

// @gate enableFragmentRefs
it('removes focus from a nested element inside of the Fragment', async () => {
const fragmentRef = React.createRef();
const root = ReactDOMClient.createRoot(container);

function Test() {
return (
<Fragment ref={fragmentRef}>
<div>
<input id="nested-input" />
</div>
</Fragment>
);
}

await act(() => {
root.render(<Test />);
});

await act(() => {
fragmentRef.current.focus();
});
expect(document.activeElement.id).toEqual('nested-input');

await act(() => {
fragmentRef.current.blur();
});
expect(document.activeElement).toEqual(document.body);
});

// @gate enableFragmentRefs
it('does not remove focus from elements outside of the Fragment', async () => {
const fragmentRefA = React.createRef();
Expand Down
Loading