u-badge.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <text
  3. v-if="show && ((Number(value) === 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 mpMixin from '../../libs/mixin/mpMixin.js';
  12. import mixin from '../../libs/mixin/mixin.js';
  13. /**
  14. * badge 徽标数
  15. * @description 该组件一般用于图标右上角显示未读的消息数量,提示用户点击,有圆点和圆包含文字两种形式。
  16. * @tutorial https://uiadmin.net/uview-plus/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, props, mixin],
  37. computed: {
  38. // 是否将badge中心与父组件右上角重合
  39. boxStyle() {
  40. let style = {};
  41. return style;
  42. },
  43. // 整个组件的样式
  44. badgeStyle() {
  45. const style = {}
  46. if(this.color) {
  47. style.color = this.color
  48. }
  49. if (this.bgColor && !this.inverted) {
  50. style.backgroundColor = this.bgColor
  51. }
  52. if (this.absolute) {
  53. style.position = 'absolute'
  54. // 如果有设置offset参数
  55. if(this.offset.length) {
  56. // top和right分为为offset的第一个和第二个值,如果没有第二个值,则right等于top
  57. const top = this.offset[0]
  58. const right = this.offset[1] || top
  59. style.top = uni.$u.addUnit(top)
  60. style.right = uni.$u.addUnit(right)
  61. }
  62. }
  63. return style
  64. },
  65. showValue() {
  66. switch (this.numberType) {
  67. case "overflow":
  68. return Number(this.value) > Number(this.max) ? this.max + "+" : this.value
  69. break;
  70. case "ellipsis":
  71. return Number(this.value) > Number(this.max) ? "..." : this.value
  72. break;
  73. case "limit":
  74. return Number(this.value) > 999 ? Number(this.value) >= 9999 ?
  75. Math.floor(this.value / 1e4 * 100) / 100 + "w" : Math.floor(this.value /
  76. 1e3 * 100) / 100 + "k" : this.value
  77. break;
  78. default:
  79. return Number(this.value)
  80. }
  81. },
  82. }
  83. }
  84. </script>
  85. <style lang="scss" scoped>
  86. @import "../../libs/css/components.scss";
  87. $u-badge-primary: $u-primary !default;
  88. $u-badge-error: $u-error !default;
  89. $u-badge-success: $u-success !default;
  90. $u-badge-info: $u-info !default;
  91. $u-badge-warning: $u-warning !default;
  92. $u-badge-dot-radius: 100px !default;
  93. $u-badge-dot-size: 8px !default;
  94. $u-badge-dot-right: 4px !default;
  95. $u-badge-dot-top: 0 !default;
  96. $u-badge-text-font-size: 11px !default;
  97. $u-badge-text-right: 10px !default;
  98. $u-badge-text-padding: 2px 5px !default;
  99. $u-badge-text-align: center !default;
  100. $u-badge-text-color: #FFFFFF !default;
  101. .u-badge {
  102. border-top-right-radius: $u-badge-dot-radius;
  103. border-top-left-radius: $u-badge-dot-radius;
  104. border-bottom-left-radius: $u-badge-dot-radius;
  105. border-bottom-right-radius: $u-badge-dot-radius;
  106. @include flex;
  107. line-height: $u-badge-text-font-size;
  108. text-align: $u-badge-text-align;
  109. font-size: $u-badge-text-font-size;
  110. color: $u-badge-text-color;
  111. &--dot {
  112. height: $u-badge-dot-size;
  113. width: $u-badge-dot-size;
  114. }
  115. &--inverted {
  116. font-size: 13px;
  117. }
  118. &--not-dot {
  119. padding: $u-badge-text-padding;
  120. }
  121. &--horn {
  122. border-bottom-left-radius: 0;
  123. }
  124. &--primary {
  125. background-color: $u-badge-primary;
  126. }
  127. &--primary--inverted {
  128. color: $u-badge-primary;
  129. }
  130. &--error {
  131. background-color: $u-badge-error;
  132. }
  133. &--error--inverted {
  134. color: $u-badge-error;
  135. }
  136. &--success {
  137. background-color: $u-badge-success;
  138. }
  139. &--success--inverted {
  140. color: $u-badge-success;
  141. }
  142. &--info {
  143. background-color: $u-badge-info;
  144. }
  145. &--info--inverted {
  146. color: $u-badge-info;
  147. }
  148. &--warning {
  149. background-color: $u-badge-warning;
  150. }
  151. &--warning--inverted {
  152. color: $u-badge-warning;
  153. }
  154. }
  155. </style>