2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-11-18 23:42:01 +08:00
|
|
|
import Animate from 'rc-animate';
|
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-04-08 13:41:32 +08:00
|
|
|
import { PresetColorTypes, 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';
|
2015-09-01 13:59:07 +08:00
|
|
|
|
2017-09-25 22:14:49 +08:00
|
|
|
export { ScrollNumberProps } from './ScrollNumber';
|
|
|
|
|
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;
|
|
|
|
/** whether to show red dot without number */
|
|
|
|
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;
|
2017-11-03 11:37:35 +08:00
|
|
|
offset?: [number | string, number | string];
|
2018-04-10 01:09:18 +08:00
|
|
|
title?: string;
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
|
|
|
|
2019-03-31 20:21:20 +08:00
|
|
|
function isPresetColor(color?: string): boolean {
|
|
|
|
return (PresetColorTypes as any[]).indexOf(color) !== -1;
|
|
|
|
}
|
|
|
|
|
2020-05-31 12:38:32 +08:00
|
|
|
const Badge: React.FC<BadgeProps> = ({
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
scrollNumberPrefixCls: customizeScrollNumberPrefixCls,
|
|
|
|
children,
|
|
|
|
status,
|
|
|
|
text,
|
|
|
|
color,
|
|
|
|
count = null,
|
|
|
|
overflowCount = 99,
|
|
|
|
dot = false,
|
|
|
|
title,
|
|
|
|
offset,
|
|
|
|
style,
|
|
|
|
className,
|
|
|
|
showZero = false,
|
|
|
|
...restProps
|
|
|
|
}) => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('badge', customizePrefixCls);
|
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const getNumberedDisplayCount = () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
const displayCount =
|
|
|
|
(count as number) > (overflowCount as number) ? `${overflowCount}+` : count;
|
2018-10-16 15:07:24 +08:00
|
|
|
return displayCount as string | number | null;
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const hasStatus = (): boolean => {
|
|
|
|
return !!status || !!color;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isZero = () => {
|
|
|
|
const numberedDisplayCount = getNumberedDisplayCount();
|
|
|
|
return numberedDisplayCount === '0' || numberedDisplayCount === 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isDot = () => {
|
|
|
|
return (dot && !isZero()) || hasStatus();
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDisplayCount = () => {
|
2018-10-16 15:07:24 +08:00
|
|
|
// dot mode don't need count
|
2020-04-25 20:53:21 +08:00
|
|
|
if (isDot()) {
|
2018-10-16 15:07:24 +08:00
|
|
|
return '';
|
|
|
|
}
|
2020-04-25 20:53:21 +08:00
|
|
|
return getNumberedDisplayCount();
|
|
|
|
};
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const getScrollNumberTitle = () => {
|
2018-10-16 15:07:24 +08:00
|
|
|
if (title) {
|
|
|
|
return title;
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
return typeof count === 'string' || typeof count === 'number' ? count : undefined;
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const getStyleWithOffset = () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
return offset
|
|
|
|
? {
|
|
|
|
right: -parseInt(offset[0] as string, 10),
|
|
|
|
marginTop: offset[1],
|
|
|
|
...style,
|
|
|
|
}
|
|
|
|
: style;
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const isHidden = () => {
|
|
|
|
const displayCount = getDisplayCount();
|
2019-08-05 18:38:10 +08:00
|
|
|
const isEmpty = displayCount === null || displayCount === undefined || displayCount === '';
|
2020-04-25 20:53:21 +08:00
|
|
|
return (isEmpty || (isZero() && !showZero)) && !isDot();
|
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-05-31 12:38:32 +08:00
|
|
|
const renderStatusText = () => {
|
2020-04-25 20:53:21 +08:00
|
|
|
const hidden = isHidden();
|
2018-12-07 20:02:01 +08:00
|
|
|
return hidden || !text ? null : <span className={`${prefixCls}-status-text`}>{text}</span>;
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const renderDisplayComponent = () => {
|
2018-12-18 18:31:12 +08:00
|
|
|
const customNode = count as React.ReactElement<any>;
|
|
|
|
if (!customNode || typeof customNode !== 'object') {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-05-14 20:54:49 +08:00
|
|
|
return cloneElement(customNode, {
|
2018-12-18 18:31:12 +08:00
|
|
|
style: {
|
2020-04-25 20:53:21 +08:00
|
|
|
...getStyleWithOffset(),
|
2018-12-18 18:31:12 +08:00
|
|
|
...(customNode.props && customNode.props.style),
|
|
|
|
},
|
|
|
|
});
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-05-31 12:38:32 +08:00
|
|
|
const renderBadgeNumber = () => {
|
|
|
|
const scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls);
|
2020-04-25 20:53:21 +08:00
|
|
|
const displayCount = getDisplayCount();
|
2020-05-31 12:38:32 +08:00
|
|
|
const bDot = isDot();
|
2020-04-25 20:53:21 +08:00
|
|
|
const hidden = isHidden();
|
2018-10-16 15:07:24 +08:00
|
|
|
|
|
|
|
const scrollNumberCls = classNames({
|
2020-05-31 12:38:32 +08:00
|
|
|
[`${prefixCls}-dot`]: bDot,
|
|
|
|
[`${prefixCls}-count`]: !bDot,
|
2018-12-07 20:02:01 +08:00
|
|
|
[`${prefixCls}-multiple-words`]:
|
2020-05-31 12:38:32 +08:00
|
|
|
!bDot && count && count.toString && count.toString().length > 1,
|
2020-02-11 12:56:18 +08:00
|
|
|
[`${prefixCls}-status-${status}`]: !!status,
|
|
|
|
[`${prefixCls}-status-${color}`]: isPresetColor(color),
|
2018-10-16 15:07:24 +08:00
|
|
|
});
|
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
let statusStyle: React.CSSProperties | undefined = getStyleWithOffset();
|
2020-02-11 12:56:18 +08:00
|
|
|
if (color && !isPresetColor(color)) {
|
|
|
|
statusStyle = statusStyle || {};
|
|
|
|
statusStyle.background = color;
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:07:24 +08:00
|
|
|
return hidden ? null : (
|
|
|
|
<ScrollNumber
|
|
|
|
prefixCls={scrollNumberPrefixCls}
|
|
|
|
data-show={!hidden}
|
|
|
|
className={scrollNumberCls}
|
|
|
|
count={displayCount}
|
2020-04-25 20:53:21 +08:00
|
|
|
displayComponent={renderDisplayComponent()} // <Badge status="success" count={<Icon type="xxx" />}></Badge>
|
|
|
|
title={getScrollNumberTitle()}
|
2020-02-11 12:56:18 +08:00
|
|
|
style={statusStyle}
|
2018-10-16 15:07:24 +08:00
|
|
|
key="scrollNumber"
|
|
|
|
/>
|
|
|
|
);
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-05-31 12:38:32 +08:00
|
|
|
const statusCls = classNames({
|
|
|
|
[`${prefixCls}-status-dot`]: hasStatus(),
|
|
|
|
[`${prefixCls}-status-${status}`]: !!status,
|
|
|
|
[`${prefixCls}-status-${color}`]: isPresetColor(color),
|
|
|
|
});
|
|
|
|
const statusStyle: React.CSSProperties = {};
|
|
|
|
if (color && !isPresetColor(color)) {
|
|
|
|
statusStyle.background = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
const badgeClassName = classNames(className, prefixCls, {
|
|
|
|
[`${prefixCls}-status`]: hasStatus(),
|
|
|
|
[`${prefixCls}-not-a-wrapper`]: !children,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
|
|
|
|
|
|
|
// <Badge status="success" />
|
|
|
|
if (!children && hasStatus()) {
|
|
|
|
const styleWithOffset = getStyleWithOffset();
|
|
|
|
const statusTextColor = styleWithOffset && styleWithOffset.color;
|
2015-11-18 23:42:01 +08:00
|
|
|
return (
|
2020-05-31 12:38:32 +08:00
|
|
|
<span {...restProps} className={badgeClassName} style={styleWithOffset}>
|
|
|
|
<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
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span {...restProps} className={badgeClassName}>
|
|
|
|
{children}
|
|
|
|
<Animate
|
|
|
|
component=""
|
|
|
|
showProp="data-show"
|
|
|
|
transitionName={children ? `${prefixCls}-zoom` : ''}
|
|
|
|
transitionAppear
|
|
|
|
>
|
|
|
|
{renderBadgeNumber()}
|
|
|
|
</Animate>
|
|
|
|
{renderStatusText()}
|
|
|
|
</span>
|
|
|
|
);
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Badge;
|