Skip to content

Commit 9954cdd

Browse files
Process Kristoffer input
1 parent 58838ae commit 9954cdd

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

packages/material-ui/src/Portal/Portal.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect
2020
const Portal = React.forwardRef(function Portal(props, ref) {
2121
const { children, container, disablePortal = false, onRendered } = props;
2222
const [mountNode, setMountNode] = React.useState(null);
23-
const handleRef = useForkRef(children.ref, ref);
23+
const handleRef = useForkRef(React.isValidElement(children) ? children.ref : null, ref);
2424

2525
useEnhancedEffect(() => {
2626
if (!disablePortal) {
@@ -46,10 +46,12 @@ const Portal = React.forwardRef(function Portal(props, ref) {
4646
}, [onRendered, mountNode, disablePortal]);
4747

4848
if (disablePortal) {
49-
React.Children.only(children);
50-
return React.cloneElement(children, {
51-
ref: handleRef,
52-
});
49+
if (React.isValidElement(children)) {
50+
return React.cloneElement(children, {
51+
ref: handleRef,
52+
});
53+
}
54+
return children;
5355
}
5456

5557
return mountNode ? ReactDOM.createPortal(children, mountNode) : mountNode;
@@ -63,7 +65,7 @@ Portal.propTypes = {
6365
/**
6466
* The children to render into the `container`.
6567
*/
66-
children: PropTypes.node.isRequired,
68+
children: PropTypes.node,
6769
/**
6870
* A node, component instance, or function that returns either.
6971
* The `container` will have the portal children appended to it.

packages/material-ui/src/Portal/Portal.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ describe('<Portal />', () => {
128128
it('should render overlay into container (document)', () => {
129129
render(
130130
<Portal>
131-
<div id="test2" />
131+
<div className="test2" />
132+
<div className="test2" />
132133
</Portal>,
133134
);
134-
expect(document.querySelectorAll('#test2').length).to.equal(1);
135+
expect(document.querySelectorAll('.test2').length).to.equal(2);
135136
});
136137

137138
it('should render overlay into container (DOMNode)', () => {

0 commit comments

Comments
 (0)