Skip to content
Open
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
8 changes: 5 additions & 3 deletions src/Portal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
canUseDom,
getNodeRef,
supportRef,
supportNodeRef,
useComposeRef,
warning,
} from '@rc-component/util';
Expand Down Expand Up @@ -131,7 +131,9 @@ const Portal = React.forwardRef<any, PortalProps>((props, ref) => {
// =========================== Ref ===========================
let childRef: React.Ref<any> = null;

if (children && supportRef(children) && ref) {
const childSupportsRef = supportNodeRef(children);

if (childSupportsRef && ref) {
childRef = getNodeRef(children);
}

Expand All @@ -148,7 +150,7 @@ const Portal = React.forwardRef<any, PortalProps>((props, ref) => {
const renderInline = mergedContainer === false || inlineMock();

let reffedChildren = children;
if (ref) {
if (ref && childSupportsRef) {
reffedChildren = React.cloneElement(children as any, {
ref: mergedRef,
});
Expand Down
16 changes: 16 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ describe('Portal', () => {
expect(portalRef.current).toBeFalsy();
});

it.each([
['text', 'Bamboo'],
['number', 0],
])('renders %s children when a ref is provided', (_, children) => {
const portalRef = React.createRef();

render(
<Portal ref={portalRef} open>
{children}
</Portal>,
);

expect(document.body).toHaveTextContent(String(children));
expect(portalRef.current).toBeNull();
});

it('no warning for React 19 ref', () => {
const errSpy = jest.spyOn(console, 'error');
const elementRef = React.createRef<HTMLParagraphElement>();
Expand Down