2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-12-09 13:31:45 +08:00
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
|
|
|
|
2022-08-17 19:57:10 +08:00
|
|
|
import ConfigProvider, { ConfigContext } from '../config-provider';
|
2023-12-09 13:31:45 +08:00
|
|
|
import type { AnyObject } from './type';
|
2022-07-07 20:05:19 +08:00
|
|
|
|
2023-12-09 19:32:10 +08:00
|
|
|
export function withPureRenderTheme<T extends AnyObject = AnyObject>(Component: React.FC<T>) {
|
2023-12-09 13:31:45 +08:00
|
|
|
return (props: T) => (
|
|
|
|
<ConfigProvider theme={{ token: { motion: false, zIndexPopupBase: 0 } }}>
|
|
|
|
<Component {...props} />
|
|
|
|
</ConfigProvider>
|
|
|
|
);
|
2023-08-08 19:48:41 +08:00
|
|
|
}
|
|
|
|
|
2022-07-07 20:05:19 +08:00
|
|
|
export interface BaseProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
}
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
/* istanbul ignore next */
|
2023-12-09 13:31:45 +08:00
|
|
|
const genPurePanel = <ComponentProps extends BaseProps = BaseProps>(
|
2022-07-11 14:58:59 +08:00
|
|
|
Component: any,
|
|
|
|
defaultPrefixCls?: string,
|
2023-04-19 23:07:47 +08:00
|
|
|
getDropdownCls?: null | ((prefixCls: string) => string),
|
|
|
|
postProps?: (props: ComponentProps) => ComponentProps,
|
2023-12-09 13:31:45 +08:00
|
|
|
) => {
|
|
|
|
type WrapProps = ComponentProps & AnyObject;
|
2023-04-19 23:07:47 +08:00
|
|
|
|
2023-12-09 13:31:45 +08:00
|
|
|
const PurePanel: React.FC<WrapProps> = (props) => {
|
2022-07-07 20:05:19 +08:00
|
|
|
const { prefixCls: customizePrefixCls, style } = props;
|
2022-08-17 19:57:10 +08:00
|
|
|
|
2022-07-07 20:05:19 +08:00
|
|
|
const holderRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
const [popupHeight, setPopupHeight] = React.useState(0);
|
2022-09-19 11:37:44 +08:00
|
|
|
const [popupWidth, setPopupWidth] = React.useState(0);
|
2022-08-17 19:57:10 +08:00
|
|
|
const [open, setOpen] = useMergedState(false, {
|
|
|
|
value: props.open,
|
|
|
|
});
|
2022-07-07 20:05:19 +08:00
|
|
|
|
|
|
|
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') {
|
2022-11-19 13:47:33 +08:00
|
|
|
const resizeObserver = new ResizeObserver((entries) => {
|
2024-04-01 15:49:45 +08:00
|
|
|
const element = entries[0].target as HTMLDivElement;
|
2022-07-07 20:05:19 +08:00
|
|
|
setPopupHeight(element.offsetHeight + 8);
|
2022-09-19 11:37:44 +08:00
|
|
|
setPopupWidth(element.offsetWidth);
|
2022-07-07 20:05:19 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const interval = setInterval(() => {
|
2022-07-11 15:57:05 +08:00
|
|
|
const dropdownCls = getDropdownCls
|
|
|
|
? `.${getDropdownCls(prefixCls)}`
|
|
|
|
: `.${prefixCls}-dropdown`;
|
|
|
|
const popup = holderRef.current?.querySelector(dropdownCls);
|
2022-07-07 20:05:19 +08:00
|
|
|
|
|
|
|
if (popup) {
|
|
|
|
clearInterval(interval);
|
|
|
|
resizeObserver.observe(popup);
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearInterval(interval);
|
|
|
|
resizeObserver.disconnect();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2023-04-19 23:07:47 +08:00
|
|
|
let mergedProps: WrapProps = {
|
|
|
|
...props,
|
|
|
|
style: {
|
|
|
|
...style,
|
|
|
|
margin: 0,
|
|
|
|
},
|
|
|
|
open,
|
|
|
|
visible: open,
|
|
|
|
getPopupContainer: () => holderRef.current!,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (postProps) {
|
2023-12-09 19:32:10 +08:00
|
|
|
mergedProps = postProps(mergedProps);
|
2023-04-19 23:07:47 +08:00
|
|
|
}
|
2023-12-09 13:31:45 +08:00
|
|
|
const mergedStyle: React.CSSProperties = {
|
|
|
|
paddingBottom: popupHeight,
|
|
|
|
position: 'relative',
|
|
|
|
minWidth: popupWidth,
|
|
|
|
};
|
2022-07-07 20:05:19 +08:00
|
|
|
return (
|
2023-12-09 13:31:45 +08:00
|
|
|
<div ref={holderRef} style={mergedStyle}>
|
2023-08-08 19:48:41 +08:00
|
|
|
<Component {...mergedProps} />
|
|
|
|
</div>
|
2022-07-07 20:05:19 +08:00
|
|
|
);
|
2023-12-09 13:31:45 +08:00
|
|
|
};
|
2023-08-08 19:48:41 +08:00
|
|
|
|
2023-12-09 19:32:10 +08:00
|
|
|
return withPureRenderTheme<AnyObject>(PurePanel);
|
2023-12-09 13:31:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default genPurePanel;
|