diff --git a/src/Portal.tsx b/src/Portal.tsx index 7fbca4f..ccf5a21 100644 --- a/src/Portal.tsx +++ b/src/Portal.tsx @@ -1,7 +1,7 @@ import { canUseDom, getNodeRef, - supportRef, + supportNodeRef, useComposeRef, warning, } from '@rc-component/util'; @@ -131,7 +131,9 @@ const Portal = React.forwardRef((props, ref) => { // =========================== Ref =========================== let childRef: React.Ref = null; - if (children && supportRef(children) && ref) { + const childSupportsRef = supportNodeRef(children); + + if (childSupportsRef && ref) { childRef = getNodeRef(children); } @@ -148,7 +150,7 @@ const Portal = React.forwardRef((props, ref) => { const renderInline = mergedContainer === false || inlineMock(); let reffedChildren = children; - if (ref) { + if (ref && childSupportsRef) { reffedChildren = React.cloneElement(children as any, { ref: mergedRef, }); diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 5b81aa8..62096d6 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -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( + + {children} + , + ); + + 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();