ant-design/components/badge/index.tsx

242 lines
6.7 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
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';
import classNames from 'classnames';
import ScrollNumber from './ScrollNumber';
import { PresetColorTypes } from '../_util/colors';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
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?: 'success' | 'processing' | 'default' | 'error' | 'warning';
color?: 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;
}
2016-07-14 13:29:50 +08:00
export default class Badge extends React.Component<BadgeProps, any> {
static defaultProps = {
count: null,
showZero: false,
dot: false,
overflowCount: 99,
2016-07-13 11:14:24 +08:00
};
static propTypes = {
count: PropTypes.node,
showZero: PropTypes.bool,
dot: PropTypes.bool,
overflowCount: PropTypes.number,
2016-07-13 11:14:24 +08:00
};
getNumberedDispayCount() {
const { count, overflowCount } = this.props;
2018-12-07 20:02:01 +08:00
const displayCount =
(count as number) > (overflowCount as number) ? `${overflowCount}+` : count;
return displayCount as string | number | null;
}
getDispayCount() {
const isDot = this.isDot();
// dot mode don't need count
if (isDot) {
return '';
}
return this.getNumberedDispayCount();
}
2018-12-28 20:59:49 +08:00
getScrollNumberTitle() {
const { title, count } = this.props;
if (title) {
return title;
}
2018-12-07 20:02:01 +08:00
return typeof count === 'string' || typeof count === 'number' ? count : undefined;
}
getStyleWithOffset() {
const { offset, style } = this.props;
2018-12-07 20:02:01 +08:00
return offset
? {
right: -parseInt(offset[0] as string, 10),
marginTop: offset[1],
...style,
}
: style;
}
2019-08-05 18:38:10 +08:00
getBadgeClassName(prefixCls: string) {
const { className, children } = this.props;
return classNames(className, prefixCls, {
[`${prefixCls}-status`]: this.hasStatus(),
[`${prefixCls}-not-a-wrapper`]: !children,
}) as string;
}
hasStatus(): boolean {
const { status, color } = this.props;
return !!status || !!color;
}
isZero() {
const numberedDispayCount = this.getNumberedDispayCount();
return numberedDispayCount === '0' || numberedDispayCount === 0;
}
isDot() {
const { dot } = this.props;
const isZero = this.isZero();
return (dot && !isZero) || this.hasStatus();
}
isHidden() {
const { showZero } = this.props;
const displayCount = this.getDispayCount();
const isZero = this.isZero();
const isDot = this.isDot();
const isEmpty = displayCount === null || displayCount === undefined || displayCount === '';
return (isEmpty || (isZero && !showZero)) && !isDot;
}
renderStatusText(prefixCls: string) {
const { text } = this.props;
const hidden = this.isHidden();
2018-12-07 20:02:01 +08:00
return hidden || !text ? null : <span className={`${prefixCls}-status-text`}>{text}</span>;
}
renderDispayComponent() {
const { count } = this.props;
const customNode = count as React.ReactElement<any>;
if (!customNode || typeof customNode !== 'object') {
return undefined;
}
return React.cloneElement(customNode, {
style: {
...this.getStyleWithOffset(),
...(customNode.props && customNode.props.style),
},
});
}
renderBadgeNumber(prefixCls: string, scrollNumberPrefixCls: string) {
const { status, count } = this.props;
const displayCount = this.getDispayCount();
const isDot = this.isDot();
const hidden = this.isHidden();
const scrollNumberCls = classNames({
[`${prefixCls}-dot`]: isDot,
[`${prefixCls}-count`]: !isDot,
2018-12-07 20:02:01 +08:00
[`${prefixCls}-multiple-words`]:
!isDot && count && count.toString && count.toString().length > 1,
[`${prefixCls}-status-${status}`]: this.hasStatus(),
});
return hidden ? null : (
<ScrollNumber
prefixCls={scrollNumberPrefixCls}
data-show={!hidden}
className={scrollNumberCls}
count={displayCount}
displayComponent={this.renderDispayComponent()} // <Badge status="success" count={<Icon type="xxx" />}></Badge>
2018-12-28 20:59:49 +08:00
title={this.getScrollNumberTitle()}
style={this.getStyleWithOffset()}
key="scrollNumber"
/>
);
}
renderBadge = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
scrollNumberPrefixCls: customizeScrollNumberPrefixCls,
children,
status,
text,
color,
...restProps
} = this.props;
2019-08-05 18:38:10 +08:00
const omitArr = [
'count',
'showZero',
'overflowCount',
'className',
'style',
'dot',
'offset',
'title',
];
const prefixCls = getPrefixCls('badge', customizePrefixCls);
const scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls);
const scrollNumber = this.renderBadgeNumber(prefixCls, scrollNumberPrefixCls);
const statusText = this.renderStatusText(prefixCls);
const statusCls = classNames({
[`${prefixCls}-status-dot`]: this.hasStatus(),
2017-11-12 14:10:41 +08:00
[`${prefixCls}-status-${status}`]: !!status,
[`${prefixCls}-status-${color}`]: isPresetColor(color),
2017-11-12 14:10:41 +08:00
});
const statusStyle: React.CSSProperties = {};
if (color && !isPresetColor(color)) {
statusStyle.background = color;
}
2016-10-12 19:42:10 +08:00
// <Badge status="success" />
if (!children && this.hasStatus()) {
const styleWithOffset = this.getStyleWithOffset();
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)}
className={this.getBadgeClassName(prefixCls)}
style={styleWithOffset}
>
<span className={statusCls} style={statusStyle} />
<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 (
2019-08-05 18:38:10 +08:00
<span {...omit(restProps, omitArr)} className={this.getBadgeClassName(prefixCls)}>
{children}
2016-07-08 17:17:57 +08:00
<Animate
component=""
2015-11-18 23:42:01 +08:00
showProp="data-show"
transitionName={children ? `${prefixCls}-zoom` : ''}
2017-05-31 15:48:11 +08:00
transitionAppear
>
{scrollNumber}
2015-11-18 23:42:01 +08:00
</Animate>
{statusText}
2015-11-18 23:42:01 +08:00
</span>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderBadge}</ConfigConsumer>;
}
2015-09-01 13:59:07 +08:00
}