2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import ScrollNumber from './ScrollNumber';
|
2020-03-07 11:45:13 +00:00
|
|
|
import { PresetColorTypes } from '../_util/colors';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-07-07 07:43:02 +00:00
|
|
|
import { initDefaultProps, getComponent, getSlot } from '../_util/props-util';
|
2019-01-12 03:33:27 +00:00
|
|
|
import { cloneElement } from '../_util/vnode';
|
|
|
|
import getTransitionProps from '../_util/getTransitionProps';
|
|
|
|
import isNumeric from '../_util/isNumeric';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-10-13 07:02:35 +00:00
|
|
|
import { inject, Transition, defineComponent, CSSProperties } from 'vue';
|
|
|
|
import { tuple } from '../_util/type';
|
2018-03-09 09:50:33 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const BadgeProps = {
|
2018-03-09 09:50:33 +00:00
|
|
|
/** Number to show in badge */
|
2020-10-13 07:02:35 +00:00
|
|
|
count: PropTypes.VNodeChild,
|
2020-10-10 10:16:28 +00:00
|
|
|
showZero: PropTypes.looseBool,
|
2018-03-09 09:50:33 +00:00
|
|
|
/** Max count to show */
|
|
|
|
overflowCount: PropTypes.number,
|
|
|
|
/** whether to show red dot without number */
|
2020-10-10 10:16:28 +00:00
|
|
|
dot: PropTypes.looseBool,
|
2018-03-09 09:50:33 +00:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
scrollNumberPrefixCls: PropTypes.string,
|
2020-10-13 07:02:35 +00:00
|
|
|
status: PropTypes.oneOf(tuple('success', 'processing', 'default', 'error', 'warning')),
|
2020-03-07 11:45:13 +00:00
|
|
|
color: PropTypes.string,
|
2020-10-13 07:02:35 +00:00
|
|
|
text: PropTypes.VNodeChild,
|
|
|
|
offset: PropTypes.arrayOf(PropTypes.oneOfType([String, Number])),
|
|
|
|
numberStyle: PropTypes.style,
|
2018-09-05 13:28:54 +00:00
|
|
|
title: PropTypes.string,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-10-13 07:02:35 +00:00
|
|
|
function isPresetColor(color?: string): boolean {
|
|
|
|
return (PresetColorTypes as any[]).indexOf(color) !== -1;
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
2020-10-13 07:02:35 +00:00
|
|
|
export default defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ABadge',
|
2018-03-09 09:50:33 +00:00
|
|
|
props: initDefaultProps(BadgeProps, {
|
|
|
|
showZero: false,
|
|
|
|
dot: false,
|
|
|
|
overflowCount: 99,
|
2020-10-13 07:02:35 +00:00
|
|
|
}) as typeof BadgeProps,
|
2020-07-07 06:19:04 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-10-13 07:02:35 +00:00
|
|
|
badgeCount: undefined
|
2020-07-07 06:19:04 +00:00
|
|
|
};
|
2019-03-12 04:04:40 +00:00
|
|
|
},
|
2019-01-03 12:46:53 +00:00
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
getNumberedDispayCount() {
|
|
|
|
const { overflowCount } = this.$props;
|
|
|
|
const count = this.badgeCount;
|
|
|
|
const displayCount = count > overflowCount ? `${overflowCount}+` : count;
|
|
|
|
return displayCount;
|
2019-01-03 12:46:53 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getDispayCount() {
|
|
|
|
const isDot = this.isDot();
|
2019-01-03 12:46:53 +00:00
|
|
|
// dot mode don't need count
|
|
|
|
if (isDot) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return '';
|
2019-01-03 12:46:53 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return this.getNumberedDispayCount();
|
2019-01-03 12:46:53 +00:00
|
|
|
},
|
|
|
|
|
2019-03-12 04:04:40 +00:00
|
|
|
getScrollNumberTitle() {
|
2019-01-12 03:33:27 +00:00
|
|
|
const { title } = this.$props;
|
|
|
|
const count = this.badgeCount;
|
2019-01-03 12:46:53 +00:00
|
|
|
if (title) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return title;
|
2019-01-03 12:46:53 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return typeof count === 'string' || typeof count === 'number' ? count : undefined;
|
2019-01-03 12:46:53 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getStyleWithOffset() {
|
|
|
|
const { offset, numberStyle } = this.$props;
|
2019-01-03 12:46:53 +00:00
|
|
|
return offset
|
|
|
|
? {
|
2020-10-13 07:02:35 +00:00
|
|
|
right: `${-parseInt(offset[0] as string, 10)}px`,
|
2019-01-12 03:33:27 +00:00
|
|
|
marginTop: isNumeric(offset[1]) ? `${offset[1]}px` : offset[1],
|
|
|
|
...numberStyle,
|
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
: { ...numberStyle };
|
|
|
|
},
|
2020-10-13 07:39:08 +00:00
|
|
|
getBadgeClassName(prefixCls: string, children: any[]) {
|
2020-04-23 03:45:05 +00:00
|
|
|
const hasStatus = this.hasStatus();
|
2020-03-07 11:45:13 +00:00
|
|
|
return classNames(prefixCls, {
|
2020-04-23 03:45:05 +00:00
|
|
|
[`${prefixCls}-status`]: hasStatus,
|
|
|
|
[`${prefixCls}-dot-status`]: hasStatus && this.dot && !this.isZero(),
|
2020-03-07 11:45:13 +00:00
|
|
|
[`${prefixCls}-not-a-wrapper`]: !children.length,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
hasStatus() {
|
|
|
|
const { status, color } = this.$props;
|
|
|
|
return !!status || !!color;
|
|
|
|
},
|
|
|
|
isZero() {
|
|
|
|
const numberedDispayCount = this.getNumberedDispayCount();
|
|
|
|
return numberedDispayCount === '0' || numberedDispayCount === 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
isDot() {
|
|
|
|
const { dot } = this.$props;
|
|
|
|
const isZero = this.isZero();
|
|
|
|
return (dot && !isZero) || this.hasStatus();
|
|
|
|
},
|
|
|
|
|
|
|
|
isHidden() {
|
|
|
|
const { showZero } = this.$props;
|
|
|
|
const displayCount = this.getDispayCount();
|
|
|
|
const isZero = this.isZero();
|
|
|
|
const isDot = this.isDot();
|
|
|
|
const isEmpty = displayCount === null || displayCount === undefined || displayCount === '';
|
|
|
|
return (isEmpty || (isZero && !showZero)) && !isDot;
|
2019-01-03 12:46:53 +00:00
|
|
|
},
|
|
|
|
|
2020-10-13 07:39:08 +00:00
|
|
|
renderStatusText(prefixCls: string) {
|
2020-08-25 05:51:01 +00:00
|
|
|
const text = getComponent(this, 'text');
|
2019-01-12 03:33:27 +00:00
|
|
|
const hidden = this.isHidden();
|
|
|
|
return hidden || !text ? null : <span class={`${prefixCls}-status-text`}>{text}</span>;
|
2019-01-03 12:46:53 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
renderDispayComponent() {
|
|
|
|
const count = this.badgeCount;
|
|
|
|
const customNode = count;
|
2019-01-03 12:46:53 +00:00
|
|
|
if (!customNode || typeof customNode !== 'object') {
|
2019-01-12 03:33:27 +00:00
|
|
|
return undefined;
|
2019-01-03 12:46:53 +00:00
|
|
|
}
|
2020-07-07 07:43:02 +00:00
|
|
|
return cloneElement(
|
|
|
|
customNode,
|
|
|
|
{
|
|
|
|
style: this.getStyleWithOffset(),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
2019-01-03 12:46:53 +00:00
|
|
|
},
|
|
|
|
|
2020-10-13 07:39:08 +00:00
|
|
|
renderBadgeNumber(prefixCls: string, scrollNumberPrefixCls: string) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { status, color } = this.$props;
|
2019-01-12 03:33:27 +00:00
|
|
|
const count = this.badgeCount;
|
|
|
|
const displayCount = this.getDispayCount();
|
|
|
|
const isDot = this.isDot();
|
|
|
|
const hidden = this.isHidden();
|
2019-01-03 12:46:53 +00:00
|
|
|
|
|
|
|
const scrollNumberCls = {
|
|
|
|
[`${prefixCls}-dot`]: isDot,
|
|
|
|
[`${prefixCls}-count`]: !isDot,
|
|
|
|
[`${prefixCls}-multiple-words`]:
|
|
|
|
!isDot && count && count.toString && count.toString().length > 1,
|
|
|
|
[`${prefixCls}-status-${status}`]: !!status,
|
2020-03-07 11:45:13 +00:00
|
|
|
[`${prefixCls}-status-${color}`]: isPresetColor(color),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2019-01-03 12:46:53 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
let statusStyle = this.getStyleWithOffset();
|
|
|
|
if (color && !isPresetColor(color)) {
|
|
|
|
statusStyle = statusStyle || {};
|
|
|
|
statusStyle.background = color;
|
|
|
|
}
|
|
|
|
|
2019-01-03 12:46:53 +00:00
|
|
|
return hidden ? null : (
|
|
|
|
<ScrollNumber
|
|
|
|
prefixCls={scrollNumberPrefixCls}
|
|
|
|
data-show={!hidden}
|
2020-10-13 07:02:35 +00:00
|
|
|
v-show={!hidden}
|
2020-07-07 07:43:02 +00:00
|
|
|
class={scrollNumberCls}
|
2019-01-03 12:46:53 +00:00
|
|
|
count={displayCount}
|
2020-04-04 14:03:18 +00:00
|
|
|
displayComponent={this.renderDispayComponent()}
|
2019-03-12 04:04:40 +00:00
|
|
|
title={this.getScrollNumberTitle()}
|
2020-03-07 11:45:13 +00:00
|
|
|
style={statusStyle}
|
2019-01-12 03:33:27 +00:00
|
|
|
key="scrollNumber"
|
2019-01-03 12:46:53 +00:00
|
|
|
/>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2019-01-03 12:46:53 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-09 09:50:33 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2019-03-12 04:04:40 +00:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
scrollNumberPrefixCls: customizeScrollNumberPrefixCls,
|
|
|
|
status,
|
2020-03-07 11:45:13 +00:00
|
|
|
color,
|
2019-03-12 04:04:40 +00:00
|
|
|
} = this;
|
|
|
|
|
2020-08-25 05:51:01 +00:00
|
|
|
const text = getComponent(this, 'text');
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-12 04:04:40 +00:00
|
|
|
const prefixCls = getPrefixCls('badge', customizePrefixCls);
|
|
|
|
const scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls);
|
|
|
|
|
2020-07-07 07:43:02 +00:00
|
|
|
const children = getSlot(this);
|
|
|
|
let count = getComponent(this, 'count');
|
2019-01-03 12:46:53 +00:00
|
|
|
if (Array.isArray(count)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
count = count[0];
|
2019-01-03 12:46:53 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.badgeCount = count;
|
2019-03-12 04:04:40 +00:00
|
|
|
const scrollNumber = this.renderBadgeNumber(prefixCls, scrollNumberPrefixCls);
|
|
|
|
const statusText = this.renderStatusText(prefixCls);
|
2018-03-09 09:50:33 +00:00
|
|
|
const statusCls = classNames({
|
2020-03-07 11:45:13 +00:00
|
|
|
[`${prefixCls}-status-dot`]: this.hasStatus(),
|
2018-03-09 09:50:33 +00:00
|
|
|
[`${prefixCls}-status-${status}`]: !!status,
|
2020-03-07 11:45:13 +00:00
|
|
|
[`${prefixCls}-status-${color}`]: isPresetColor(color),
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-10-13 07:02:35 +00:00
|
|
|
const statusStyle: CSSProperties = {};
|
2020-03-07 11:45:13 +00:00
|
|
|
if (color && !isPresetColor(color)) {
|
|
|
|
statusStyle.background = color;
|
|
|
|
}
|
2019-01-03 12:46:53 +00:00
|
|
|
// <Badge status="success" />
|
2020-03-07 11:45:13 +00:00
|
|
|
if (!children.length && this.hasStatus()) {
|
|
|
|
const styleWithOffset = this.getStyleWithOffset();
|
|
|
|
const statusTextColor = styleWithOffset && styleWithOffset.color;
|
2018-03-09 09:50:33 +00:00
|
|
|
return (
|
2020-07-07 07:43:02 +00:00
|
|
|
<span class={this.getBadgeClassName(prefixCls, children)} style={styleWithOffset}>
|
2020-03-07 11:45:13 +00:00
|
|
|
<span class={statusCls} style={statusStyle} />
|
|
|
|
<span style={{ color: statusTextColor }} class={`${prefixCls}-status-text`}>
|
|
|
|
{text}
|
|
|
|
</span>
|
2018-03-09 09:50:33 +00:00
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-09 09:50:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const transitionProps = getTransitionProps(children.length ? `${prefixCls}-zoom` : '');
|
2018-03-09 09:50:33 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
return (
|
2020-07-07 07:43:02 +00:00
|
|
|
<span class={this.getBadgeClassName(prefixCls, children)}>
|
2019-01-12 03:33:27 +00:00
|
|
|
{children}
|
2020-07-07 07:43:02 +00:00
|
|
|
<Transition {...transitionProps}>{scrollNumber}</Transition>
|
2019-01-12 03:33:27 +00:00
|
|
|
{statusText}
|
|
|
|
</span>
|
|
|
|
);
|
2017-11-16 10:29:02 +00:00
|
|
|
},
|
2020-10-13 07:02:35 +00:00
|
|
|
});
|