index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import styleToCssString from '../helpers/libs/styleToCssString'
  4. baseComponent({
  5. externalClasses: ['wux-class-badge'],
  6. properties: {
  7. prefixCls: {
  8. type: String,
  9. value: 'wux-badge',
  10. },
  11. count: {
  12. type: Number,
  13. value: 0,
  14. },
  15. overflowCount: {
  16. type: Number,
  17. value: 99,
  18. },
  19. dot: {
  20. type: Boolean,
  21. value: false,
  22. },
  23. showZero: {
  24. type: Boolean,
  25. value: false,
  26. },
  27. status: {
  28. type: String,
  29. value: '',
  30. },
  31. text: {
  32. type: String,
  33. value: '',
  34. },
  35. position: {
  36. type: String,
  37. value: 'topRight',
  38. },
  39. backgroundColor: {
  40. type: String,
  41. value: '#ed3f14',
  42. },
  43. hideShadow: {
  44. type: Boolean,
  45. value: false,
  46. },
  47. title: {
  48. type: String,
  49. value: '',
  50. },
  51. },
  52. data: {
  53. finalCount: 0,
  54. badgeStyle: '',
  55. },
  56. observers: {
  57. ['count, overflowCount'](count, overflowCount) {
  58. this.updated({
  59. count,
  60. overflowCount,
  61. })
  62. },
  63. backgroundColor(newVal) {
  64. this.updateStyle(newVal)
  65. },
  66. },
  67. computed: {
  68. classes: ['prefixCls, position, hideShadow, status', function(prefixCls, position, hideShadow, st) {
  69. const wrap = classNames(prefixCls, {
  70. [`${prefixCls}--position-${position}`]: position,
  71. [`${prefixCls}--hide-shadow`]: hideShadow,
  72. })
  73. const status = `${prefixCls}__status`
  74. const statusDot = classNames(`${prefixCls}__status-dot`, {
  75. [`${prefixCls}__status-dot--${st}`]: st,
  76. })
  77. const statusText = `${prefixCls}__status-text`
  78. const dot = `${prefixCls}__dot`
  79. const count = `${prefixCls}__count`
  80. return {
  81. wrap,
  82. status,
  83. statusDot,
  84. statusText,
  85. dot,
  86. count,
  87. }
  88. }],
  89. },
  90. methods: {
  91. updated(props = this.data) {
  92. const { count, overflowCount } = props
  93. const finalCount = count >= overflowCount ? `${overflowCount}+` : count
  94. this.setData({
  95. finalCount,
  96. })
  97. },
  98. updateStyle(backgroundColor) {
  99. const badgeStyle = styleToCssString({
  100. backgroundColor,
  101. })
  102. if (badgeStyle !== this.data.badgeStyle) {
  103. this.setData({
  104. badgeStyle,
  105. })
  106. }
  107. },
  108. },
  109. })