Blame view

src/components/badge/badge.vue 3.55 KB
7fa943eb   梁灏   init
1
  <template>
15368be1   梁灏   Support Badge
2
      <span v-if="dot" :class="classes" ref="badge">
7fa943eb   梁灏   init
3
          <slot></slot>
457fe087   梁灏   update Badge style
4
          <sup :class="dotClasses" :style="styles" v-show="badge"></sup>
7fa943eb   梁灏   init
5
      </span>
0823f88b   梁灏   Badge add status
6
7
8
9
      <span v-else-if="status" :class="classes" class="ivu-badge-status" ref="badge">
          <span :class="statusClasses"></span>
          <span class="ivu-badge-status-text">{{ text }}</span>
      </span>
15368be1   梁灏   Support Badge
10
      <span v-else :class="classes" ref="badge">
7fa943eb   梁灏   init
11
          <slot></slot>
457fe087   梁灏   update Badge style
12
          <sup v-if="hasCount" :style="styles" :class="countClasses" v-show="badge">{{ finalCount }}</sup>
7fa943eb   梁灏   init
13
14
15
      </span>
  </template>
  <script>
fdafcd2c   梁灏   Badge add text prop
16
      import { oneOf } from '../../utils/assist';
7fa943eb   梁灏   init
17
18
19
      const prefixCls = 'ivu-badge';
  
      export default {
34ee7b4a   梁灏   support Tree & ad...
20
          name: 'Badge',
7fa943eb   梁灏   init
21
          props: {
fdafcd2c   梁灏   Badge add text prop
22
              count: Number,
7fa943eb   梁灏   init
23
24
25
26
27
28
29
30
              dot: {
                  type: Boolean,
                  default: false
              },
              overflowCount: {
                  type: [Number, String],
                  default: 99
              },
d3905b35   xiaofengsha   Badge 组件增加 showZe...
31
32
33
34
              className: String,
              showZero: {
                  type: Boolean,
                  default: false
fdafcd2c   梁灏   Badge add text prop
35
36
37
38
39
40
41
42
43
44
45
46
              },
              text: {
                  type: String,
                  default: ''
              },
              status: {
                  validator (value) {
                      return oneOf(value, ['success', 'processing', 'default', 'error', 'warning']);
                  }
              },
              offset: {
                  type: Array
d3905b35   xiaofengsha   Badge 组件增加 showZe...
47
              }
7fa943eb   梁灏   init
48
49
50
51
52
53
54
55
56
57
58
59
          },
          computed: {
              classes () {
                  return `${prefixCls}`;
              },
              dotClasses () {
                  return `${prefixCls}-dot`;
              },
              countClasses () {
                  return [
                      `${prefixCls}-count`,
                      {
15368be1   梁灏   Support Badge
60
                          [`${this.className}`]: !!this.className,
7fa943eb   梁灏   init
61
62
                          [`${prefixCls}-count-alone`]: this.alone
                      }
b0893113   jingsam   :art: add eslint
63
                  ];
7fa943eb   梁灏   init
64
              },
0823f88b   梁灏   Badge add status
65
66
67
68
69
70
71
72
              statusClasses () {
                  return [
                      `${prefixCls}-status-dot`,
                      {
                          [`${prefixCls}-status-${this.status}`]: !!this.status
                      }
                  ];
              },
457fe087   梁灏   update Badge style
73
74
75
76
77
78
79
80
              styles () {
                  const style = {};
                  if (this.offset && this.offset.length === 2) {
                      style['margin-top'] = `${this.offset[0]}px`;
                      style['margin-right'] = `${this.offset[1]}px`;
                  }
                  return style;
              },
7fa943eb   梁灏   init
81
              finalCount () {
fdafcd2c   梁灏   Badge add text prop
82
                  if (this.text !== '') return this.text;
7fa943eb   梁灏   init
83
84
85
86
87
88
89
90
91
92
93
                  return parseInt(this.count) >= parseInt(this.overflowCount) ? `${this.overflowCount}+` : this.count;
              },
              badge () {
                  let status = false;
  
                  if (this.count) {
                      status = !(parseInt(this.count) === 0);
                  }
  
                  if (this.dot) {
                      status = true;
f575eca7   Lison   update badge.vue
94
                      if (this.count !== null) {
7fa943eb   梁灏   init
95
96
97
98
99
100
                          if (parseInt(this.count) === 0) {
                              status = false;
                          }
                      }
                  }
  
fdafcd2c   梁灏   Badge add text prop
101
102
                  if (this.text !== '') status = true;
  
d3905b35   xiaofengsha   Badge 组件增加 showZe...
103
104
105
                  return status || this.showZero;
              },
              hasCount() {
fdafcd2c   梁灏   Badge add text prop
106
                  if(this.count || this.text !== '') return true;
d3905b35   xiaofengsha   Badge 组件增加 showZe...
107
108
                  if(this.showZero && parseInt(this.count) === 0) return true;
                  else return false;
75c32564   Aresn   fixed #646
109
110
111
              },
              alone () {
                  return this.$slots.default === undefined;
7fa943eb   梁灏   init
112
113
              }
          }
b0893113   jingsam   :art: add eslint
114
115
      };
  </script>