tm-badges.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!-- 徽章 -->
  2. <template>
  3. <view class="tm--badges fulled">
  4. <view @click.stop="onclick" v-if="icon" class="tm--badges--cm icons flex-center border-white-a-1" :class="[color]" :style="offses+`;width:${iconWidth}rpx;height:${iconWidth}rpx;`">
  5. <view class="d-inline-block flex-center vertical-align-middle" style="transform: scale(0.7); line-height: 0;">
  6. <tm-icons style="line-height: 0;" :size="iconSize" :color="iconColor" dense :name="icon"></tm-icons>
  7. </view>
  8. </view>
  9. <view
  10. @click="onclick"
  11. v-if="!icon"
  12. :style="offses"
  13. class="tm--badges--cm d-inline-block px-6 "
  14. :class="[color_tmeme, label != '' && !dot ? 'num shadow-red-10' : 'dot', 'flex-center']"
  15. >
  16. <text v-if="!dot" class="text-size-xs">{{ label }}</text>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. /**
  22. * 徽章、角标
  23. * @property {String} label = [] 默认:'',当填入信息时即显示数字角标。
  24. * @property {Number} icon-size = [] 默认:24,当为图标时,可以设置图标大小。
  25. * @property {Number} icon-width = [] 默认:32,当为图标时,可以设置宽高。
  26. * @property {String} color = [] 默认:red,主题背景色
  27. * @property {String} icon-color = [] 默认:white,图标主题背景色
  28. * @property {Boolean|String} dot = [] 默认:true,使用点。优先级高于label数字展示。
  29. * @property {String} icon = [] 默认:'',使用图标作为显示角标。
  30. * @property {Array} offset = [] 默认:[0,0],位置,x,y偏移量。
  31. * @property {Funciton} click 点击角标时触发。
  32. * @example <tm-badges />
  33. */
  34. import tmIcons from '@/tm-vuetify/components/tm-icons/tm-icons.vue';
  35. export default {
  36. components: { tmIcons },
  37. name: 'tm-badges',
  38. props: {
  39. label: {
  40. type: String | Number,
  41. default: 0
  42. },
  43. // 使用点。优先级高于label数字展示。
  44. dot: {
  45. type: Boolean | String,
  46. default: true
  47. },
  48. // 使用图标展示
  49. icon: {
  50. type: String,
  51. default: ''
  52. },
  53. iconSize: {
  54. type: Number|String,
  55. default: 24
  56. },
  57. iconWidth:{
  58. type: Number|String,
  59. default: 32
  60. },
  61. // 主题色名称
  62. color: {
  63. type: String,
  64. default: 'red'
  65. },
  66. // 图标主题色名称
  67. iconColor: {
  68. type: String,
  69. default: 'white'
  70. },
  71. // 位置[0,0]
  72. offset: {
  73. type: Array,
  74. default: () => {
  75. return [0, 0];
  76. }
  77. },
  78. // 跟随主题色的改变而改变。
  79. fllowTheme: {
  80. type: Boolean | String,
  81. default: false
  82. }
  83. },
  84. computed: {
  85. offses: function() {
  86. let p = uni.$tm.objToString({
  87. transform: `translateX(${this.offset[0]}px) translateY(${this.offset[1]}px)`
  88. });
  89. return p;
  90. },
  91. color_tmeme: function() {
  92. if (this.$tm.vx.state().tmVuetify.color !== null && this.$tm.vx.state().tmVuetify.color && this.fllowTheme) {
  93. return this.$tm.vx.state().tmVuetify.color;
  94. }
  95. return this.color;
  96. }
  97. },
  98. data() {
  99. return {};
  100. },
  101. methods: {
  102. onclick(e) {
  103. this.$emit('click', e);
  104. }
  105. }
  106. };
  107. </script>
  108. <style lang="scss" scoped>
  109. .tm--badges {
  110. position: relative;
  111. // pointer-events: none;
  112. display: block;
  113. .tm--badges--cm {
  114. position: absolute;
  115. right: 0;
  116. top: 0;
  117. width: 100%;
  118. &.num {
  119. width: auto;
  120. min-width: 26rpx;
  121. height: 35rpx;
  122. color: #fff;
  123. border-radius: 50rpx;
  124. font-size: 22upx;
  125. line-height: 35upx;
  126. text-align: center;
  127. }
  128. &.icons {
  129. @extend .num;
  130. }
  131. &.dot {
  132. width: 16upx;
  133. height: 16upx;
  134. min-width: 0 !important;
  135. color: #fff;
  136. padding: 0;
  137. border-radius: 50%;
  138. }
  139. }
  140. }
  141. </style>