Badge.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default
  21. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. var import_vue2 = require("vue");
  25. var import_utils = require("../utils");
  26. const [name, bem] = (0, import_utils.createNamespace)("badge");
  27. const badgeProps = {
  28. dot: Boolean,
  29. max: import_utils.numericProp,
  30. tag: (0, import_utils.makeStringProp)("div"),
  31. color: String,
  32. offset: Array,
  33. content: import_utils.numericProp,
  34. showZero: import_utils.truthProp,
  35. position: (0, import_utils.makeStringProp)("top-right")
  36. };
  37. var stdin_default = (0, import_vue2.defineComponent)({
  38. name,
  39. props: badgeProps,
  40. setup(props, {
  41. slots
  42. }) {
  43. const hasContent = () => {
  44. if (slots.content) {
  45. return true;
  46. }
  47. const {
  48. content,
  49. showZero
  50. } = props;
  51. return (0, import_utils.isDef)(content) && content !== "" && (showZero || content !== 0 && content !== "0");
  52. };
  53. const renderContent = () => {
  54. const {
  55. dot,
  56. max,
  57. content
  58. } = props;
  59. if (!dot && hasContent()) {
  60. if (slots.content) {
  61. return slots.content();
  62. }
  63. if ((0, import_utils.isDef)(max) && (0, import_utils.isNumeric)(content) && +content > max) {
  64. return `${max}+`;
  65. }
  66. return content;
  67. }
  68. };
  69. const style = (0, import_vue2.computed)(() => {
  70. const style2 = {
  71. background: props.color
  72. };
  73. if (props.offset) {
  74. const [x, y] = props.offset;
  75. if (slots.default) {
  76. style2.top = (0, import_utils.addUnit)(y);
  77. if (typeof x === "number") {
  78. style2.right = (0, import_utils.addUnit)(-x);
  79. } else {
  80. style2.right = x.startsWith("-") ? x.replace("-", "") : `-${x}`;
  81. }
  82. } else {
  83. style2.marginTop = (0, import_utils.addUnit)(y);
  84. style2.marginLeft = (0, import_utils.addUnit)(x);
  85. }
  86. }
  87. return style2;
  88. });
  89. const renderBadge = () => {
  90. if (hasContent() || props.dot) {
  91. return (0, import_vue.createVNode)("div", {
  92. "class": bem([props.position, {
  93. dot: props.dot,
  94. fixed: !!slots.default
  95. }]),
  96. "style": style.value
  97. }, [renderContent()]);
  98. }
  99. };
  100. return () => {
  101. if (slots.default) {
  102. const {
  103. tag
  104. } = props;
  105. return (0, import_vue.createVNode)(tag, {
  106. "class": bem("wrapper")
  107. }, {
  108. default: () => [slots.default(), renderBadge()]
  109. });
  110. }
  111. return renderBadge();
  112. };
  113. }
  114. });