2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-05-09 13:40:33 +08:00
|
|
|
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';
|
2015-12-18 17:27:22 +08:00
|
|
|
import classNames from 'classnames';
|
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 */
|
2017-05-29 18:14:16 +08:00
|
|
|
count?: number | string;
|
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;
|
2016-08-20 17:43:46 +08:00
|
|
|
status?: 'success' | 'processing' | 'default' | 'error' | 'warning';
|
|
|
|
text?: string;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export default class Badge extends React.Component<BadgeProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-badge',
|
2017-10-14 14:29:18 +08:00
|
|
|
scrollNumberPrefixCls: 'ant-scroll-number',
|
2016-03-29 14:01:10 +08:00
|
|
|
count: null,
|
2017-03-01 11:16:58 +08:00
|
|
|
showZero: false,
|
2016-03-29 14:01:10 +08:00
|
|
|
dot: false,
|
|
|
|
overflowCount: 99,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
2017-03-01 11:16:58 +08:00
|
|
|
count: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
2016-03-29 14:01:10 +08:00
|
|
|
]),
|
2017-03-01 11:16:58 +08:00
|
|
|
showZero: PropTypes.bool,
|
|
|
|
dot: PropTypes.bool,
|
|
|
|
overflowCount: PropTypes.number,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2015-09-01 13:59:07 +08:00
|
|
|
render() {
|
2017-03-01 11:16:58 +08:00
|
|
|
const {
|
|
|
|
count,
|
|
|
|
showZero,
|
|
|
|
prefixCls,
|
2017-10-14 14:29:18 +08:00
|
|
|
scrollNumberPrefixCls,
|
2017-03-01 11:16:58 +08:00
|
|
|
overflowCount,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
children,
|
|
|
|
dot,
|
|
|
|
status,
|
|
|
|
text,
|
2017-11-03 11:37:35 +08:00
|
|
|
offset,
|
2018-04-10 01:09:18 +08:00
|
|
|
title,
|
2017-03-01 11:16:58 +08:00
|
|
|
...restProps,
|
|
|
|
} = this.props;
|
2017-05-29 19:06:26 +08:00
|
|
|
let displayCount = (count as number) > (overflowCount as number) ? `${overflowCount}+` : count;
|
2018-02-24 11:40:44 +08:00
|
|
|
const isZero = displayCount === '0' || displayCount === 0;
|
|
|
|
const isDot = (dot && !isZero) || status;
|
2015-11-19 00:13:16 +08:00
|
|
|
// dot mode don't need count
|
2016-08-20 17:43:46 +08:00
|
|
|
if (isDot) {
|
2016-12-19 15:19:15 +08:00
|
|
|
displayCount = '';
|
2015-11-19 00:13:16 +08:00
|
|
|
}
|
2017-03-01 11:16:58 +08:00
|
|
|
const isEmpty = displayCount === null || displayCount === undefined || displayCount === '';
|
|
|
|
const hidden = (isEmpty || (isZero && !showZero)) && !isDot;
|
2017-11-12 13:58:12 +08:00
|
|
|
const statusCls = classNames({
|
|
|
|
[`${prefixCls}-status-dot`]: !!status,
|
2017-11-12 14:10:41 +08:00
|
|
|
[`${prefixCls}-status-${status}`]: !!status,
|
|
|
|
});
|
2016-08-20 17:43:46 +08:00
|
|
|
const scrollNumberCls = classNames({
|
|
|
|
[`${prefixCls}-dot`]: isDot,
|
|
|
|
[`${prefixCls}-count`]: !isDot,
|
2018-02-24 11:40:44 +08:00
|
|
|
[`${prefixCls}-multiple-words`]: !isDot && count && count.toString && count.toString().length > 1,
|
2017-11-12 14:10:41 +08:00
|
|
|
[`${prefixCls}-status-${status}`]: !!status,
|
|
|
|
});
|
2016-11-30 10:20:23 +08:00
|
|
|
const badgeCls = classNames(className, prefixCls, {
|
2016-10-12 19:42:10 +08:00
|
|
|
[`${prefixCls}-status`]: !!status,
|
2015-12-18 17:27:22 +08:00
|
|
|
[`${prefixCls}-not-a-wrapper`]: !children,
|
|
|
|
});
|
2017-11-03 11:37:35 +08:00
|
|
|
const styleWithOffset = offset ? {
|
|
|
|
marginTop: offset[0],
|
|
|
|
marginLeft: offset[1],
|
|
|
|
...style,
|
|
|
|
} : style;
|
2016-10-12 19:42:10 +08:00
|
|
|
// <Badge status="success" />
|
|
|
|
if (!children && status) {
|
|
|
|
return (
|
2018-05-23 13:11:49 +08:00
|
|
|
<span {...restProps} className={badgeCls} style={styleWithOffset}>
|
2016-10-12 19:42:10 +08:00
|
|
|
<span className={statusCls} />
|
|
|
|
<span className={`${prefixCls}-status-text`}>{text}</span>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:03:39 +08:00
|
|
|
const scrollNumber = hidden ? null : (
|
|
|
|
<ScrollNumber
|
2017-10-14 14:29:18 +08:00
|
|
|
prefixCls={scrollNumberPrefixCls}
|
2016-11-25 12:03:39 +08:00
|
|
|
data-show={!hidden}
|
|
|
|
className={scrollNumberCls}
|
2016-12-19 15:19:15 +08:00
|
|
|
count={displayCount}
|
2018-04-10 01:09:18 +08:00
|
|
|
title={title || count}
|
2017-11-03 11:37:35 +08:00
|
|
|
style={styleWithOffset}
|
2018-05-25 17:34:40 +08:00
|
|
|
key="scrollNumber"
|
2016-11-25 12:03:39 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
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}>
|
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>
|
|
|
|
);
|
2015-09-01 13:59:07 +08:00
|
|
|
}
|
|
|
|
}
|