cl-badge.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <view class="cl-badge" v-show="$slots.default || $slots.$default">
  3. <slot></slot>
  4. <text v-if="!hidden && (content != 0 || isDot)" :class="[classList]">{{ content }}</text>
  5. </view>
  6. </template>
  7. <script>
  8. import { isNumber } from "../../utils";
  9. /**
  10. * badge 角标
  11. * @description 位于右上角的角标
  12. * @tutorial https://docs.cool-js.com/uni/components/view/badge.html
  13. * @property {Boolean} value 文本内容
  14. * @property {Number} max 最大值
  15. * @property {Boolean} isDot 是否点状
  16. * @property {Boolean} hidden 是否隐藏
  17. * @property {String} type 类型 primary | success | warning | error
  18. * @example <cl-badge :value="10">Hello !</cl-badge>
  19. */
  20. export default {
  21. name: "cl-badge",
  22. props: {
  23. // 文本内容
  24. value: [String, Number],
  25. // 最大值
  26. max: Number,
  27. // 是否点状
  28. isDot: Boolean,
  29. // 是否隐藏
  30. hidden: Boolean,
  31. // 类型 primary | success | warning | error
  32. type: String,
  33. },
  34. computed: {
  35. content() {
  36. if (this.isDot) return "";
  37. const value = this.value;
  38. const max = this.max;
  39. if (isNumber(value) && isNumber(max) && max > 0) {
  40. return max < value ? `${max}+` : value;
  41. }
  42. return value;
  43. },
  44. classList() {
  45. let list = ["cl-badge__content"];
  46. if (this.type) {
  47. list.push(`cl-badge__content--${this.type}`);
  48. }
  49. if (this.isDot) {
  50. list.push("is-dot");
  51. }
  52. return list.join(" ");
  53. },
  54. },
  55. };
  56. </script>