1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| import React, { PureComponent, Component } from 'react'; import cx from 'classnames'; import PropTypes from 'prop-types';
export default class Badge extends (PureComponent || Component) { static propTypes = { count: PropTypes.number, maxCount: PropTypes.number, dot: PropTypes.bool, showZero: PropTypes.bool, children: PropTypes.node, className: PropTypes.string, prefix: PropTypes.string };
static defaultProps = { count: 0, maxCount: 99, dot: false, showZero: false, className: '', prefix: 'zent' };
render() { const { count, maxCount, dot, showZero, className, prefix, children } = this.props; const containerCls = cx({ [`${prefix}-badge`]: true, [`${prefix}-badge-none-cont`]: !children, [className]: !!className });
const renderCount = () => { let countEle = null; if (dot) { countEle = ( <span className={`${prefix}-badge-count ${prefix}-badge-dot`} /> ); } else if (count > 0 || (count === 0 && showZero)) { countEle = ( <span className={`${prefix}-badge-count`}> {} {count > maxCount ? `${maxCount}+` : count} </span> ); } return countEle; };
return ( <div className={containerCls}> {children ? ( <div className={`${prefix}-badge-content`}>{children}</div> ) : null} {} {renderCount()} </div> ); } }
|