|
1 | 1 | import * as React from 'react';
|
| 2 | +import { useMemo, useRef } from 'react'; |
2 | 3 | import CSSMotion from 'rc-motion';
|
3 | 4 | import classNames from 'classnames';
|
4 | 5 | import ScrollNumber from './ScrollNumber';
|
@@ -68,21 +69,28 @@ const Badge: CompoundedComponent = ({
|
68 | 69 |
|
69 | 70 | const showAsDot = (dot && !isZero) || hasStatus;
|
70 | 71 |
|
71 |
| - const displayCount = showAsDot ? '' : numberedDisplayCount; |
| 72 | + const mergedCount = showAsDot ? '' : numberedDisplayCount; |
72 | 73 |
|
73 |
| - const isHidden = React.useMemo(() => { |
74 |
| - const isEmpty = displayCount === null || displayCount === undefined || displayCount === ''; |
| 74 | + const isHidden = useMemo(() => { |
| 75 | + const isEmpty = mergedCount === null || mergedCount === undefined || mergedCount === ''; |
75 | 76 | return (isEmpty || (isZero && !showZero)) && !showAsDot;
|
76 |
| - }, [displayCount, isZero, showZero, showAsDot]); |
| 77 | + }, [mergedCount, isZero, showZero, showAsDot]); |
| 78 | + |
| 79 | + // We need cache count since remove motion should not change count display |
| 80 | + const displayCountRef = useRef(mergedCount); |
| 81 | + if (!isHidden) { |
| 82 | + displayCountRef.current = mergedCount; |
| 83 | + } |
| 84 | + const displayCount = displayCountRef.current; |
77 | 85 |
|
78 | 86 | // We will cache the dot status to avoid shaking on leaved motion
|
79 |
| - const isDotRef = React.useRef(showAsDot); |
| 87 | + const isDotRef = useRef(showAsDot); |
80 | 88 | if (!isHidden) {
|
81 | 89 | isDotRef.current = showAsDot;
|
82 | 90 | }
|
83 | 91 |
|
84 | 92 | // =============================== Styles ===============================
|
85 |
| - const mergedStyle = React.useMemo<React.CSSProperties>(() => { |
| 93 | + const mergedStyle = useMemo<React.CSSProperties>(() => { |
86 | 94 | if (!offset) {
|
87 | 95 | return { ...style };
|
88 | 96 | }
|
@@ -173,7 +181,7 @@ const Badge: CompoundedComponent = ({
|
173 | 181 | [`${prefixCls}-count`]: !isDot,
|
174 | 182 | [`${prefixCls}-count-sm`]: size === 'small',
|
175 | 183 | [`${prefixCls}-multiple-words`]:
|
176 |
| - !isDot && count && count.toString && count.toString().length > 1, |
| 184 | + !isDot && displayCount && displayCount?.toString().length > 1, |
177 | 185 | [`${prefixCls}-status-${status}`]: !!status,
|
178 | 186 | [`${prefixCls}-status-${color}`]: isPresetColor(color),
|
179 | 187 | });
|
|
0 commit comments