uni-badge.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. }
  86. .uni-badge--inverted {
  87. padding: 0 5px 0 0;
  88. color: #f1f1f1;
  89. }
  90. .uni-badge--default {
  91. color: #333;
  92. background-color: #f1f1f1;
  93. }
  94. .uni-badge--default-inverted {
  95. color: #999;
  96. background-color: transparent;
  97. }
  98. .uni-badge--primary {
  99. color: #fff;
  100. background-color: #007aff;
  101. }
  102. .uni-badge--primary-inverted {
  103. color: #007aff;
  104. background-color: transparent;
  105. }
  106. .uni-badge--success {
  107. color: #fff;
  108. background-color: #4cd964;
  109. }
  110. .uni-badge--success-inverted {
  111. color: #4cd964;
  112. background-color: transparent;
  113. }
  114. .uni-badge--warning {
  115. color: #fff;
  116. background-color: #f0ad4e;
  117. }
  118. .uni-badge--warning-inverted {
  119. color: #f0ad4e;
  120. background-color: transparent;
  121. }
  122. .uni-badge--error {
  123. color: #fff;
  124. background-color: #dd524d;
  125. }
  126. .uni-badge--error-inverted {
  127. color: #dd524d;
  128. background-color: transparent;
  129. }
  130. .uni-badge--small {
  131. transform: scale(0.8);
  132. transform-origin: center center;
  133. }
  134. </style>