u-badge.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <text
  3. v-if="show && ((currentValue === 0 ? showZero : true) || isDot)"
  4. :class="[isDot ? 'u-badge--dot' : 'u-badge--not-dot', inverted && 'u-badge--inverted', shape === 'horn' && 'u-badge--horn', `u-badge--${type}${inverted ? '--inverted' : ''}`]"
  5. :style="[$u.addStyle(customStyle), badgeStyle]"
  6. class="u-badge"
  7. >{{ isDot ? '' :showValue }}</text>
  8. </template>
  9. <script>
  10. import props from './props.js';
  11. import mixin from '../../libs/mixin/mixin'
  12. import mpMixin from '../../libs/mixin/mpMixin';
  13. /**
  14. * badge 徽标数
  15. * @description 该组件一般用于图标右上角显示未读的消息数量,提示用户点击,有圆点和圆包含文字两种形式。
  16. * @tutorial https://uviewui.com/components/badge.html
  17. *
  18. * @property {Boolean} isDot 是否显示圆点 (默认 false )
  19. * @property {String | Number} value 显示的内容
  20. * @property {Boolean} show 是否显示 (默认 true )
  21. * @property {String | Number} max 最大值,超过最大值会显示 '{max}+' (默认999)
  22. * @property {String} type 主题类型,error|warning|success|primary (默认 'error' )
  23. * @property {Boolean} showZero 当数值为 0 时,是否展示 Badge (默认 false )
  24. * @property {String} bgColor 背景颜色,优先级比type高,如设置,type参数会失效
  25. * @property {String} color 字体颜色 (默认 '#ffffff' )
  26. * @property {String} shape 徽标形状,circle-四角均为圆角,horn-左下角为直角 (默认 'circle' )
  27. * @property {String} numberType 设置数字的显示方式,overflow|ellipsis|limit (默认 'overflow' )
  28. * @property {Array}} offset 设置badge的位置偏移,格式为 [x, y],也即设置的为top和right的值,absolute为true时有效
  29. * @property {Boolean} inverted 是否反转背景和字体颜色(默认 false )
  30. * @property {Boolean} absolute 是否绝对定位(默认 false )
  31. * @property {Object} customStyle 定义需要用到的外部样式
  32. * @example <u-badge :type="type" :count="count"></u-badge>
  33. */
  34. export default {
  35. name: 'u-badge',
  36. mixins: [mpMixin, mixin, props],
  37. computed: {
  38. currentValue() {
  39. // #ifdef VUE2
  40. return Number(this.value);
  41. // #endif
  42. // #ifdef VUE3
  43. return Number(this.modelValue || this.value);
  44. // #endif
  45. },
  46. // 是否将badge中心与父组件右上角重合
  47. boxStyle() {
  48. let style = {};
  49. return style;
  50. },
  51. // 整个组件的样式
  52. badgeStyle() {
  53. const style = {}
  54. if(this.color) {
  55. style.color = this.$uColor('color');
  56. }
  57. if (this.bgColor && !this.inverted) {
  58. style.backgroundColor = this.$uColor('bgColor');
  59. }
  60. if (this.absolute) {
  61. style.position = 'absolute'
  62. // 如果有设置offset参数
  63. if(this.offset.length) {
  64. // top和right分为为offset的第一个和第二个值,如果没有第二个值,则right等于top
  65. const top = this.offset[0]
  66. const right = this.offset[1] || top
  67. style.top = uni.$u.addUnit(top)
  68. style.right = uni.$u.addUnit(right)
  69. }
  70. }
  71. return style
  72. },
  73. showValue() {
  74. switch (this.numberType) {
  75. case "overflow":
  76. return this.currentValue > Number(this.max) ? this.max + "+" : this.modelValue || this.value
  77. break;
  78. case "ellipsis":
  79. return this.currentValue > Number(this.max) ? "..." : this.modelValue || this.value
  80. break;
  81. case "limit":
  82. return this.currentValue > 999 ? this.currentValue >= 9999 ?
  83. Math.floor(this.modelValue || this.value / 1e4 * 100) / 100 + "w" : Math.floor(this.modelValue || this.value /
  84. 1e3 * 100) / 100 + "k" : this.modelValue || this.value
  85. break;
  86. default:
  87. return this.currentValue
  88. }
  89. },
  90. }
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. @import "../../libs/css/components.scss";
  95. $u-badge-primary: $u-primary !default;
  96. $u-badge-error: $u-error !default;
  97. $u-badge-success: $u-success !default;
  98. $u-badge-info: $u-info !default;
  99. $u-badge-warning: $u-warning !default;
  100. $u-badge-dot-radius: 100px !default;
  101. $u-badge-dot-size: 8px !default;
  102. $u-badge-dot-right: 4px !default;
  103. $u-badge-dot-top: 0 !default;
  104. $u-badge-text-font-size: 11px !default;
  105. $u-badge-text-right: 10px !default;
  106. $u-badge-text-padding: 2px 5px !default;
  107. $u-badge-text-align: center !default;
  108. $u-badge-text-color: #FFFFFF !default;
  109. .u-badge {
  110. border-top-right-radius: $u-badge-dot-radius;
  111. border-top-left-radius: $u-badge-dot-radius;
  112. border-bottom-left-radius: $u-badge-dot-radius;
  113. border-bottom-right-radius: $u-badge-dot-radius;
  114. @include flex;
  115. line-height: $u-badge-text-font-size;
  116. text-align: $u-badge-text-align;
  117. font-size: $u-badge-text-font-size;
  118. color: $u-badge-text-color;
  119. &--dot {
  120. height: $u-badge-dot-size;
  121. width: $u-badge-dot-size;
  122. }
  123. &--inverted {
  124. font-size: 13px;
  125. }
  126. &--not-dot {
  127. padding: $u-badge-text-padding;
  128. }
  129. &--horn {
  130. border-bottom-left-radius: 0;
  131. }
  132. &--primary {
  133. background-color: $u-badge-primary;
  134. }
  135. &--primary--inverted {
  136. color: $u-badge-primary;
  137. }
  138. &--error {
  139. background-color: $u-badge-error;
  140. }
  141. &--error--inverted {
  142. color: $u-badge-error;
  143. }
  144. &--success {
  145. background-color: $u-badge-success;
  146. }
  147. &--success--inverted {
  148. color: $u-badge-success;
  149. }
  150. &--info {
  151. background-color: $u-badge-info;
  152. }
  153. &--info--inverted {
  154. color: $u-badge-info;
  155. }
  156. &--warning {
  157. background-color: $u-badge-warning;
  158. }
  159. &--warning--inverted {
  160. color: $u-badge-warning;
  161. }
  162. }
  163. </style>