2022-07-07 20:05:19 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
|
|
|
|
export interface BaseProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* istanbul ignore next */
|
2022-07-11 14:58:59 +08:00
|
|
|
export default function genPurePanel<ComponentProps extends BaseProps>(
|
|
|
|
Component: any,
|
|
|
|
defaultPrefixCls?: string,
|
|
|
|
) {
|
2022-07-07 20:05:19 +08:00
|
|
|
return function PurePanel(props: ComponentProps) {
|
|
|
|
const { prefixCls: customizePrefixCls, style } = props;
|
|
|
|
const holderRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
const [popupHeight, setPopupHeight] = React.useState(0);
|
|
|
|
const [open, setOpen] = React.useState(false);
|
|
|
|
|
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
2022-07-11 14:58:59 +08:00
|
|
|
const prefixCls = getPrefixCls(defaultPrefixCls || 'select', customizePrefixCls);
|
2022-07-07 20:05:19 +08:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
// We do not care about ssr
|
|
|
|
setOpen(true);
|
|
|
|
|
|
|
|
if (typeof ResizeObserver !== 'undefined') {
|
|
|
|
const resizeObserver = new ResizeObserver(entries => {
|
|
|
|
const element: HTMLDivElement = entries[0].target as any;
|
|
|
|
setPopupHeight(element.offsetHeight + 8);
|
|
|
|
});
|
|
|
|
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
const popup = holderRef.current?.querySelector(`.${prefixCls}-dropdown`);
|
|
|
|
|
|
|
|
if (popup) {
|
|
|
|
clearInterval(interval);
|
|
|
|
resizeObserver.observe(popup);
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearInterval(interval);
|
|
|
|
resizeObserver.disconnect();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2022-07-11 14:58:59 +08:00
|
|
|
<div ref={holderRef} style={{ paddingBottom: popupHeight, position: 'relative' }}>
|
2022-07-07 20:05:19 +08:00
|
|
|
<Component
|
|
|
|
{...props}
|
|
|
|
style={{
|
|
|
|
...style,
|
|
|
|
margin: 0,
|
|
|
|
}}
|
|
|
|
open={open}
|
2022-07-11 14:58:59 +08:00
|
|
|
visible={open}
|
2022-07-07 20:05:19 +08:00
|
|
|
getPopupContainer={() => holderRef.current!}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} as typeof Component;
|
|
|
|
}
|