2020-04-10 15:39:07 +08:00
|
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
2023-07-09 16:47:31 +08:00
|
|
|
import classNames from 'classnames';
|
2022-06-22 14:57:09 +08:00
|
|
|
import CSSMotion from 'rc-motion';
|
2023-04-11 11:37:31 +08:00
|
|
|
import React, { forwardRef } from 'react';
|
|
|
|
import IconWrapper from './IconWrapper';
|
|
|
|
|
|
|
|
type InnerLoadingIconProps = {
|
|
|
|
prefixCls: string;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
iconClassName?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const InnerLoadingIcon = forwardRef<HTMLSpanElement, InnerLoadingIconProps>(
|
|
|
|
({ prefixCls, className, style, iconClassName }, ref) => {
|
|
|
|
const mergedIconCls = classNames(`${prefixCls}-loading-icon`, className);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<IconWrapper prefixCls={prefixCls} className={mergedIconCls} style={style} ref={ref}>
|
|
|
|
<LoadingOutlined className={iconClassName} />
|
|
|
|
</IconWrapper>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2020-04-10 15:39:07 +08:00
|
|
|
|
|
|
|
export interface LoadingIconProps {
|
|
|
|
prefixCls: string;
|
|
|
|
existIcon: boolean;
|
|
|
|
loading?: boolean | object;
|
2023-04-11 11:37:31 +08:00
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
2020-04-10 15:39:07 +08:00
|
|
|
}
|
2023-01-11 21:37:12 +08:00
|
|
|
|
|
|
|
const getCollapsedWidth = (): React.CSSProperties => ({
|
|
|
|
width: 0,
|
|
|
|
opacity: 0,
|
|
|
|
transform: 'scale(0)',
|
|
|
|
});
|
|
|
|
|
|
|
|
const getRealWidth = (node: HTMLElement): React.CSSProperties => ({
|
2020-04-10 15:39:07 +08:00
|
|
|
width: node.scrollWidth,
|
|
|
|
opacity: 1,
|
|
|
|
transform: 'scale(1)',
|
|
|
|
});
|
|
|
|
|
2023-07-09 16:47:31 +08:00
|
|
|
const LoadingIcon: React.FC<LoadingIconProps> = (props) => {
|
|
|
|
const { prefixCls, loading, existIcon, className, style } = props;
|
2020-04-10 15:39:07 +08:00
|
|
|
const visible = !!loading;
|
|
|
|
|
|
|
|
if (existIcon) {
|
2023-04-11 11:37:31 +08:00
|
|
|
return <InnerLoadingIcon prefixCls={prefixCls} className={className} style={style} />;
|
2020-04-10 15:39:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CSSMotion
|
|
|
|
visible={visible}
|
|
|
|
// We do not really use this motionName
|
|
|
|
motionName={`${prefixCls}-loading-icon-motion`}
|
|
|
|
removeOnLeave
|
|
|
|
onAppearStart={getCollapsedWidth}
|
|
|
|
onAppearActive={getRealWidth}
|
|
|
|
onEnterStart={getCollapsedWidth}
|
|
|
|
onEnterActive={getRealWidth}
|
|
|
|
onLeaveStart={getRealWidth}
|
|
|
|
onLeaveActive={getCollapsedWidth}
|
|
|
|
>
|
2023-04-11 11:37:31 +08:00
|
|
|
{(
|
|
|
|
{
|
|
|
|
className: motionCls,
|
|
|
|
style: motionStyle,
|
|
|
|
}: { className?: string; style?: React.CSSProperties },
|
|
|
|
ref: any,
|
|
|
|
) => (
|
|
|
|
<InnerLoadingIcon
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={className}
|
|
|
|
style={{ ...style, ...motionStyle }}
|
|
|
|
ref={ref}
|
|
|
|
iconClassName={motionCls}
|
|
|
|
/>
|
2020-12-09 17:12:32 +08:00
|
|
|
)}
|
2020-04-10 15:39:07 +08:00
|
|
|
</CSSMotion>
|
|
|
|
);
|
2020-11-20 21:43:43 +08:00
|
|
|
};
|
2020-11-14 00:13:48 +08:00
|
|
|
|
|
|
|
export default LoadingIcon;
|