ant-design/components/badge/index.tsx

130 lines
3.4 KiB
TypeScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
2015-11-18 23:42:01 +08:00
import Animate from 'rc-animate';
2015-11-23 22:58:11 +08:00
import ScrollNumber from './ScrollNumber';
import classNames from 'classnames';
import warning from '../_util/warning';
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?: number | string;
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';
text?: string;
2016-07-14 13:29:50 +08:00
}
export default class Badge extends React.Component<BadgeProps, any> {
static defaultProps = {
prefixCls: 'ant-badge',
scrollNumberPrefixCls: 'ant-scroll-number',
count: null,
showZero: false,
dot: false,
overflowCount: 99,
2016-07-13 11:14:24 +08:00
};
static propTypes = {
count: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
showZero: PropTypes.bool,
dot: PropTypes.bool,
overflowCount: PropTypes.number,
2016-07-13 11:14:24 +08:00
};
2015-09-01 13:59:07 +08:00
render() {
const {
count,
showZero,
prefixCls,
scrollNumberPrefixCls,
overflowCount,
className,
style,
children,
dot,
status,
text,
...restProps,
} = this.props;
const isDot = dot || status;
2017-05-29 19:06:26 +08:00
let displayCount = (count as number) > (overflowCount as number) ? `${overflowCount}+` : count;
2015-11-19 00:13:16 +08:00
// dot mode don't need count
if (isDot) {
2016-12-19 15:19:15 +08:00
displayCount = '';
2015-11-19 00:13:16 +08:00
}
const isZero = displayCount === '0' || displayCount === 0;
const isEmpty = displayCount === null || displayCount === undefined || displayCount === '';
const hidden = (isEmpty || (isZero && !showZero)) && !isDot;
const scrollNumberCls = classNames({
[`${prefixCls}-dot`]: isDot,
[`${prefixCls}-count`]: !isDot,
2017-10-18 17:19:42 +08:00
[`${prefixCls}-multiple-words`]: count && count.toString && count.toString().length > 1,
});
const badgeCls = classNames(className, prefixCls, {
2016-10-12 19:42:10 +08:00
[`${prefixCls}-status`]: !!status,
[`${prefixCls}-not-a-wrapper`]: !children,
});
2015-11-19 00:13:16 +08:00
warning(
!(children && status),
'`Badge[children]` and `Badge[status]` cannot be used at the same time.',
);
2016-10-12 19:42:10 +08:00
// <Badge status="success" />
if (!children && status) {
const statusCls = classNames({
[`${prefixCls}-status-dot`]: !!status,
[`${prefixCls}-status-${status}`]: true,
});
return (
<span className={badgeCls}>
<span className={statusCls} />
<span className={`${prefixCls}-status-text`}>{text}</span>
</span>
);
}
const scrollNumber = hidden ? null : (
<ScrollNumber
prefixCls={scrollNumberPrefixCls}
data-show={!hidden}
className={scrollNumberCls}
2016-12-19 15:19:15 +08:00
count={displayCount}
2017-08-31 10:55:25 +08:00
title={count}
style={style}
/>
);
const statusText = (hidden || !text) ? null : (
<span className={`${prefixCls}-status-text`}>{text}</span>
);
2015-11-18 23:42:01 +08:00
return (
2017-07-31 18:01:45 +08:00
<span {...restProps} className={badgeCls}>
{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>
);
2015-09-01 13:59:07 +08:00
}
}