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