ant-design/components/badge/index.tsx

205 lines
5.7 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2015-11-18 23:42:01 +08:00
import Animate from 'rc-animate';
import classNames from 'classnames';
import ScrollNumber from './ScrollNumber';
import { PresetColorTypes, PresetColorType, PresetStatusColorType } from '../_util/colors';
import { ConfigContext } from '../config-provider';
import { LiteralUnion } from '../_util/type';
import { cloneElement } from '../_util/reactNode';
2015-09-01 13:59:07 +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 */
count?: React.ReactNode;
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;
scrollNumberPrefixCls?: string;
2016-07-14 13:29:50 +08:00
className?: string;
status?: PresetStatusColorType;
color?: LiteralUnion<PresetColorType, string>;
text?: React.ReactNode;
2017-11-03 11:37:35 +08:00
offset?: [number | string, number | string];
title?: string;
2016-07-14 13:29:50 +08:00
}
function isPresetColor(color?: string): boolean {
return (PresetColorTypes as any[]).indexOf(color) !== -1;
}
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);
const getNumberedDisplayCount = () => {
2018-12-07 20:02:01 +08:00
const displayCount =
(count as number) > (overflowCount as number) ? `${overflowCount}+` : count;
return displayCount as string | number | null;
};
const hasStatus = (): boolean => {
return !!status || !!color;
};
const isZero = () => {
const numberedDisplayCount = getNumberedDisplayCount();
return numberedDisplayCount === '0' || numberedDisplayCount === 0;
};
const isDot = () => {
return (dot && !isZero()) || hasStatus();
};
const getDisplayCount = () => {
// dot mode don't need count
if (isDot()) {
return '';
}
return getNumberedDisplayCount();
};
const getScrollNumberTitle = () => {
if (title) {
return title;
}
2018-12-07 20:02:01 +08:00
return typeof count === 'string' || typeof count === 'number' ? count : undefined;
};
const getStyleWithOffset = () => {
2018-12-07 20:02:01 +08:00
return offset
? {
right: -parseInt(offset[0] as string, 10),
marginTop: offset[1],
...style,
}
: style;
};
const isHidden = () => {
const displayCount = getDisplayCount();
2019-08-05 18:38:10 +08:00
const isEmpty = displayCount === null || displayCount === undefined || displayCount === '';
return (isEmpty || (isZero() && !showZero)) && !isDot();
};
2019-08-05 18:38:10 +08:00
const renderStatusText = () => {
const hidden = isHidden();
2018-12-07 20:02:01 +08:00
return hidden || !text ? null : <span className={`${prefixCls}-status-text`}>{text}</span>;
};
const renderDisplayComponent = () => {
const customNode = count as React.ReactElement<any>;
if (!customNode || typeof customNode !== 'object') {
return undefined;
}
return cloneElement(customNode, {
style: {
...getStyleWithOffset(),
...(customNode.props && customNode.props.style),
},
});
};
const renderBadgeNumber = () => {
const scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls);
const displayCount = getDisplayCount();
const bDot = isDot();
const hidden = isHidden();
const scrollNumberCls = classNames({
[`${prefixCls}-dot`]: bDot,
[`${prefixCls}-count`]: !bDot,
2018-12-07 20:02:01 +08:00
[`${prefixCls}-multiple-words`]:
!bDot && count && count.toString && count.toString().length > 1,
[`${prefixCls}-status-${status}`]: !!status,
[`${prefixCls}-status-${color}`]: isPresetColor(color),
});
let statusStyle: React.CSSProperties | undefined = getStyleWithOffset();
if (color && !isPresetColor(color)) {
statusStyle = statusStyle || {};
statusStyle.background = color;
}
return hidden ? null : (
<ScrollNumber
prefixCls={scrollNumberPrefixCls}
data-show={!hidden}
className={scrollNumberCls}
count={displayCount}
displayComponent={renderDisplayComponent()} // <Badge status="success" count={<Icon type="xxx" />}></Badge>
title={getScrollNumberTitle()}
style={statusStyle}
key="scrollNumber"
/>
);
};
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 (
<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>
);
}
return (
<span {...restProps} className={badgeClassName}>
{children}
<Animate
component=""
showProp="data-show"
transitionName={children ? `${prefixCls}-zoom` : ''}
transitionAppear
>
{renderBadgeNumber()}
</Animate>
{renderStatusText()}
</span>
);
};
export default Badge;