2023-01-20 11:03:50 +08:00
|
|
|
import React from 'react';
|
2023-07-18 12:18:51 +08:00
|
|
|
import type { PortalProps, PortalRef } from 'rc-util/lib/Portal';
|
2023-06-07 21:59:21 +08:00
|
|
|
import { TriggerMockContext } from '../../../shared/demoTestContext';
|
2023-01-20 11:03:50 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
let OriginPortal = jest.requireActual('rc-util/lib/Portal');
|
|
|
|
OriginPortal = OriginPortal.default ?? OriginPortal;
|
2023-07-18 12:18:51 +08:00
|
|
|
|
2024-05-06 12:04:27 +08:00
|
|
|
class MockPortal extends React.Component<React.PropsWithChildren> {
|
2024-04-01 15:49:45 +08:00
|
|
|
container: boolean | undefined;
|
2023-01-20 11:03:50 +08:00
|
|
|
|
|
|
|
static contextType = TriggerMockContext;
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.createContainer();
|
|
|
|
}
|
|
|
|
|
|
|
|
createContainer() {
|
|
|
|
this.container = true;
|
|
|
|
this.forceUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { children } = this.props;
|
|
|
|
if (this.container) {
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 12:04:27 +08:00
|
|
|
const CustomPortal = React.forwardRef<PortalRef, PortalProps | React.PropsWithChildren>((props, ref) => {
|
2023-01-20 11:03:50 +08:00
|
|
|
const context = React.useContext(TriggerMockContext);
|
|
|
|
if (context?.mock === false) {
|
|
|
|
return <OriginPortal {...props} ref={ref} />;
|
|
|
|
}
|
|
|
|
return <MockPortal {...props} />;
|
|
|
|
});
|
2023-07-18 12:18:51 +08:00
|
|
|
|
|
|
|
export default CustomPortal;
|