uni-badge.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "UniBadge",
  5. emits: ["click"],
  6. props: {
  7. type: {
  8. type: String,
  9. default: "error"
  10. },
  11. inverted: {
  12. type: Boolean,
  13. default: false
  14. },
  15. isDot: {
  16. type: Boolean,
  17. default: false
  18. },
  19. maxNum: {
  20. type: Number,
  21. default: 99
  22. },
  23. absolute: {
  24. type: String,
  25. default: ""
  26. },
  27. offset: {
  28. type: Array,
  29. default() {
  30. return [0, 0];
  31. }
  32. },
  33. text: {
  34. type: [String, Number],
  35. default: ""
  36. },
  37. size: {
  38. type: String,
  39. default: "small"
  40. },
  41. customStyle: {
  42. type: Object,
  43. default() {
  44. return {};
  45. }
  46. }
  47. },
  48. data() {
  49. return {};
  50. },
  51. computed: {
  52. width() {
  53. return String(this.text).length * 8 + 12;
  54. },
  55. classNames() {
  56. const {
  57. inverted,
  58. type,
  59. size,
  60. absolute
  61. } = this;
  62. return [
  63. inverted ? "uni-badge--" + type + "-inverted" : "",
  64. "uni-badge--" + type,
  65. "uni-badge--" + size,
  66. absolute ? "uni-badge--absolute" : ""
  67. ].join(" ");
  68. },
  69. positionStyle() {
  70. if (!this.absolute)
  71. return {};
  72. let w = this.width / 2, h = 10;
  73. if (this.isDot) {
  74. w = 5;
  75. h = 5;
  76. }
  77. const x = `${-w + this.offset[0]}px`;
  78. const y = `${-h + this.offset[1]}px`;
  79. const whiteList = {
  80. rightTop: {
  81. right: x,
  82. top: y
  83. },
  84. rightBottom: {
  85. right: x,
  86. bottom: y
  87. },
  88. leftBottom: {
  89. left: x,
  90. bottom: y
  91. },
  92. leftTop: {
  93. left: x,
  94. top: y
  95. }
  96. };
  97. const match = whiteList[this.absolute];
  98. return match ? match : whiteList["rightTop"];
  99. },
  100. badgeWidth() {
  101. return {
  102. width: `${this.width}px`
  103. };
  104. },
  105. dotStyle() {
  106. if (!this.isDot)
  107. return {};
  108. return {
  109. width: "10px",
  110. height: "10px",
  111. borderRadius: "10px"
  112. };
  113. },
  114. displayValue() {
  115. const {
  116. isDot,
  117. text,
  118. maxNum
  119. } = this;
  120. return isDot ? "" : Number(text) > maxNum ? `${maxNum}+` : text;
  121. }
  122. },
  123. methods: {
  124. onClick() {
  125. this.$emit("click");
  126. }
  127. }
  128. };
  129. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  130. return common_vendor.e({
  131. a: $props.text
  132. }, $props.text ? {
  133. b: common_vendor.t($options.displayValue),
  134. c: common_vendor.n($options.classNames),
  135. d: common_vendor.s($options.badgeWidth),
  136. e: common_vendor.s($options.positionStyle),
  137. f: common_vendor.s($props.customStyle),
  138. g: common_vendor.s($options.dotStyle),
  139. h: common_vendor.o(($event) => $options.onClick())
  140. } : {});
  141. }
  142. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-c97cb896"], ["__file", "D:/zx/mall-front-app/uni_modules/uni-badge/components/uni-badge/uni-badge.vue"]]);
  143. wx.createComponent(Component);