rf-badge.vue 2.0 KB

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