Blame view

node_modules/uview-ui/components/u-icon/u-icon.vue 9.76 KB
c7add6cf   “wangming”   初始版本开发完毕
1
  <template>
25852764   unknown   s
2
3
4
5
6
7
8
9
  	<view :style="[customStyle]" class="u-icon" @tap="click" :class="['u-icon--' + labelPos]">
  		<image class="u-icon__img" v-if="isImg" :src="name" :mode="imgMode" :style="[imgStyle]"></image>
  		<text v-else class="u-icon__icon" :class="customClass" :style="[iconStyle]" :hover-class="hoverClass"
  			  @touchstart="touchstart">
  			<text v-if="showDecimalIcon" :style="[decimalIconStyle]" :class="decimalIconClass" :hover-class="hoverClass"
  				  class="u-icon__decimal">
  			</text>
  		</text>
c7add6cf   “wangming”   初始版本开发完毕
10
  		<!-- 这里进行空字符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示 -->
25852764   unknown   s
11
  		<text v-if="label !== ''" class="u-icon__label" :style="{
c7add6cf   “wangming”   初始版本开发完毕
12
13
  			color: labelColor,
  			fontSize: $u.addUnit(labelSize),
25852764   unknown   s
14
15
16
17
18
19
  			marginLeft: labelPos == 'right' ? $u.addUnit(marginLeft) : 0,
  			marginTop: labelPos == 'bottom' ? $u.addUnit(marginTop) : 0,
  			marginRight: labelPos == 'left' ? $u.addUnit(marginRight) : 0,
  			marginBottom: labelPos == 'top' ? $u.addUnit(marginBottom) : 0,
  		}">{{ label }}
  		</text>
c7add6cf   “wangming”   初始版本开发完毕
20
21
22
23
  	</view>
  </template>
  
  <script>
