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';
|
2019-08-05 18:38:10 +08:00
|
|
|
import omit from 'omit.js';
|
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';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-04-08 13:41:32 +08:00
|
|
|
import { LiteralUnion } from '../_util/type';
|
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-04-25 20:53:21 +08:00
|
|
|
const Badge: React.FC<BadgeProps> = props => {
|
|
|
|
const getNumberedDisplayCount = () => {
|
|
|
|
const { count, overflowCount } = props;
|
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 => {
|
|
|
|
const { status, color } = props;
|
|
|
|
return !!status || !!color;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isZero = () => {
|
|
|
|
const numberedDisplayCount = getNumberedDisplayCount();
|
|
|
|
return numberedDisplayCount === '0' || numberedDisplayCount === 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isDot = () => {
|
|
|
|
const { dot } = props;
|
|
|
|
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 = () => {
|
|
|
|
const { title, count } = props;
|
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 = () => {
|
|
|
|
const { offset, style } = props;
|
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 getBadgeClassName = (prefixCls: string, direction: string = 'ltr') => {
|
|
|
|
const { className, children } = props;
|
2019-08-05 18:38:10 +08:00
|
|
|
return classNames(className, prefixCls, {
|
2020-04-25 20:53:21 +08:00
|
|
|
[`${prefixCls}-status`]: hasStatus(),
|
2019-08-05 18:38:10 +08:00
|
|
|
[`${prefixCls}-not-a-wrapper`]: !children,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2019-08-05 18:38:10 +08:00
|
|
|
}) as string;
|
2020-04-25 20:53:21 +08:00
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const isHidden = () => {
|
|
|
|
const { showZero } = props;
|
|
|
|
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-04-25 20:53:21 +08:00
|
|
|
const renderStatusText = (prefixCls: string) => {
|
|
|
|
const { text } = props;
|
|
|
|
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 = () => {
|
|
|
|
const { count } = props;
|
2018-12-18 18:31:12 +08:00
|
|
|
const customNode = count as React.ReactElement<any>;
|
|
|
|
if (!customNode || typeof customNode !== 'object') {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return React.cloneElement(customNode, {
|
|
|
|
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-04-25 20:53:21 +08:00
|
|
|
const renderBadgeNumber = (prefixCls: string, scrollNumberPrefixCls: string) => {
|
|
|
|
const { status, count, color } = props;
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const displayCount = getDisplayCount();
|
|
|
|
const dot = isDot();
|
|
|
|
const hidden = isHidden();
|
2018-10-16 15:07:24 +08:00
|
|
|
|
|
|
|
const scrollNumberCls = classNames({
|
2020-04-25 20:53:21 +08:00
|
|
|
[`${prefixCls}-dot`]: dot,
|
|
|
|
[`${prefixCls}-count`]: !dot,
|
2018-12-07 20:02:01 +08:00
|
|
|
[`${prefixCls}-multiple-words`]:
|
2020-04-25 20:53:21 +08:00
|
|
|
!dot && 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-04-25 20:53:21 +08:00
|
|
|
const renderBadge = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2017-03-01 11:16:58 +08:00
|
|
|
const {
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
scrollNumberPrefixCls: customizeScrollNumberPrefixCls,
|
2017-03-01 11:16:58 +08:00
|
|
|
children,
|
|
|
|
status,
|
|
|
|
text,
|
2019-03-31 20:21:20 +08:00
|
|
|
color,
|
2018-06-02 20:12:09 +08:00
|
|
|
...restProps
|
2020-04-25 20:53:21 +08:00
|
|
|
} = props;
|
2019-08-05 18:38:10 +08:00
|
|
|
const omitArr = [
|
|
|
|
'count',
|
|
|
|
'showZero',
|
|
|
|
'overflowCount',
|
|
|
|
'className',
|
|
|
|
'style',
|
|
|
|
'dot',
|
|
|
|
'offset',
|
|
|
|
'title',
|
|
|
|
];
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('badge', customizePrefixCls);
|
|
|
|
const scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls);
|
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
const scrollNumber = renderBadgeNumber(prefixCls, scrollNumberPrefixCls);
|
|
|
|
const statusText = renderStatusText(prefixCls);
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2017-11-12 13:58:12 +08:00
|
|
|
const statusCls = classNames({
|
2020-04-25 20:53:21 +08:00
|
|
|
[`${prefixCls}-status-dot`]: hasStatus(),
|
2017-11-12 14:10:41 +08:00
|
|
|
[`${prefixCls}-status-${status}`]: !!status,
|
2019-03-31 20:21:20 +08:00
|
|
|
[`${prefixCls}-status-${color}`]: isPresetColor(color),
|
2017-11-12 14:10:41 +08:00
|
|
|
});
|
2019-03-31 20:21:20 +08:00
|
|
|
const statusStyle: React.CSSProperties = {};
|
|
|
|
if (color && !isPresetColor(color)) {
|
|
|
|
statusStyle.background = color;
|
|
|
|
}
|
2018-10-16 15:07:24 +08:00
|
|
|
|
2016-10-12 19:42:10 +08:00
|
|
|
// <Badge status="success" />
|
2020-04-25 20:53:21 +08:00
|
|
|
if (!children && hasStatus()) {
|
|
|
|
const styleWithOffset = getStyleWithOffset();
|
2019-03-14 15:53:51 +08:00
|
|
|
const statusTextColor = styleWithOffset && styleWithOffset.color;
|
2016-10-12 19:42:10 +08:00
|
|
|
return (
|
2019-08-05 18:38:10 +08:00
|
|
|
<span
|
|
|
|
{...omit(restProps, omitArr)}
|
2020-04-25 20:53:21 +08:00
|
|
|
className={getBadgeClassName(prefixCls, direction)}
|
2019-08-05 18:38:10 +08:00
|
|
|
style={styleWithOffset}
|
|
|
|
>
|
2019-03-31 20:21:20 +08:00
|
|
|
<span className={statusCls} style={statusStyle} />
|
2019-03-14 15:53:51 +08:00
|
|
|
<span style={{ color: statusTextColor }} className={`${prefixCls}-status-text`}>
|
|
|
|
{text}
|
|
|
|
</span>
|
2016-10-12 19:42:10 +08:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-18 23:42:01 +08:00
|
|
|
return (
|
2020-04-25 20:53:21 +08:00
|
|
|
<span {...omit(restProps, omitArr)} className={getBadgeClassName(prefixCls, direction)}>
|
2015-12-18 17:27:22 +08:00
|
|
|
{children}
|
2016-07-08 17:17:57 +08:00
|
|
|
<Animate
|
|
|
|
component=""
|
2015-11-18 23:42:01 +08:00
|
|
|
showProp="data-show"
|
2016-11-03 16:58:51 +08:00
|
|
|
transitionName={children ? `${prefixCls}-zoom` : ''}
|
2017-05-31 15:48:11 +08:00
|
|
|
transitionAppear
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-11-25 12:03:39 +08:00
|
|
|
{scrollNumber}
|
2015-11-18 23:42:01 +08:00
|
|
|
</Animate>
|
2016-11-25 12:03:39 +08:00
|
|
|
{statusText}
|
2015-11-18 23:42:01 +08:00
|
|
|
</span>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-04-25 20:53:21 +08:00
|
|
|
return <ConfigConsumer>{renderBadge}</ConfigConsumer>;
|
|
|
|
};
|
|
|
|
|
|
|
|
Badge.defaultProps = {
|
|
|
|
count: null,
|
|
|
|
showZero: false,
|
|
|
|
dot: false,
|
|
|
|
overflowCount: 99,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Badge;
|