ant-design/components/_util/PurePanel.tsx
二货机器人 8a0764dd69
feat: Tooltip support pure render (#36512)
* chore: init

* feat: Pure Panel Tooltip

* test: snapshot

* chore: clean up
2022-07-13 23:06:28 +08:00

69 lines
2.0 KiB
TypeScript

import * as React from 'react';
import { ConfigContext } from '../config-provider';
export interface BaseProps {
prefixCls?: string;
style?: React.CSSProperties;
}
/* istanbul ignore next */
export default function genPurePanel<ComponentProps extends BaseProps>(
Component: any,
defaultPrefixCls?: string,
getDropdownCls?: (prefixCls: string) => string,
) {
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);
const prefixCls = getPrefixCls(defaultPrefixCls || 'select', customizePrefixCls);
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 dropdownCls = getDropdownCls
? `.${getDropdownCls(prefixCls)}`
: `.${prefixCls}-dropdown`;
const popup = holderRef.current?.querySelector(dropdownCls);
if (popup) {
clearInterval(interval);
resizeObserver.observe(popup);
}
}, 10);
return () => {
clearInterval(interval);
resizeObserver.disconnect();
};
}
}, []);
return (
<div ref={holderRef} style={{ paddingBottom: popupHeight, position: 'relative' }}>
<Component
{...props}
style={{
...style,
margin: 0,
}}
open={open}
visible={open}
getPopupContainer={() => holderRef.current!}
/>
</div>
);
} as typeof Component;
}