2016-09-21 11:54:53 +08:00
|
|
|
import React from 'react';
|
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';
|
2016-11-01 11:10:11 +08:00
|
|
|
import warning from '../_util/warning';
|
2016-09-21 22:01:54 +08:00
|
|
|
import splitObject from '../_util/splitObject';
|
2015-09-01 13:59:07 +08:00
|
|
|
|
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;
|
|
|
|
/** Max count to show */
|
|
|
|
overflowCount?: number;
|
|
|
|
/** whether to show red dot without number */
|
|
|
|
dot?: boolean;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2016-08-20 17:43:46 +08:00
|
|
|
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> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-badge',
|
|
|
|
count: null,
|
|
|
|
dot: false,
|
|
|
|
overflowCount: 99,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
count: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
2016-05-11 09:32:33 +08:00
|
|
|
React.PropTypes.number,
|
2016-03-29 14:01:10 +08:00
|
|
|
]),
|
|
|
|
dot: React.PropTypes.bool,
|
|
|
|
overflowCount: React.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() {
|
2016-09-21 22:01:54 +08:00
|
|
|
let [{
|
|
|
|
count, prefixCls, overflowCount, className, style, children, dot, status, text,
|
|
|
|
}, restProps] = splitObject(
|
|
|
|
this.props,
|
|
|
|
['count', 'prefixCls', 'overflowCount', 'className', 'style', 'children', 'dot', 'status', 'text']
|
|
|
|
);
|
2016-08-20 17:43:46 +08:00
|
|
|
const isDot = dot || status;
|
2016-10-30 12:44:47 +08:00
|
|
|
const realCount = count;
|
2015-12-18 17:27:22 +08:00
|
|
|
count = count > overflowCount ? `${overflowCount}+` : count;
|
2015-11-19 00:13:16 +08:00
|
|
|
|
|
|
|
// dot mode don't need count
|
2016-08-20 17:43:46 +08:00
|
|
|
if (isDot) {
|
2015-11-19 00:13:16 +08:00
|
|
|
count = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// null undefined "" "0" 0
|
2016-08-20 17:43:46 +08:00
|
|
|
const hidden = (!count || count === '0') && !isDot;
|
|
|
|
const scrollNumberCls = classNames({
|
|
|
|
[`${prefixCls}-dot`]: isDot,
|
|
|
|
[`${prefixCls}-count`]: !isDot,
|
|
|
|
});
|
2015-12-18 17:27:22 +08:00
|
|
|
const badgeCls = classNames({
|
|
|
|
[className]: !!className,
|
|
|
|
[prefixCls]: true,
|
2016-10-12 19:42:10 +08:00
|
|
|
[`${prefixCls}-status`]: !!status,
|
2015-12-18 17:27:22 +08:00
|
|
|
[`${prefixCls}-not-a-wrapper`]: !children,
|
|
|
|
});
|
2015-11-19 00:13:16 +08:00
|
|
|
|
2016-10-19 09:45:46 +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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-18 23:42:01 +08:00
|
|
|
return (
|
2016-10-30 12:44:47 +08:00
|
|
|
<span {...restProps} className={badgeCls} title={realCount}>
|
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` : ''}
|
2016-06-06 13:54:10 +08:00
|
|
|
transitionAppear
|
|
|
|
>
|
2015-11-19 00:13:16 +08:00
|
|
|
{
|
|
|
|
hidden ? null :
|
2016-07-08 17:17:57 +08:00
|
|
|
<ScrollNumber
|
|
|
|
data-show={!hidden}
|
|
|
|
className={scrollNumberCls}
|
|
|
|
count={count}
|
|
|
|
style={style}
|
2016-06-06 13:54:10 +08:00
|
|
|
/>
|
2015-11-19 00:13:16 +08:00
|
|
|
}
|
2015-11-18 23:42:01 +08:00
|
|
|
</Animate>
|
2016-08-20 17:43:46 +08:00
|
|
|
{
|
|
|
|
hidden || !text ? null :
|
|
|
|
<span className={`${prefixCls}-status-text`}>{text}</span>
|
|
|
|
}
|
2015-11-18 23:42:01 +08:00
|
|
|
</span>
|
|
|
|
);
|
2015-09-01 13:59:07 +08:00
|
|
|
}
|
|
|
|
}
|