25852764   unknown   s
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
  /**
   * icon 图标
   * @description 基于字体的图标集,包含了大多数常见场景的图标。
   * @tutorial https://www.uviewui.com/components/icon.html
   * @property {String} name 图标名称,见示例图标集
   * @property {String} color 图标颜色(默认inherit
   * @property {String | Number} size 图标字体大小,单位rpx(默认32
   * @property {String | Number} label-size label字体大小,单位rpx(默认28
   * @property {String} label 图标右侧的label文字(默认28
   * @property {String} label-pos label文字相对于图标的位置,只能rightbottom(默认right
   * @property {String} label-color label字体颜色(默认#606266
   * @property {Object} custom-style icon的样式,对象形式
   * @property {String} custom-prefix 自定义字体图标库时,需要写上此值
   * @property {String | Number} margin-left label在右侧时与图标的距离,单位rpx(默认6
   * @property {String | Number} margin-top label在下方时与图标的距离,单位rpx(默认6
   * @property {String | Number} margin-bottom label在上方时与图标的距离,单位rpx(默认6
   * @property {String | Number} margin-right label在左侧时与图标的距离,单位rpx(默认6
   * @property {String} label-pos label相对于图标的位置,只能rightbottom(默认right
   * @property {String} index 一个用于区分多个图标的值,点击图标时通过click事件传出
   * @property {String} hover-class 图标按下去的样式类,用法同uniview组件的hover-class参数,详情见官网
   * @property {String} width 显示图片小图标时的宽度
   * @property {String} height 显示图片小图标时的高度
   * @property {String} top 图标在垂直方向上的定位
   * @property {String} top 图标在垂直方向上的定位
   * @property {String} top 图标在垂直方向上的定位
   * @property {Boolean} show-decimal-icon 是否为DecimalIcon
   * @property {String} inactive-color 背景颜色,可接受主题色,仅Decimal时有效
   * @property {String | Number} percent 显示的百分比,仅Decimal时有效
   * @event {Function} click 点击图标时触发
   * @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
   */
  export default {
  	name: 'u-icon',
  	props: {
  		// 图标类名
  		name: {
  			type: String,
  			default: ''
  		},
  		// 图标颜色,可接受主题色
  		color: {
  			type: String,
  			default: ''
  		},
  		// 字体大小,单位rpx
  		size: {
  			type: [Number, String],
  			default: 'inherit'
  		},
  		// 是否显示粗体
  		bold: {
  			type: Boolean,
  			default: false
  		},
  		// 点击图标的时候传递事件出去的index(用于区分点击了哪一个)
  		index: {
  			type: [Number, String],
  			default: ''
  		},
  		// 触摸图标时的类名
  		hoverClass: {
  			type: String,
  			default: ''
  		},
  		// 自定义扩展前缀,方便用户扩展自己的图标库
  		customPrefix: {
  			type: String,
  			default: 'uicon'
  		},
  		// 图标右边或者下面的文字
  		label: {
  			type: [String, Number],
  			default: ''
  		},
  		// label的位置,只能右边或者下边
  		labelPos: {
  			type: String,
  			default: 'right'
  		},
  		// label的大小
  		labelSize: {
  			type: [String, Number],
  			default: '28'
  		},
  		// label的颜色
  		labelColor: {
  			type: String,
  			default: '#606266'
  		},
  		// label与图标的距离(横向排列)
  		marginLeft: {
  			type: [String, Number],
  			default: '6'
  		},
  		// label与图标的距离(竖向排列)
  		marginTop: {
  			type: [String, Number],
  			default: '6'
  		},
  		// label与图标的距离(竖向排列)
  		marginRight: {
  			type: [String, Number],
  			default: '6'
  		},
  		// label与图标的距离(竖向排列)
  		marginBottom: {
  			type: [String, Number],
  			default: '6'
  		},
  		// 图片的mode
  		imgMode: {
  			type: String,
  			default: 'widthFix'
  		},
  		// 自定义样式
  		customStyle: {
  			type: Object,
  			default() {
  				return {}
  			}
  		},
  		// 用于显示图片小图标时,图片的宽度
  		width: {
  			type: [String, Number],
  			default: ''
  		},
  		// 用于显示图片小图标时,图片的高度
  		height: {
  			type: [String, Number],
  			default: ''
  		},
  		// 用于解决某些情况下,让图标垂直居中的用途
  		top: {
  			type: [String, Number],
  			default: 0
  		},
  		// 是否为DecimalIcon
  		showDecimalIcon: {
  			type: Boolean,
  			default: false
  		},
  		// 背景颜色,可接受主题色,仅Decimal时有效
  		inactiveColor: {
  			type: String,
  			default: '#ececec'
  		},
  		// 显示的百分比,仅Decimal时有效
  		percent: {
  			type: [Number, String],
  			default: '50'
  		}
  	},
  	computed: {
  		customClass() {
  			let classes = []
  			classes.push(this.customPrefix + '-' + this.name)
  			// uView的自定义图标类名为u-iconfont
  			if (this.customPrefix == 'uicon') {
  				classes.push('u-iconfont')
  			} else {
  				classes.push(this.customPrefix)
c7add6cf   “wangming”   初始版本开发完毕
185
  			}
25852764   unknown   s
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
  			// 主题色,通过类配置
  			if (this.showDecimalIcon && this.inactiveColor && this.$u.config.type.includes(this.inactiveColor)) {
  				classes.push('u-icon__icon--' + this.inactiveColor)
  			} else if (this.color && this.$u.config.type.includes(this.color)) classes.push('u-icon__icon--' + this.color)
  			// 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
  			// 故需将其拆成一个字符串的形式,通过空格隔开各个类名
  			//#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  			classes = classes.join(' ')
  			//#endif
  			return classes
  		},
  		iconStyle() {
  			let style = {}
  			style = {
  				fontSize: this.size == 'inherit' ? 'inherit' : this.$u.addUnit(this.size),
  				fontWeight: this.bold ? 'bold' : 'normal',
  				// 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
  				top: this.$u.addUnit(this.top)
  			}
  			// 非主题色值时,才当作颜色值
  			if (this.showDecimalIcon && this.inactiveColor && !this.$u.config.type.includes(this.inactiveColor)) {
  				style.color = this.inactiveColor
  			} else if (this.color && !this.$u.config.type.includes(this.color)) style.color = this.color
  
  			return style
  		},
  		// 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
  		isImg() {
  			return this.name.indexOf('/') !== -1
c7add6cf   “wangming”   初始版本开发完毕
215
  		},
25852764   unknown   s
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  		imgStyle() {
  			let style = {}
  			// 如果设置width和height属性,则优先使用,否则使用size属性
  			style.width = this.width ? this.$u.addUnit(this.width) : this.$u.addUnit(this.size)
  			style.height = this.height ? this.$u.addUnit(this.height) : this.$u.addUnit(this.size)
  			return style
  		},
  		decimalIconStyle() {
  			let style = {}
  			style = {
  				fontSize: this.size == 'inherit' ? 'inherit' : this.$u.addUnit(this.size),
  				fontWeight: this.bold ? 'bold' : 'normal',
  				// 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
  				top: this.$u.addUnit(this.top),
  				width: this.percent + '%'
c7add6cf   “wangming”   初始版本开发完毕
231
  			}
25852764   unknown   s
232
233
234
  			// 非主题色值时,才当作颜色值
  			if (this.color && !this.$u.config.type.includes(this.color)) style.color = this.color
  			return style
c7add6cf   “wangming”   初始版本开发完毕
235
  		},
25852764   unknown   s
236
237
238
239
240
241
242
243
  		decimalIconClass() {
  			let classes = []
  			classes.push(this.customPrefix + '-' + this.name)
  			// uView的自定义图标类名为u-iconfont
  			if (this.customPrefix == 'uicon') {
  				classes.push('u-iconfont')
  			} else {
  				classes.push(this.customPrefix)
c7add6cf   “wangming”   初始版本开发完毕
244
  			}
25852764   unknown   s
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  			// 主题色,通过类配置
  			if (this.color && this.$u.config.type.includes(this.color)) classes.push('u-icon__icon--' + this.color)
  			else classes.push('u-icon__icon--primary')
  			// 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
  			// 故需将其拆成一个字符串的形式,通过空格隔开各个类名
  			//#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  			classes = classes.join(' ')
  			//#endif
  			return classes
  		}
  	},
  	methods: {
  		click() {
  			this.$emit('click', this.index)
  		},
  		touchstart() {
  			this.$emit('touchstart', this.index)
c7add6cf   “wangming”   初始版本开发完毕
262
263
  		}
  	}
25852764   unknown   s
264
  }
