mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-18 11:18:14 +08:00
38 lines
863 B
TypeScript
38 lines
863 B
TypeScript
|
import React from 'react';
|
||
|
import { TriggerMockContext } from '../../../shared/demoTestContext';
|
||
|
|
||
|
let OriginPortal = jest.requireActual('rc-util/lib/Portal');
|
||
|
OriginPortal = OriginPortal.default ?? OriginPortal;
|
||
|
class MockPortal extends React.Component<{ children?: React.ReactNode }> {
|
||
|
container: boolean;
|
||
|
|
||
|
static contextType = TriggerMockContext;
|
||
|
|
||
|
componentDidMount() {
|
||
|
this.createContainer();
|
||
|
}
|
||
|
|
||
|
createContainer() {
|
||
|
this.container = true;
|
||
|
this.forceUpdate();
|
||
|
}
|
||
|
|
||
|
render() {
|
||
|
const { children } = this.props;
|
||
|
if (this.container) {
|
||
|
return children;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default React.forwardRef((props: any, ref: any) => {
|
||
|
const context = React.useContext(TriggerMockContext);
|
||
|
|
||
|
if (context?.mock === false) {
|
||
|
return <OriginPortal {...props} ref={ref} />;
|
||
|
}
|
||
|
|
||
|
return <MockPortal {...props} />;
|
||
|
});
|