2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2020-12-07 21:23:42 +08:00
|
|
|
import { useMemo, useRef } from 'react';
|
2020-11-18 18:32:00 +08:00
|
|
|
import CSSMotion from 'rc-motion';
|
2015-12-18 17:27:22 +08:00
|
|
|
import classNames from 'classnames';
|
2018-12-05 19:12:18 +08:00
|
|
|
import ScrollNumber from './ScrollNumber';
|
2020-07-08 14:47:07 +08:00
|
|
|
import Ribbon from './Ribbon';
|
|
|
|
import { PresetColorType, PresetStatusColorType } from '../_util/colors';
|
2020-05-31 12:38:32 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-04-08 13:41:32 +08:00
|
|
|
import { LiteralUnion } from '../_util/type';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2020-07-08 14:47:07 +08:00
|
|
|
import { isPresetColor } from './utils';
|
2015-09-01 13:59:07 +08:00
|
|
|
|
2017-09-25 22:14:49 +08:00
|
|
|
export { ScrollNumberProps } from './ScrollNumber';
|
|
|
|
|
2020-07-08 14:47:07 +08:00
|
|
|
interface CompoundedComponent extends React.FC<BadgeProps> {
|
|
|
|
Ribbon: typeof Ribbon;
|
|
|
|
}
|
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface BadgeProps {
|
2016-07-14 13:29:50 +08:00
|
|
|
/** Number to show in badge */
|
2018-10-16 15:07:24 +08:00
|
|
|
count?: React.ReactNode;
|
2017-03-01 11:16:58 +08:00
|
|
|
showZero?: boolean;
|
2016-07-14 13:29:50 +08:00
|
|
|
/** Max count to show */
|
|
|
|
overflowCount?: number;
|
2020-12-28 15:30:18 +08:00
|
|
|
/** Whether to show red dot without number */
|
2016-07-14 13:29:50 +08:00
|
|
|
dot?: boolean;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
prefixCls?: string;
|
2017-10-14 14:29:18 +08:00
|
|
|
scrollNumberPrefixCls?: string;
|
2016-07-14 13:29:50 +08:00
|
|
|
className?: string;
|
2020-04-08 13:41:32 +08:00
|
|
|
status?: PresetStatusColorType;
|
|
|
|
color?: LiteralUnion<PresetColorType, string>;
|
2019-03-08 12:26:01 +08:00
|
|
|
text?: React.ReactNode;
|
2020-07-28 15:27:50 +08:00
|
|
|
size?: 'default' | 'small';
|
2017-11-03 11:37:35 +08:00
|
|
|
offset?: [number | string, number | string];
|
2018-04-10 01:09:18 +08:00
|
|
|
title?: string;
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
|
|
|
|
2020-07-08 14:47:07 +08:00
|
|
|
const Badge: CompoundedComponent = ({
|
2020-05-31 12:38:32 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
scrollNumberPrefixCls: customizeScrollNumberPrefixCls,
|
|
|
|
children,
|
|
|
|
status,
|
|
|
|
text,
|
|
|
|
color,
|
|
|
|
count = null,
|
|
|
|
overflowCount = 99,
|
|
|
|
dot = false,
|
2020-07-28 15:27:50 +08:00
|
|
|
size = 'default',
|
2020-05-31 12:38:32 +08:00
|
|
|
title,
|
|
|
|
offset,
|
|
|
|
style,
|
|
|
|
className,
|
|
|
|
showZero = false,
|
|
|
|
...restProps
|
|
|
|
}) => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('badge', customizePrefixCls);
|
|
|
|
|
2020-11-18 18:32:00 +08:00
|
|
|
// ================================ Misc ================================
|
2021-08-02 12:22:30 +08:00
|
|
|
const numberedDisplayCount = (
|
|
|
|
(count as number) > (overflowCount as number) ? `${overflowCount}+` : count
|
|
|
|
) as string | number | null;
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-11-18 18:32:00 +08:00
|
|
|
const hasStatus =
|
2020-09-06 13:07:39 +08:00
|
|
|
(status !== null && status !== undefined) || (color !== null && color !== undefined);
|
2020-04-25 20:53:21 +08:00
|
|
|
|
2020-11-18 18:32:00 +08:00
|
|
|
const isZero = numberedDisplayCount === '0' || numberedDisplayCount === 0;
|
2020-04-25 20:53:21 +08:00
|
|
|
|
2021-08-02 12:22:30 +08:00
|
|
|
const showAsDot = dot && !isZero;
|
2020-04-25 20:53:21 +08:00
|
|
|
|
2020-12-07 21:23:42 +08:00
|
|
|
const mergedCount = showAsDot ? '' : numberedDisplayCount;
|
2020-11-18 18:32:00 +08:00
|
|
|
|
2020-12-07 21:23:42 +08:00
|
|
|
const isHidden = useMemo(() => {
|
|
|
|
const isEmpty = mergedCount === null || mergedCount === undefined || mergedCount === '';
|
2020-11-18 18:32:00 +08:00
|
|
|
return (isEmpty || (isZero && !showZero)) && !showAsDot;
|
2020-12-07 21:23:42 +08:00
|
|
|
}, [mergedCount, isZero, showZero, showAsDot]);
|
|
|
|
|
2021-01-28 18:16:05 +08:00
|
|
|
// Count should be cache in case hidden change it
|
|
|
|
const countRef = useRef(count);
|
|
|
|
if (!isHidden) {
|
|
|
|
countRef.current = count;
|
|
|
|
}
|
|
|
|
const livingCount = countRef.current;
|
|
|
|
|
2020-12-07 21:23:42 +08:00
|
|
|
// We need cache count since remove motion should not change count display
|
|
|
|
const displayCountRef = useRef(mergedCount);
|
|
|
|
if (!isHidden) {
|
|
|
|
displayCountRef.current = mergedCount;
|
|
|
|
}
|
|
|
|
const displayCount = displayCountRef.current;
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-11-18 18:32:00 +08:00
|
|
|
// We will cache the dot status to avoid shaking on leaved motion
|
2020-12-07 21:23:42 +08:00
|
|
|
const isDotRef = useRef(showAsDot);
|
2020-11-18 18:32:00 +08:00
|
|
|
if (!isHidden) {
|
|
|
|
isDotRef.current = showAsDot;
|
|
|
|
}
|
|
|
|
|
|
|
|
// =============================== Styles ===============================
|
2020-12-07 21:23:42 +08:00
|
|
|
const mergedStyle = useMemo<React.CSSProperties>(() => {
|
2020-11-18 18:32:00 +08:00
|
|
|
if (!offset) {
|
|
|
|
return { ...style };
|
2018-10-16 15:07:24 +08:00
|
|
|
}
|
|
|
|
|
2020-11-18 18:32:00 +08:00
|
|
|
const offsetStyle: React.CSSProperties = { marginTop: offset[1] };
|
2020-06-06 13:20:09 +08:00
|
|
|
if (direction === 'rtl') {
|
2020-11-18 18:32:00 +08:00
|
|
|
offsetStyle.left = parseInt(offset[0] as string, 10);
|
|
|
|
} else {
|
|
|
|
offsetStyle.right = -parseInt(offset[0] as string, 10);
|
2020-02-11 12:56:18 +08:00
|
|
|
}
|
|
|
|
|
2020-11-18 18:32:00 +08:00
|
|
|
return {
|
|
|
|
...offsetStyle,
|
|
|
|
...style,
|
|
|
|
};
|
|
|
|
}, [direction, offset, style]);
|
|
|
|
|
|
|
|
// =============================== Render ===============================
|
|
|
|
// >>> Title
|
|
|
|
const titleNode =
|
2021-01-28 18:16:05 +08:00
|
|
|
title ??
|
|
|
|
(typeof livingCount === 'string' || typeof livingCount === 'number' ? livingCount : undefined);
|
2020-11-18 18:32:00 +08:00
|
|
|
|
|
|
|
// >>> Status Text
|
|
|
|
const statusTextNode =
|
|
|
|
isHidden || !text ? null : <span className={`${prefixCls}-status-text`}>{text}</span>;
|
|
|
|
|
|
|
|
// >>> Display Component
|
|
|
|
const displayNode =
|
2021-01-28 18:16:05 +08:00
|
|
|
!livingCount || typeof livingCount !== 'object'
|
2020-11-18 18:32:00 +08:00
|
|
|
? undefined
|
2021-01-28 18:16:05 +08:00
|
|
|
: cloneElement(livingCount, oriProps => ({
|
2020-11-18 18:32:00 +08:00
|
|
|
style: {
|
|
|
|
...mergedStyle,
|
|
|
|
...oriProps.style,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Shared styles
|
2020-05-31 12:38:32 +08:00
|
|
|
const statusCls = classNames({
|
2020-11-18 18:32:00 +08:00
|
|
|
[`${prefixCls}-status-dot`]: hasStatus,
|
2020-05-31 12:38:32 +08:00
|
|
|
[`${prefixCls}-status-${status}`]: !!status,
|
|
|
|
[`${prefixCls}-status-${color}`]: isPresetColor(color),
|
|
|
|
});
|
2020-11-18 18:32:00 +08:00
|
|
|
|
2020-05-31 12:38:32 +08:00
|
|
|
const statusStyle: React.CSSProperties = {};
|
|
|
|
if (color && !isPresetColor(color)) {
|
|
|
|
statusStyle.background = color;
|
|
|
|
}
|
|
|
|
|
2020-09-06 13:07:39 +08:00
|
|
|
const badgeClassName = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
2020-11-18 18:32:00 +08:00
|
|
|
[`${prefixCls}-status`]: hasStatus,
|
2020-09-06 13:07:39 +08:00
|
|
|
[`${prefixCls}-not-a-wrapper`]: !children,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2020-05-31 12:38:32 +08:00
|
|
|
|
|
|
|
// <Badge status="success" />
|
2020-11-18 18:32:00 +08:00
|
|
|
if (!children && hasStatus) {
|
|
|
|
const statusTextColor = mergedStyle.color;
|
2015-11-18 23:42:01 +08:00
|
|
|
return (
|
2020-11-18 18:32:00 +08:00
|
|
|
<span {...restProps} className={badgeClassName} style={mergedStyle}>
|
2020-05-31 12:38:32 +08:00
|
|
|
<span className={statusCls} style={statusStyle} />
|
|
|
|
<span style={{ color: statusTextColor }} className={`${prefixCls}-status-text`}>
|
|
|
|
{text}
|
|
|
|
</span>
|
2015-11-18 23:42:01 +08:00
|
|
|
</span>
|
|
|
|
);
|
2020-05-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
2020-11-18 18:32:00 +08:00
|
|
|
// <Badge status="success" count={<Icon type="xxx" />}></Badge>
|
2020-05-31 12:38:32 +08:00
|
|
|
return (
|
|
|
|
<span {...restProps} className={badgeClassName}>
|
|
|
|
{children}
|
2021-12-06 11:23:34 +08:00
|
|
|
<CSSMotion
|
|
|
|
visible={!isHidden}
|
|
|
|
motionName={`${prefixCls}-zoom`}
|
|
|
|
motionAppear={false}
|
|
|
|
motionDeadline={1000}
|
|
|
|
>
|
2020-11-18 18:32:00 +08:00
|
|
|
{({ className: motionClassName }) => {
|
|
|
|
const scrollNumberPrefixCls = getPrefixCls(
|
|
|
|
'scroll-number',
|
|
|
|
customizeScrollNumberPrefixCls,
|
|
|
|
);
|
|
|
|
|
|
|
|
const isDot = isDotRef.current;
|
|
|
|
|
|
|
|
const scrollNumberCls = classNames({
|
|
|
|
[`${prefixCls}-dot`]: isDot,
|
2021-04-23 10:23:57 +08:00
|
|
|
[`${prefixCls}-count`]: !isDot,
|
|
|
|
[`${prefixCls}-count-sm`]: size === 'small',
|
2020-11-18 18:32:00 +08:00
|
|
|
[`${prefixCls}-multiple-words`]:
|
2021-04-19 14:05:00 +08:00
|
|
|
!isDot && displayCount && displayCount.toString().length > 1,
|
2020-11-18 18:32:00 +08:00
|
|
|
[`${prefixCls}-status-${status}`]: !!status,
|
|
|
|
[`${prefixCls}-status-${color}`]: isPresetColor(color),
|
|
|
|
});
|
|
|
|
|
|
|
|
let scrollNumberStyle: React.CSSProperties = { ...mergedStyle };
|
|
|
|
if (color && !isPresetColor(color)) {
|
|
|
|
scrollNumberStyle = scrollNumberStyle || {};
|
|
|
|
scrollNumberStyle.background = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ScrollNumber
|
|
|
|
prefixCls={scrollNumberPrefixCls}
|
|
|
|
show={!isHidden}
|
2021-01-28 18:16:05 +08:00
|
|
|
motionClassName={motionClassName}
|
|
|
|
className={scrollNumberCls}
|
2020-11-18 18:32:00 +08:00
|
|
|
count={displayCount}
|
|
|
|
title={titleNode}
|
|
|
|
style={scrollNumberStyle}
|
|
|
|
key="scrollNumber"
|
|
|
|
>
|
|
|
|
{displayNode}
|
|
|
|
</ScrollNumber>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</CSSMotion>
|
|
|
|
{statusTextNode}
|
2020-05-31 12:38:32 +08:00
|
|
|
</span>
|
|
|
|
);
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
|
|
|
|
2020-07-08 14:47:07 +08:00
|
|
|
Badge.Ribbon = Ribbon;
|
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
export default Badge;
|