index.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <div class="m-badge">
  3. <slot></slot>
  4. <sup
  5. class="m-badge__content"
  6. :class="styleClass"
  7. v-show="!hidden && (content || content === 0 || isDot)">
  8. {{content}}
  9. </sup>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name: "mBadge"
  15. };
  16. </script>
  17. <script setup>
  18. import { computed,useSlots } from 'vue-demi';
  19. const props = defineProps({
  20. value: String | Number,
  21. max: Number,
  22. type: {
  23. type: String,
  24. validator(val) {
  25. return ['primary', 'success', 'warning', 'info', 'danger'].indexOf(val) > -1;
  26. }
  27. },
  28. isDot: {
  29. type: Boolean,
  30. default: false
  31. },
  32. hidden: {
  33. type: Boolean,
  34. default: false
  35. }
  36. })
  37. const $slots = useSlots()
  38. const styleClass = computed(() => {
  39. return {
  40. 'is-fixed': $slots.default,
  41. [`m-badge--${props.type}`]: props.type,
  42. 'is-dot': props.isDot
  43. }
  44. })
  45. const content = computed(() => {
  46. if (props.isDot) return;
  47. const { value,max } = props
  48. if (typeof value === 'number' && typeof max === 'number') {
  49. return max < value ? `${max}+` : value;
  50. }
  51. return value;
  52. })
  53. </script>
  54. <style lang="scss" scoped>
  55. @import '../../styles/components/badge.scss';
  56. </style>