You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.3 KiB

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