uni-badge.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <text v-if="text" :class="inverted ? 'uni-badge--' + type + ' uni-badge--' + size + ' uni-badge--' + type + '-inverted' : 'uni-badge--' + type + ' uni-badge--' + size"
  3. :style="badgeStyle" class="uni-badge" @click="onClick()">{{ text }}</text>
  4. </template>
  5. <script>
  6. /**
  7. * Badge 数字角标
  8. * @description 数字角标一般和其它控件(列表、9宫格等)配合使用,用于进行数量提示,默认为实心灰色背景
  9. * @tutorial https://ext.dcloud.net.cn/plugin?id=21
  10. * @property {String} text 角标内容
  11. * @property {String} type = [default|primary|success|warning|error] 颜色类型
  12. * @value default 灰色
  13. * @value primary 蓝色
  14. * @value success 绿色
  15. * @value warning 黄色
  16. * @value error 红色
  17. * @property {String} size = [normal|small] Badge 大小
  18. * @value normal 一般尺寸
  19. * @value small 小尺寸
  20. * @property {String} inverted = [true|false] 是否无需背景颜色
  21. * @event {Function} click 点击 Badge 触发事件
  22. * @example <uni-badge text="1"></uni-badge>
  23. */
  24. export default {
  25. name: 'UniBadge',
  26. props: {
  27. type: {
  28. type: String,
  29. default: 'default'
  30. },
  31. inverted: {
  32. type: Boolean,
  33. default: false
  34. },
  35. text: {
  36. type: [String, Number],
  37. default: ''
  38. },
  39. size: {
  40. type: String,
  41. default: 'normal'
  42. }
  43. },
  44. data() {
  45. return {
  46. badgeStyle: ''
  47. };
  48. },
  49. watch: {
  50. text() {
  51. this.setStyle()
  52. }
  53. },
  54. mounted() {
  55. this.setStyle()
  56. },
  57. methods: {
  58. setStyle() {
  59. this.badgeStyle = `width: ${String(this.text).length * 8 + 12}px`
  60. },
  61. onClick() {
  62. this.$emit('click');
  63. }
  64. }
  65. };
  66. </script>
  67. <style lang="scss" scoped>
  68. $bage-size: 12px;
  69. $bage-small: scale(0.8);
  70. $bage-height: 20px;
  71. .uni-badge {
  72. /* #ifndef APP-PLUS */
  73. display: flex;
  74. box-sizing: border-box;
  75. overflow: hidden;
  76. /* #endif */
  77. justify-content: center;
  78. flex-direction: row;
  79. height: $bage-height;
  80. line-height: $bage-height;
  81. color: $uni-text-color;
  82. border-radius: 100px;
  83. background-color: $uni-bg-color-hover;
  84. background-color: transparent;
  85. text-align: center;
  86. font-family: 'Helvetica Neue', Helvetica, sans-serif;
  87. font-size: $bage-size;
  88. padding: 0px 6px;
  89. }
  90. .uni-badge--inverted {
  91. padding: 0 5px 0 0;
  92. color: $uni-bg-color-hover;
  93. }
  94. .uni-badge--default {
  95. color: $uni-text-color;
  96. background-color: $uni-bg-color-hover;
  97. }
  98. .uni-badge--default-inverted {
  99. color: $uni-text-color-grey;
  100. background-color: transparent;
  101. }
  102. .uni-badge--primary {
  103. color: $uni-text-color-inverse;
  104. background-color: $uni-color-primary;
  105. }
  106. .uni-badge--primary-inverted {
  107. color: $uni-color-primary;
  108. background-color: transparent;
  109. }
  110. .uni-badge--success {
  111. color: $uni-text-color-inverse;
  112. background-color: $uni-color-success;
  113. }
  114. .uni-badge--success-inverted {
  115. color: $uni-color-success;
  116. background-color: transparent;
  117. }
  118. .uni-badge--warning {
  119. color: $uni-text-color-inverse;
  120. background-color: $uni-color-warning;
  121. }
  122. .uni-badge--warning-inverted {
  123. color: $uni-color-warning;
  124. background-color: transparent;
  125. }
  126. .uni-badge--error {
  127. color: $uni-text-color-inverse;
  128. background-color: $uni-color-error;
  129. }
  130. .uni-badge--error-inverted {
  131. color: $uni-color-error;
  132. background-color: transparent;
  133. }
  134. .uni-badge--small {
  135. transform: $bage-small;
  136. transform-origin: center center;
  137. }
  138. </style>