ant-design/components/badge/index.tsx

91 lines
2.3 KiB
TypeScript
Raw Normal View History

2016-07-07 20:25:03 +08:00
import * as 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';
import classNames from 'classnames';
2015-09-01 13:59:07 +08:00
2016-07-14 13:29:50 +08:00
interface BadgeProps {
/** 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;
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',
count: null,
dot: false,
overflowCount: 99,
// status: 'default',
2016-07-13 11:14:24 +08:00
};
static propTypes = {
count: React.PropTypes.oneOfType([
React.PropTypes.string,
2016-05-11 09:32:33 +08:00
React.PropTypes.number,
]),
dot: React.PropTypes.bool,
overflowCount: React.PropTypes.number,
2016-07-13 11:14:24 +08:00
};
2015-09-01 13:59:07 +08:00
render() {
let { count, prefixCls, overflowCount, className, style, children, dot, status, text } = this.props;
const isDot = dot || status;
2015-11-19 00:13:16 +08:00
count = count > overflowCount ? `${overflowCount}+` : count;
2015-11-19 00:13:16 +08:00
// dot mode don't need count
if (isDot) {
2015-11-19 00:13:16 +08:00
count = '';
}
// null undefined "" "0" 0
const hidden = (!count || count === '0') && !isDot;
const scrollNumberCls = classNames({
[`${prefixCls}-dot`]: isDot,
[`${prefixCls}-count`]: !isDot,
[`${prefixCls}-status`]: status,
[`${prefixCls}-status-${status}`]: status,
[`${prefixCls}-status-with-text`]: text,
});
const badgeCls = classNames({
[className]: !!className,
[prefixCls]: true,
[`${prefixCls}-not-a-wrapper`]: !children,
});
2015-11-19 00:13:16 +08:00
2015-11-18 23:42:01 +08:00
return (
2016-07-08 17:17:57 +08:00
<span className={badgeCls} title={count} style={null}>
{children}
2016-07-08 17:17:57 +08:00
<Animate
component=""
2015-11-18 23:42:01 +08:00
showProp="data-show"
transitionName={`${prefixCls}-zoom`}
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}
/>
2015-11-19 00:13:16 +08:00
}
2015-11-18 23:42:01 +08:00
</Animate>
{
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
}
}