uni-badge.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /* #endif */
  75. justify-content: center;
  76. flex-direction: row;
  77. height: $bage-height;
  78. line-height: $bage-height;
  79. color: $uni-text-color;
  80. border-radius: 100px;
  81. background-color: $uni-bg-color-hover;
  82. background-color: transparent;
  83. text-align: center;
  84. font-family: 'Helvetica Neue', Helvetica, sans-serif;
  85. font-size: $bage-size;
  86. padding: 0px 6px;
  87. }
  88. .uni-badge--inverted {
  89. padding: 0 5px 0 0;
  90. color: $uni-bg-color-hover;
  91. }
  92. .uni-badge--default {
  93. color: $uni-text-color;
  94. background-color: $uni-bg-color-hover;
  95. }
  96. .uni-badge--default-inverted {
  97. color: $uni-text-color-grey;
  98. background-color: transparent;
  99. }
  100. .uni-badge--primary {
  101. color: $uni-text-color-inverse;
  102. background-color: $uni-color-primary;
  103. }
  104. .uni-badge--primary-inverted {
  105. color: $uni-color-primary;
  106. background-color: transparent;
  107. }
  108. .uni-badge--success {
  109. color: $uni-text-color-inverse;
  110. background-color: $uni-color-success;
  111. }
  112. .uni-badge--success-inverted {
  113. color: $uni-color-success;
  114. background-color: transparent;
  115. }
  116. .uni-badge--warning {
  117. color: $uni-text-color-inverse;
  118. background-color: $uni-color-warning;
  119. }
  120. .uni-badge--warning-inverted {
  121. color: $uni-color-warning;
  122. background-color: transparent;
  123. }
  124. .uni-badge--error {
  125. color: $uni-text-color-inverse;
  126. background-color: $uni-color-error;
  127. }
  128. .uni-badge--error-inverted {
  129. color: $uni-color-error;
  130. background-color: transparent;
  131. }
  132. .uni-badge--small {
  133. transform: $bage-small;
  134. transform-origin: center center;
  135. }
  136. </style>