Badge.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import _typeof from "@babel/runtime/helpers/esm/typeof";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. import { withDirectives as _withDirectives, vShow as _vShow, createVNode as _createVNode } from "vue";
  5. import PropTypes from '../_util/vue-types';
  6. import ScrollNumber from './ScrollNumber';
  7. import classNames from '../_util/classNames';
  8. import { getPropsSlot, flattenChildren } from '../_util/props-util';
  9. import { cloneElement } from '../_util/vnode';
  10. import { getTransitionProps, Transition } from '../_util/transition';
  11. import { defineComponent, computed, ref, watch } from 'vue';
  12. import Ribbon from './Ribbon';
  13. import { isPresetColor } from './utils';
  14. import useConfigInject from '../_util/hooks/useConfigInject';
  15. import isNumeric from '../_util/isNumeric';
  16. export var badgeProps = function badgeProps() {
  17. return {
  18. /** Number to show in badge */
  19. count: PropTypes.any,
  20. showZero: {
  21. type: Boolean,
  22. default: undefined
  23. },
  24. /** Max count to show */
  25. overflowCount: {
  26. type: Number,
  27. default: 99
  28. },
  29. /** whether to show red dot without number */
  30. dot: {
  31. type: Boolean,
  32. default: undefined
  33. },
  34. prefixCls: String,
  35. scrollNumberPrefixCls: String,
  36. status: {
  37. type: String
  38. },
  39. size: {
  40. type: String,
  41. default: 'default'
  42. },
  43. color: String,
  44. text: PropTypes.any,
  45. offset: Array,
  46. numberStyle: {
  47. type: Object,
  48. default: undefined
  49. },
  50. title: String
  51. };
  52. };
  53. export default defineComponent({
  54. compatConfig: {
  55. MODE: 3
  56. },
  57. name: 'ABadge',
  58. Ribbon: Ribbon,
  59. inheritAttrs: false,
  60. props: badgeProps(),
  61. slots: ['text', 'count'],
  62. setup: function setup(props, _ref) {
  63. var slots = _ref.slots,
  64. attrs = _ref.attrs;
  65. var _useConfigInject = useConfigInject('badge', props),
  66. prefixCls = _useConfigInject.prefixCls,
  67. direction = _useConfigInject.direction;
  68. // ================================ Misc ================================
  69. var numberedDisplayCount = computed(function () {
  70. return props.count > props.overflowCount ? "".concat(props.overflowCount, "+") : props.count;
  71. });
  72. var hasStatus = computed(function () {
  73. return props.status !== null && props.status !== undefined || props.color !== null && props.color !== undefined;
  74. });
  75. var isZero = computed(function () {
  76. return numberedDisplayCount.value === '0' || numberedDisplayCount.value === 0;
  77. });
  78. var showAsDot = computed(function () {
  79. return props.dot && !isZero.value;
  80. });
  81. var mergedCount = computed(function () {
  82. return showAsDot.value ? '' : numberedDisplayCount.value;
  83. });
  84. var isHidden = computed(function () {
  85. var isEmpty = mergedCount.value === null || mergedCount.value === undefined || mergedCount.value === '';
  86. return (isEmpty || isZero.value && !props.showZero) && !showAsDot.value;
  87. });
  88. // Count should be cache in case hidden change it
  89. var livingCount = ref(props.count);
  90. // We need cache count since remove motion should not change count display
  91. var displayCount = ref(mergedCount.value);
  92. // We will cache the dot status to avoid shaking on leaved motion
  93. var isDotRef = ref(showAsDot.value);
  94. watch([function () {
  95. return props.count;
  96. }, mergedCount, showAsDot], function () {
  97. if (!isHidden.value) {
  98. livingCount.value = props.count;
  99. displayCount.value = mergedCount.value;
  100. isDotRef.value = showAsDot.value;
  101. }
  102. }, {
  103. immediate: true
  104. });
  105. // Shared styles
  106. var statusCls = computed(function () {
  107. var _ref2;
  108. return _ref2 = {}, _defineProperty(_ref2, "".concat(prefixCls.value, "-status-dot"), hasStatus.value), _defineProperty(_ref2, "".concat(prefixCls.value, "-status-").concat(props.status), !!props.status), _defineProperty(_ref2, "".concat(prefixCls.value, "-status-").concat(props.color), isPresetColor(props.color)), _ref2;
  109. });
  110. var statusStyle = computed(function () {
  111. if (props.color && !isPresetColor(props.color)) {
  112. return {
  113. background: props.color
  114. };
  115. } else {
  116. return {};
  117. }
  118. });
  119. var scrollNumberCls = computed(function () {
  120. var _ref3;
  121. return _ref3 = {}, _defineProperty(_ref3, "".concat(prefixCls.value, "-dot"), isDotRef.value), _defineProperty(_ref3, "".concat(prefixCls.value, "-count"), !isDotRef.value), _defineProperty(_ref3, "".concat(prefixCls.value, "-count-sm"), props.size === 'small'), _defineProperty(_ref3, "".concat(prefixCls.value, "-multiple-words"), !isDotRef.value && displayCount.value && displayCount.value.toString().length > 1), _defineProperty(_ref3, "".concat(prefixCls.value, "-status-").concat(props.status), !!props.status), _defineProperty(_ref3, "".concat(prefixCls.value, "-status-").concat(props.color), isPresetColor(props.color)), _ref3;
  122. });
  123. return function () {
  124. var _slots$default, _slots$count, _classNames;
  125. var offset = props.offset,
  126. title = props.title,
  127. color = props.color;
  128. var style = attrs.style;
  129. var text = getPropsSlot(slots, props, 'text');
  130. var pre = prefixCls.value;
  131. var count = livingCount.value;
  132. var children = flattenChildren((_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots));
  133. children = children.length ? children : null;
  134. var visible = !!(!isHidden.value || slots.count);
  135. // =============================== Styles ===============================
  136. var mergedStyle = function () {
  137. if (!offset) {
  138. return _objectSpread({}, style);
  139. }
  140. var offsetStyle = {
  141. marginTop: isNumeric(offset[1]) ? "".concat(offset[1], "px") : offset[1]
  142. };
  143. if (direction.value === 'rtl') {
  144. offsetStyle.left = "".concat(parseInt(offset[0], 10), "px");
  145. } else {
  146. offsetStyle.right = "".concat(-parseInt(offset[0], 10), "px");
  147. }
  148. return _objectSpread(_objectSpread({}, offsetStyle), style);
  149. }();
  150. // =============================== Render ===============================
  151. // >>> Title
  152. var titleNode = title !== null && title !== void 0 ? title : typeof count === 'string' || typeof count === 'number' ? count : undefined;
  153. // >>> Status Text
  154. var statusTextNode = visible || !text ? null : _createVNode("span", {
  155. "class": "".concat(pre, "-status-text")
  156. }, [text]);
  157. // >>> Display Component
  158. var displayNode = _typeof(count) === 'object' || count === undefined && slots.count ? cloneElement(count !== null && count !== void 0 ? count : (_slots$count = slots.count) === null || _slots$count === void 0 ? void 0 : _slots$count.call(slots), {
  159. style: mergedStyle
  160. }, false) : null;
  161. var badgeClassName = classNames(pre, (_classNames = {}, _defineProperty(_classNames, "".concat(pre, "-status"), hasStatus.value), _defineProperty(_classNames, "".concat(pre, "-not-a-wrapper"), !children), _defineProperty(_classNames, "".concat(pre, "-rtl"), direction.value === 'rtl'), _classNames), attrs.class);
  162. // <Badge status="success" />
  163. if (!children && hasStatus.value) {
  164. var statusTextColor = mergedStyle.color;
  165. return _createVNode("span", _objectSpread(_objectSpread({}, attrs), {}, {
  166. "class": badgeClassName,
  167. "style": mergedStyle
  168. }), [_createVNode("span", {
  169. "class": statusCls.value,
  170. "style": statusStyle.value
  171. }, null), _createVNode("span", {
  172. "style": {
  173. color: statusTextColor
  174. },
  175. "class": "".concat(pre, "-status-text")
  176. }, [text])]);
  177. }
  178. var transitionProps = getTransitionProps(children ? "".concat(pre, "-zoom") : '', {
  179. appear: false
  180. });
  181. var scrollNumberStyle = _objectSpread(_objectSpread({}, mergedStyle), props.numberStyle);
  182. if (color && !isPresetColor(color)) {
  183. scrollNumberStyle = scrollNumberStyle || {};
  184. scrollNumberStyle.background = color;
  185. }
  186. return _createVNode("span", _objectSpread(_objectSpread({}, attrs), {}, {
  187. "class": badgeClassName
  188. }), [children, _createVNode(Transition, transitionProps, {
  189. default: function _default() {
  190. return [_withDirectives(_createVNode(ScrollNumber, {
  191. "prefixCls": props.scrollNumberPrefixCls,
  192. "show": visible,
  193. "class": scrollNumberCls.value,
  194. "count": displayCount.value,
  195. "title": titleNode,
  196. "style": scrollNumberStyle,
  197. "key": "scrollNumber"
  198. }, {
  199. default: function _default() {
  200. return [displayNode];
  201. }
  202. }), [[_vShow, visible]])];
  203. }
  204. }), statusTextNode]);
  205. };
  206. }
  207. });