wd-badge.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <view :class="['wd-badge', customClass]" :style="customStyle">
  3. <slot></slot>
  4. <view
  5. v-if="shouldShowBadge"
  6. :class="['wd-badge__content', 'is-fixed', type ? 'wd-badge__content--' + type : '', isDot ? 'is-dot' : '']"
  7. :style="contentStyle"
  8. >
  9. {{ content }}
  10. </view>
  11. </view>
  12. </template>
  13. <script lang="ts">
  14. export default {
  15. name: 'wd-badge',
  16. options: {
  17. addGlobalClass: true,
  18. virtualHost: true,
  19. styleIsolation: 'shared'
  20. }
  21. }
  22. </script>
  23. <script lang="ts" setup>
  24. import { computed, type CSSProperties } from 'vue'
  25. import { badgeProps } from './types'
  26. import { addUnit, isDef, isNumber, objToStyle } from '../common/util'
  27. const props = defineProps(badgeProps)
  28. const content = computed(() => {
  29. const { modelValue, max, isDot } = props
  30. if (isDot) return ''
  31. let value = modelValue
  32. if (value && max && isNumber(value) && !Number.isNaN(value) && !Number.isNaN(max)) {
  33. value = max < value ? `${max}+` : value
  34. }
  35. return value
  36. })
  37. const contentStyle = computed(() => {
  38. const style: CSSProperties = {}
  39. if (isDef(props.bgColor)) {
  40. style.backgroundColor = props.bgColor
  41. }
  42. if (isDef(props.top)) {
  43. style.top = addUnit(props.top)
  44. }
  45. if (isDef(props.right)) {
  46. style.right = addUnit(props.right)
  47. }
  48. return objToStyle(style)
  49. })
  50. // 是否展示徽标数字
  51. const shouldShowBadge = computed(() => !props.hidden && (content.value || (content.value === 0 && props.showZero) || props.isDot))
  52. </script>
  53. <style lang="scss" scoped>
  54. @import './index.scss';
  55. </style>