import React from 'react'; import PropTypes from 'prop-types'; import Animate from 'rc-animate'; import ScrollNumber from './ScrollNumber'; import classNames from 'classnames'; import warning from '../_util/warning'; export { ScrollNumberProps } from './ScrollNumber'; export interface BadgeProps { /** Number to show in badge */ count?: number | string; showZero?: boolean; /** Max count to show */ overflowCount?: number; /** whether to show red dot without number */ dot?: boolean; style?: React.CSSProperties; prefixCls?: string; scrollNumberPrefixCls?: string; className?: string; status?: 'success' | 'processing' | 'default' | 'error' | 'warning'; text?: string; offset?: [number | string, number | string]; } export default class Badge extends React.Component { static defaultProps = { prefixCls: 'ant-badge', scrollNumberPrefixCls: 'ant-scroll-number', count: null, showZero: false, dot: false, overflowCount: 99, }; static propTypes = { count: PropTypes.oneOfType([ PropTypes.string, PropTypes.number, ]), showZero: PropTypes.bool, dot: PropTypes.bool, overflowCount: PropTypes.number, }; render() { const { count, showZero, prefixCls, scrollNumberPrefixCls, overflowCount, className, style, children, dot, status, text, offset, ...restProps, } = this.props; const isDot = dot || status; let displayCount = (count as number) > (overflowCount as number) ? `${overflowCount}+` : count; // dot mode don't need count if (isDot) { displayCount = ''; } 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, [`${prefixCls}-multiple-words`]: count && count.toString && count.toString().length > 1, }); const badgeCls = classNames(className, prefixCls, { [`${prefixCls}-status`]: !!status, [`${prefixCls}-not-a-wrapper`]: !children, }); warning( !(children && status), '`Badge[children]` and `Badge[status]` cannot be used at the same time.', ); const styleWithOffset = offset ? { marginTop: offset[0], marginLeft: offset[1], ...style, } : style; // if (!children && status) { const statusCls = classNames({ [`${prefixCls}-status-dot`]: !!status, [`${prefixCls}-status-${status}`]: true, }); return ( {text} ); } const scrollNumber = hidden ? null : ( ); const statusText = (hidden || !text) ? null : ( {text} ); return ( {children} {scrollNumber} {statusText} ); } }