Badge.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _babelHelperVueJsxMergeProps = require('babel-helper-vue-jsx-merge-props');
  6. var _babelHelperVueJsxMergeProps2 = _interopRequireDefault(_babelHelperVueJsxMergeProps);
  7. var _typeof2 = require('babel-runtime/helpers/typeof');
  8. var _typeof3 = _interopRequireDefault(_typeof2);
  9. var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
  10. var _defineProperty3 = _interopRequireDefault(_defineProperty2);
  11. var _extends2 = require('babel-runtime/helpers/extends');
  12. var _extends3 = _interopRequireDefault(_extends2);
  13. var _vueTypes = require('../_util/vue-types');
  14. var _vueTypes2 = _interopRequireDefault(_vueTypes);
  15. var _ScrollNumber = require('./ScrollNumber');
  16. var _ScrollNumber2 = _interopRequireDefault(_ScrollNumber);
  17. var _colors = require('../_util/colors');
  18. var _classnames = require('classnames');
  19. var _classnames2 = _interopRequireDefault(_classnames);
  20. var _propsUtil = require('../_util/props-util');
  21. var _vnode = require('../_util/vnode');
  22. var _getTransitionProps = require('../_util/getTransitionProps');
  23. var _getTransitionProps2 = _interopRequireDefault(_getTransitionProps);
  24. var _isNumeric = require('../_util/isNumeric');
  25. var _isNumeric2 = _interopRequireDefault(_isNumeric);
  26. var _configConsumerProps = require('../config-provider/configConsumerProps');
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  28. var BadgeProps = {
  29. /** Number to show in badge */
  30. count: _vueTypes2['default'].any,
  31. showZero: _vueTypes2['default'].bool,
  32. /** Max count to show */
  33. overflowCount: _vueTypes2['default'].number,
  34. /** whether to show red dot without number */
  35. dot: _vueTypes2['default'].bool,
  36. prefixCls: _vueTypes2['default'].string,
  37. scrollNumberPrefixCls: _vueTypes2['default'].string,
  38. status: _vueTypes2['default'].oneOf(['success', 'processing', 'default', 'error', 'warning']),
  39. color: _vueTypes2['default'].string,
  40. text: _vueTypes2['default'].string,
  41. offset: _vueTypes2['default'].array,
  42. numberStyle: _vueTypes2['default'].object.def(function () {
  43. return {};
  44. }),
  45. title: _vueTypes2['default'].string
  46. };
  47. function isPresetColor(color) {
  48. return _colors.PresetColorTypes.indexOf(color) !== -1;
  49. }
  50. exports['default'] = {
  51. name: 'ABadge',
  52. props: (0, _propsUtil.initDefaultProps)(BadgeProps, {
  53. showZero: false,
  54. dot: false,
  55. overflowCount: 99
  56. }),
  57. inject: {
  58. configProvider: { 'default': function _default() {
  59. return _configConsumerProps.ConfigConsumerProps;
  60. } }
  61. },
  62. methods: {
  63. getNumberedDispayCount: function getNumberedDispayCount() {
  64. var overflowCount = this.$props.overflowCount;
  65. var count = this.badgeCount;
  66. var displayCount = count > overflowCount ? overflowCount + '+' : count;
  67. return displayCount;
  68. },
  69. getDispayCount: function getDispayCount() {
  70. var isDot = this.isDot();
  71. // dot mode don't need count
  72. if (isDot) {
  73. return '';
  74. }
  75. return this.getNumberedDispayCount();
  76. },
  77. getScrollNumberTitle: function getScrollNumberTitle() {
  78. var title = this.$props.title;
  79. var count = this.badgeCount;
  80. if (title) {
  81. return title;
  82. }
  83. return typeof count === 'string' || typeof count === 'number' ? count : undefined;
  84. },
  85. getStyleWithOffset: function getStyleWithOffset() {
  86. var _$props = this.$props,
  87. offset = _$props.offset,
  88. numberStyle = _$props.numberStyle;
  89. return offset ? (0, _extends3['default'])({
  90. right: -parseInt(offset[0], 10) + 'px',
  91. marginTop: (0, _isNumeric2['default'])(offset[1]) ? offset[1] + 'px' : offset[1]
  92. }, numberStyle) : (0, _extends3['default'])({}, numberStyle);
  93. },
  94. getBadgeClassName: function getBadgeClassName(prefixCls) {
  95. var _classNames;
  96. var children = (0, _propsUtil.filterEmpty)(this.$slots['default']);
  97. var hasStatus = this.hasStatus();
  98. return (0, _classnames2['default'])(prefixCls, (_classNames = {}, (0, _defineProperty3['default'])(_classNames, prefixCls + '-status', hasStatus), (0, _defineProperty3['default'])(_classNames, prefixCls + '-dot-status', hasStatus && this.dot && !this.isZero()), (0, _defineProperty3['default'])(_classNames, prefixCls + '-not-a-wrapper', !children.length), _classNames));
  99. },
  100. hasStatus: function hasStatus() {
  101. var _$props2 = this.$props,
  102. status = _$props2.status,
  103. color = _$props2.color;
  104. return !!status || !!color;
  105. },
  106. isZero: function isZero() {
  107. var numberedDispayCount = this.getNumberedDispayCount();
  108. return numberedDispayCount === '0' || numberedDispayCount === 0;
  109. },
  110. isDot: function isDot() {
  111. var dot = this.$props.dot;
  112. var isZero = this.isZero();
  113. return dot && !isZero || this.hasStatus();
  114. },
  115. isHidden: function isHidden() {
  116. var showZero = this.$props.showZero;
  117. var displayCount = this.getDispayCount();
  118. var isZero = this.isZero();
  119. var isDot = this.isDot();
  120. var isEmpty = displayCount === null || displayCount === undefined || displayCount === '';
  121. return (isEmpty || isZero && !showZero) && !isDot;
  122. },
  123. renderStatusText: function renderStatusText(prefixCls) {
  124. var h = this.$createElement;
  125. var text = this.$props.text;
  126. var hidden = this.isHidden();
  127. return hidden || !text ? null : h(
  128. 'span',
  129. { 'class': prefixCls + '-status-text' },
  130. [text]
  131. );
  132. },
  133. renderDispayComponent: function renderDispayComponent() {
  134. var count = this.badgeCount;
  135. var customNode = count;
  136. if (!customNode || (typeof customNode === 'undefined' ? 'undefined' : (0, _typeof3['default'])(customNode)) !== 'object') {
  137. return undefined;
  138. }
  139. return (0, _vnode.cloneElement)(customNode, {
  140. style: this.getStyleWithOffset()
  141. });
  142. },
  143. renderBadgeNumber: function renderBadgeNumber(prefixCls, scrollNumberPrefixCls) {
  144. var _scrollNumberCls;
  145. var h = this.$createElement;
  146. var _$props3 = this.$props,
  147. status = _$props3.status,
  148. color = _$props3.color;
  149. var count = this.badgeCount;
  150. var displayCount = this.getDispayCount();
  151. var isDot = this.isDot();
  152. var hidden = this.isHidden();
  153. var scrollNumberCls = (_scrollNumberCls = {}, (0, _defineProperty3['default'])(_scrollNumberCls, prefixCls + '-dot', isDot), (0, _defineProperty3['default'])(_scrollNumberCls, prefixCls + '-count', !isDot), (0, _defineProperty3['default'])(_scrollNumberCls, prefixCls + '-multiple-words', !isDot && count && count.toString && count.toString().length > 1), (0, _defineProperty3['default'])(_scrollNumberCls, prefixCls + '-status-' + status, !!status), (0, _defineProperty3['default'])(_scrollNumberCls, prefixCls + '-status-' + color, isPresetColor(color)), _scrollNumberCls);
  154. var statusStyle = this.getStyleWithOffset();
  155. if (color && !isPresetColor(color)) {
  156. statusStyle = statusStyle || {};
  157. statusStyle.background = color;
  158. }
  159. return hidden ? null : h(_ScrollNumber2['default'], {
  160. attrs: {
  161. prefixCls: scrollNumberPrefixCls,
  162. 'data-show': !hidden,
  163. className: scrollNumberCls,
  164. count: displayCount,
  165. displayComponent: this.renderDispayComponent() // <Badge status="success" count={<Icon type="xxx" />}></Badge>
  166. , title: this.getScrollNumberTitle()
  167. },
  168. directives: [{
  169. name: 'show',
  170. value: !hidden
  171. }],
  172. style: statusStyle,
  173. key: 'scrollNumber'
  174. });
  175. }
  176. },
  177. render: function render() {
  178. var _classNames2;
  179. var h = arguments[0];
  180. var customizePrefixCls = this.prefixCls,
  181. customizeScrollNumberPrefixCls = this.scrollNumberPrefixCls,
  182. status = this.status,
  183. text = this.text,
  184. color = this.color,
  185. $slots = this.$slots;
  186. var getPrefixCls = this.configProvider.getPrefixCls;
  187. var prefixCls = getPrefixCls('badge', customizePrefixCls);
  188. var scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls);
  189. var children = (0, _propsUtil.filterEmpty)($slots['default']);
  190. var count = (0, _propsUtil.getComponentFromProp)(this, 'count');
  191. if (Array.isArray(count)) {
  192. count = count[0];
  193. }
  194. this.badgeCount = count;
  195. var scrollNumber = this.renderBadgeNumber(prefixCls, scrollNumberPrefixCls);
  196. var statusText = this.renderStatusText(prefixCls);
  197. var statusCls = (0, _classnames2['default'])((_classNames2 = {}, (0, _defineProperty3['default'])(_classNames2, prefixCls + '-status-dot', this.hasStatus()), (0, _defineProperty3['default'])(_classNames2, prefixCls + '-status-' + status, !!status), (0, _defineProperty3['default'])(_classNames2, prefixCls + '-status-' + color, isPresetColor(color)), _classNames2));
  198. var statusStyle = {};
  199. if (color && !isPresetColor(color)) {
  200. statusStyle.background = color;
  201. }
  202. // <Badge status="success" />
  203. if (!children.length && this.hasStatus()) {
  204. var styleWithOffset = this.getStyleWithOffset();
  205. var statusTextColor = styleWithOffset && styleWithOffset.color;
  206. return h(
  207. 'span',
  208. (0, _babelHelperVueJsxMergeProps2['default'])([{ on: (0, _propsUtil.getListeners)(this) }, {
  209. 'class': this.getBadgeClassName(prefixCls),
  210. style: styleWithOffset
  211. }]),
  212. [h('span', { 'class': statusCls, style: statusStyle }), h(
  213. 'span',
  214. { style: { color: statusTextColor }, 'class': prefixCls + '-status-text' },
  215. [text]
  216. )]
  217. );
  218. }
  219. var transitionProps = (0, _getTransitionProps2['default'])(children.length ? prefixCls + '-zoom' : '');
  220. return h(
  221. 'span',
  222. (0, _babelHelperVueJsxMergeProps2['default'])([{ on: (0, _propsUtil.getListeners)(this) }, { 'class': this.getBadgeClassName(prefixCls) }]),
  223. [children, h(
  224. 'transition',
  225. transitionProps,
  226. [scrollNumber]
  227. ), statusText]
  228. );
  229. }
  230. };