Blame view

src/components/tag/tag.vue 5.11 KB
7fa943eb   梁灏   init
1
  <template>
456daf34   梁灏   support Tag
2
      <transition name="fade">
7d4b325b   zhigang.li   close #2406
3
4
5
6
          <div :class="classes" @click.stop="check" :style="wraperStyles">
              <span :class="dotClasses" v-if="showDot" :style="bgColorStyle"></span>
              <span :class="textClasses" :style="textColorStyle"><slot></slot></span>
              <Icon v-if="closable" type="ios-close-empty" :color="lineColor" @click.native.stop="close"></Icon>
456daf34   梁灏   support Tag
7
8
          </div>
      </transition>
7fa943eb   梁灏   init
9
10
11
12
  </template>
  <script>
      import Icon from '../icon';
      import { oneOf } from '../../utils/assist';
7fa943eb   梁灏   init
13
      const prefixCls = 'ivu-tag';
7d4b325b   zhigang.li   close #2406
14
      const initColorList = ['blue', 'green', 'red', 'yellow', 'default'];
7fa943eb   梁灏   init
15
      export default {
34ee7b4a   梁灏   support Tree & ad...
16
          name: 'Tag',
7fa943eb   梁灏   init
17
18
19
20
21
22
          components: { Icon },
          props: {
              closable: {
                  type: Boolean,
                  default: false
              },
dc39cc31   daiyanze   Feature: Checkabl...
23
24
25
26
27
28
29
30
              checkable: {
                  type: Boolean,
                  default: false
              },
              checked: {
                  type: Boolean,
                  default: true
              },
7fa943eb   梁灏   init
31
              color: {
7d4b325b   zhigang.li   close #2406
32
33
                  type: String,
                  default: 'default'
382c000c   梁灏   Tag add type prop...
34
35
36
37
38
              },
              type: {
                  validator (value) {
                      return oneOf(value, ['border', 'dot']);
                  }
ec4117cb   梁灏   tag add prop: name
39
40
41
              },
              name: {
                  type: [String, Number]
7fa943eb   梁灏   init
42
43
              }
          },
dc39cc31   daiyanze   Feature: Checkabl...
44
45
46
47
48
          data () {
              return {
                  isChecked: this.checked
              };
          },
7fa943eb   梁灏   init
49
50
51
52
53
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
2079c47b   梁灏   update Tag
54
                          [`${prefixCls}-${this.color}`]: !!this.color,
382c000c   梁灏   Tag add type prop...
55
                          [`${prefixCls}-${this.type}`]: !!this.type,
dc39cc31   daiyanze   Feature: Checkabl...
56
                          [`${prefixCls}-closable`]: this.closable,
fd79f102   Aresn   Update tag.vue
57
                          [`${prefixCls}-checked`]: this.isChecked
7fa943eb   梁灏   init
58
                      }
b0893113   jingsam   :art: add eslint
59
                  ];
7fa943eb   梁灏   init
60
              },
7d4b325b   zhigang.li   close #2406
61
62
63
              wraperStyles () {
                  return oneOf(this.color, initColorList) ? {} : {background: this.defaultTypeColor, borderColor: this.lineColor, color: this.lineColor};
              },
7fa943eb   梁灏   init
64
              textClasses () {
7d4b325b   zhigang.li   close #2406
65
66
67
68
69
                  return [
                      `${prefixCls}-text`,
                      this.type === 'border' ? (oneOf(this.color, initColorList) ? `${prefixCls}-color-${this.color}` : '') : '',
                      (this.type !== 'dot' && this.type !== 'border' && this.color !== 'default') ? `${prefixCls}-color-white` : ''
                  ];
382c000c   梁灏   Tag add type prop...
70
71
72
73
74
75
              },
              dotClasses () {
                  return `${prefixCls}-dot-inner`;
              },
              showDot () {
                  return !!this.type && this.type === 'dot';
7d4b325b   zhigang.li   close #2406
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
              },
              lineColor () {
                  if (this.type === 'dot') {
                      return '';
                  } else if (this.type === 'border') {
                      return this.color !== undefined ? this.transferColor(this.color) : '';
                  } else {
                      return this.color !== undefined ? (this.color === 'default' ? '' : 'rgb(255, 255, 255)') : '';
                  }
              },
              borderColor () {
                  if (this.type === 'dot') {
                      return '';
                  } else if (this.type === 'border') {
                      return this.color !== undefined ? this.transferColor(this.color) : '';
                  } else {
                      return '';
                  }
              },
              textColorStyle () {
                  return oneOf(this.color, initColorList) ? {} : {color: this.lineColor};
              },
              mainColor () {
                  return this.color !== undefined ? this.transferColor(this.color) : '';
              },
              bgColorStyle () {
                  return oneOf(this.color, initColorList) ? {} : {background: this.mainColor};
              },
              defaultTypeColor () {
                  return (this.type !== 'dot' && this.type !== 'border') ? (this.color !== undefined ? this.transferColor(this.color) : '') : '';
7fa943eb   梁灏   init
106
107
108
              }
          },
          methods: {
ec4117cb   梁灏   tag add prop: name
109
              close (event) {
4c6d9962   Aresn   Update tag.vue
110
111
112
113
114
                  if (this.name === undefined) {
                      this.$emit('on-close', event);
                  } else {
                      this.$emit('on-close', event, this.name);
                  }
dc39cc31   daiyanze   Feature: Checkabl...
115
              },
4c6d9962   Aresn   Update tag.vue
116
117
118
119
              check () {
                  if (!this.checkable) return;
                  const checked = !this.isChecked;
                  this.isChecked = checked;
ec4117cb   梁灏   tag add prop: name
120
                  if (this.name === undefined) {
4c6d9962   Aresn   Update tag.vue
121
                      this.$emit('on-change', checked);
ec4117cb   梁灏   tag add prop: name
122
                  } else {
4c6d9962   Aresn   Update tag.vue
123
                      this.$emit('on-change', checked, this.name);
ec4117cb   梁灏   tag add prop: name
124
                  }
7d4b325b   zhigang.li   close #2406
125
126
127
128
129
130
131
132
133
134
135
136
137
              },
              transferColor (name) {
                  if (oneOf(name, initColorList)) {
                      switch (name) {
                          case 'red': return '#ed3f14';
                          case 'green': return '#19be6b';
                          case 'yellow': return '#ff9900';
                          case 'blue': return '#2d8cf0';
                          case 'default': return '';
                      }
                  } else {
                      return name;
                  }
7fa943eb   梁灏   init
138
139
              }
          }
b0893113   jingsam   :art: add eslint
140
      };
7d4b325b   zhigang.li   close #2406
141
  </script>