ant-design/components/button/LoadingIcon.tsx

82 lines
2.2 KiB
TypeScript
Raw Normal View History

feat: Support iconPosition with button icon (#47791) * feat: button support iconPosition * fix: iconPosition compatible LoadingIcon * docs(Button): update Button with iconPosition demo * fix: delete debug type * fix: iconPosition for InnerLoadingIcon with not existIcon * chore: update button snapshots * fix: fixed loading-icon-end style * chore: refactor buttonContent with genButtonContent * fix: iconPosition compatible with rtl * docs(Button): update button with iconPosition demo * chore: update test * fix: iconPosition compatible with rtl * docs(Buttob): add icon-position demo * restore icon demo * update snapshots * docs: update icon button demo * docs: update iconPosition button demo * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/demo/icon-position.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> --------- Signed-off-by: George H <default_hyn@163.com> Co-authored-by: kiner-tang <1127031143@qq.com> Co-authored-by: afc163 <afc163@gmail.com>
2024-04-07 22:42:20 +08:00
import React, { forwardRef } from 'react';
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
import classNames from 'classnames';
2022-06-22 14:57:09 +08:00
import CSSMotion from 'rc-motion';
feat: Support iconPosition with button icon (#47791) * feat: button support iconPosition * fix: iconPosition compatible LoadingIcon * docs(Button): update Button with iconPosition demo * fix: delete debug type * fix: iconPosition for InnerLoadingIcon with not existIcon * chore: update button snapshots * fix: fixed loading-icon-end style * chore: refactor buttonContent with genButtonContent * fix: iconPosition compatible with rtl * docs(Button): update button with iconPosition demo * chore: update test * fix: iconPosition compatible with rtl * docs(Buttob): add icon-position demo * restore icon demo * update snapshots * docs: update icon button demo * docs: update iconPosition button demo * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/demo/icon-position.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> --------- Signed-off-by: George H <default_hyn@163.com> Co-authored-by: kiner-tang <1127031143@qq.com> Co-authored-by: afc163 <afc163@gmail.com>
2024-04-07 22:42:20 +08:00
import IconWrapper from './IconWrapper';
type InnerLoadingIconProps = {
prefixCls: string;
className?: string;
style?: React.CSSProperties;
iconClassName?: string;
};
feat: Support iconPosition with button icon (#47791) * feat: button support iconPosition * fix: iconPosition compatible LoadingIcon * docs(Button): update Button with iconPosition demo * fix: delete debug type * fix: iconPosition for InnerLoadingIcon with not existIcon * chore: update button snapshots * fix: fixed loading-icon-end style * chore: refactor buttonContent with genButtonContent * fix: iconPosition compatible with rtl * docs(Button): update button with iconPosition demo * chore: update test * fix: iconPosition compatible with rtl * docs(Buttob): add icon-position demo * restore icon demo * update snapshots * docs: update icon button demo * docs: update iconPosition button demo * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/demo/icon-position.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> --------- Signed-off-by: George H <default_hyn@163.com> Co-authored-by: kiner-tang <1127031143@qq.com> Co-authored-by: afc163 <afc163@gmail.com>
2024-04-07 22:42:20 +08:00
const InnerLoadingIcon = forwardRef<HTMLSpanElement, InnerLoadingIconProps>((props, ref) => {
const { prefixCls, className, style, iconClassName } = props;
const mergedIconCls = classNames(`${prefixCls}-loading-icon`, className);
feat: Support iconPosition with button icon (#47791) * feat: button support iconPosition * fix: iconPosition compatible LoadingIcon * docs(Button): update Button with iconPosition demo * fix: delete debug type * fix: iconPosition for InnerLoadingIcon with not existIcon * chore: update button snapshots * fix: fixed loading-icon-end style * chore: refactor buttonContent with genButtonContent * fix: iconPosition compatible with rtl * docs(Button): update button with iconPosition demo * chore: update test * fix: iconPosition compatible with rtl * docs(Buttob): add icon-position demo * restore icon demo * update snapshots * docs: update icon button demo * docs: update iconPosition button demo * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/demo/icon-position.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> --------- Signed-off-by: George H <default_hyn@163.com> Co-authored-by: kiner-tang <1127031143@qq.com> Co-authored-by: afc163 <afc163@gmail.com>
2024-04-07 22:42:20 +08:00
return (
<IconWrapper prefixCls={prefixCls} className={mergedIconCls} style={style} ref={ref}>
<LoadingOutlined className={iconClassName} />
</IconWrapper>
);
});
feat: Support iconPosition with button icon (#47791) * feat: button support iconPosition * fix: iconPosition compatible LoadingIcon * docs(Button): update Button with iconPosition demo * fix: delete debug type * fix: iconPosition for InnerLoadingIcon with not existIcon * chore: update button snapshots * fix: fixed loading-icon-end style * chore: refactor buttonContent with genButtonContent * fix: iconPosition compatible with rtl * docs(Button): update button with iconPosition demo * chore: update test * fix: iconPosition compatible with rtl * docs(Buttob): add icon-position demo * restore icon demo * update snapshots * docs: update icon button demo * docs: update iconPosition button demo * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.zh-CN.md Co-authored-by: kiner-tang <1127031143@qq.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/demo/icon-position.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> * Update components/button/index.en-US.md Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: George H <default_hyn@163.com> --------- Signed-off-by: George H <default_hyn@163.com> Co-authored-by: kiner-tang <1127031143@qq.com> Co-authored-by: afc163 <afc163@gmail.com>
2024-04-07 22:42:20 +08:00
export type LoadingIconProps = {
prefixCls: string;
existIcon: boolean;
loading?: boolean | object;
className?: string;
style?: React.CSSProperties;
};
const getCollapsedWidth = (): React.CSSProperties => ({
width: 0,
opacity: 0,
transform: 'scale(0)',
});
const getRealWidth = (node: HTMLElement): React.CSSProperties => ({
width: node.scrollWidth,
opacity: 1,
transform: 'scale(1)',
});
const LoadingIcon: React.FC<LoadingIconProps> = (props) => {
const { prefixCls, loading, existIcon, className, style } = props;
const visible = !!loading;
if (existIcon) {
return <InnerLoadingIcon prefixCls={prefixCls} className={className} style={style} />;
}
return (
<CSSMotion
visible={visible}
// We do not really use this motionName
motionName={`${prefixCls}-loading-icon-motion`}
motionLeave={visible}
removeOnLeave
onAppearStart={getCollapsedWidth}
onAppearActive={getRealWidth}
onEnterStart={getCollapsedWidth}
onEnterActive={getRealWidth}
onLeaveStart={getRealWidth}
onLeaveActive={getCollapsedWidth}
>
{({ className: motionCls, style: motionStyle }, ref: React.Ref<HTMLSpanElement>) => (
<InnerLoadingIcon
prefixCls={prefixCls}
className={className}
style={{ ...style, ...motionStyle }}
ref={ref}
iconClassName={motionCls}
/>
)}
</CSSMotion>
);
};
export default LoadingIcon;