dengjunjie
5 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<template>
  <view
    class="tn-tag-class tn-tag"
    :class="[
      tagClass,
      backgroundColorClass,
      fontColorClass
    ]"
    :style="[tagStyle]"
    @tap="handleClick"
  >
    <slot></slot>
  </view>
</template>
 
<script>
  import componentsColorMixin from '../../libs/mixin/components_color.js'
  export default {
    mixins: [ componentsColorMixin ],
    options: {
        // 在微信小程序中将组件节点渲染为虚拟节点,更加接近Vue组件的表现(不会出现shadow节点下再去创建元素)
        virtualHost: true
    },
    name: 'tn-tag',
    props: {
      // 序号,用于区分多个标签
      index: {
        type: [Number, String],
        default: '0'
      },
      // 形状 圆角 radius 椭圆  circle 左半圆 circleLeft 右半圆 circleRight
      shape: {
        type: String,
        default: ''
      },
      // 标签大小 sm lg
      size: {
        type: String,
        default: ''
      },
      // 宽度
      width: {
        type: String,
        default: ''
      },
      // 高度
      height: {
        type: String,
        default: ''
      },
      // 内边距
      padding: {
        type: String,
        default: ''
      },
      // 外边距
      margin: {
        type: String,
        default: '0'
      },
      // 是否镂空
      plain: {
        type: Boolean,
        default: false
      },
      // 是否将元素基点设置在左边
      originLeft: {
        type: Boolean,
        default: false
      },
      // 是否将元素基点设置在右边
      originRight: {
        type: Boolean,
        default: false
      }
    },
    computed: {
      tagClass() {
        let clazz = ''
        // 设置标签的形状
        switch(this.shape) {
          case 'radius':
            clazz += ' tn-radius'
            break
          case 'circle':
            clazz += ' tn-round'
            break
          case 'circleLeft':
            clazz += ' tn-tag--fillet-left'
            break
          case 'circleRight':
            clazz += ' tn-tag--fillet-right'
            break
        }
        
        // 设置为镂空并且设置镂空便可才进行设置
        if (this.plain) {
          clazz += ' tn-tag--plain tn-border-solid'
          if (this.backgroundColor !== '' && this.backgroundColor.includes('tn-bg')) {
            const color = this.backgroundColor.slice(this.backgroundColor.lastIndexOf('-') + 1)
            clazz += ` tn-border-${color}`
          }
        }
        
        // 设置基准点
        if (this.originLeft) {
          clazz += ' tn-tag--origin-left'
        }
        if (this.originRight) {
          clazz += ' tn-tag--origin-right'
        }
        
        return clazz
      },
      tagStyle() {
        let style = {}
        switch(this.size) {
          case 'sm':
            style.padding = '0 12rpx'
            style.fontSize = '20rpx'
            style.height = '32rpx'
            break
          case 'lg':
            style.padding = '0 20rpx'
            style.fontSize = '28rpx'
            style.height = '62rpx'
            break
          default:
            style.padding = '0 16rpx'
            style.fontSize = '24rpx'
            style.height = '48rpx'
            break
        }
        
        style.width = this.width || '120rpx'
        style.height = this.height || style.height
        
        style.padding = this.padding || style.padding
        if (this.margin) {
          style.margin = this.margin
        }
        
        if (this.fontColorStyle) {
          style.color = this.fontColorStyle
        }
        if (this.fontSize !== 0) {
          style.fontSize = this.fontSize + this.fontUnit
        }
 
        if (!this.backgroundColorClass) {
          style.backgroundColor = !this.plain ? (this.backgroundColorStyle || '#FFFFFF') : ''
          if (this.plain) {
            style.borderColor = (this.backgroundColorStyle || '#080808')
          }
        }
        
        
        
        return style
      },
    },
    data() {
      return {
 
      }
    },
    methods: {
      // 处理点击事件
      handleClick() {
        this.$emit('click', {
          index: Number(this.index)
        })
        this.$emit('tap', {
          index: Number(this.index)
        })
      },
    }
  }
</script>
 
<style lang="scss" scoped>
  .tn-tag {
    vertical-align: middle;
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    font-family: Helvetica Neue, Helvetica, sans-serif;
    white-space: nowrap;
    // color: #FFFFFF;
    
    &--fillet-left {
      border-radius: 50rpx 0 0 50rpx;
    }
    
    &--fillet-right {
      border-radius: 0 50rpx 50rpx 0;
    }
    
    &--plain {
      background-color: transparent !important;
      background-image: none;
      
      &.tn-round {
        border-radius: 1000rpx !important;
      }
      
      &.tn-radius {
        border-radius: 12rpx !important;
      }
    }
    
    &--origin-left {
      transform-origin: 0 center;
    }
    
    &--origin-right {
      transform-origin: 100% center;
    }
  }
  
</style>