c7add6cf   “wangming”   初始版本开发完毕
265
266
  </script>
  
25852764   unknown   s
267
268
269
  <style scoped lang="scss">
  @import "../../libs/css/style.components.scss";
  @import '../../iconfont.css';
c7add6cf   “wangming”   初始版本开发完毕
270
  
25852764   unknown   s
271
272
273
  .u-icon {
  	display: inline-flex;
  	align-items: center;
c7add6cf   “wangming”   初始版本开发完毕
274
  
25852764   unknown   s
275
276
  	&--left {
  		flex-direction: row-reverse;
c7add6cf   “wangming”   初始版本开发完毕
277
  		align-items: center;
25852764   unknown   s
278
  	}
c7add6cf   “wangming”   初始版本开发完毕
279
  
25852764   unknown   s
280
281
282
283
  	&--right {
  		flex-direction: row;
  		align-items: center;
  	}
c7add6cf   “wangming”   初始版本开发完毕
284
  
25852764   unknown   s
285
286
287
288
  	&--top {
  		flex-direction: column-reverse;
  		justify-content: center;
  	}
c7add6cf   “wangming”   初始版本开发完毕
289
  
25852764   unknown   s
290
291
292
293
  	&--bottom {
  		flex-direction: column;
  		justify-content: center;
  	}
c7add6cf   “wangming”   初始版本开发完毕
294
  
25852764   unknown   s
295
296
  	&__icon {
  		position: relative;
c7add6cf   “wangming”   初始版本开发完毕
297
  
25852764   unknown   s
298
299
300
  		&--primary {
  			color: $u-type-primary;
  		}
c7add6cf   “wangming”   初始版本开发完毕
301
  
25852764   unknown   s
302
303
304
  		&--success {
  			color: $u-type-success;
  		}
c7add6cf   “wangming”   初始版本开发完毕
305
  
25852764   unknown   s
306
307
  		&--error {
  			color: $u-type-error;
c7add6cf   “wangming”   初始版本开发完毕
308
309
  		}
  
25852764   unknown   s
310
311
  		&--warning {
  			color: $u-type-warning;
c7add6cf   “wangming”   初始版本开发完毕
312
313
  		}
  
25852764   unknown   s
314
315
  		&--info {
  			color: $u-type-info;
c7add6cf   “wangming”   初始版本开发完毕
316
317
  		}
  	}
25852764   unknown   s
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  
  	&__decimal {
  		position: absolute;
  		top: 0;
  		left: 0;
  		display: inline-block;
  		overflow: hidden;
  	}
  
  	&__img {
  		height: auto;
  		will-change: transform;
  	}
  
  	&__label {
  		line-height: 1;
  	}
  }
c7add6cf   “wangming”   初始版本开发完毕
336
  </style>