1
1
<template >
2
2
<view :style =" [customStyle]" class =" u-icon" @tap =" click" :class =" ['u-icon--' + labelPos]" >
3
3
<image class =" u-icon__img" v-if =" isImg" :src =" name" :mode =" imgMode" :style =" [imgStyle]" ></image >
4
- <text v-else class =" u-icon__icon" :class =" customClass" :style =" [iconStyle]" :hover-class =" hoverClass" @touchstart =" touchstart" ></text >
4
+ <text v-else class =" u-icon__icon" :class =" customClass" :style =" [iconStyle]" :hover-class =" hoverClass"
5
+ @touchstart =" touchstart" >
6
+ <text v-if =" showDecimalIcon" :style =" [decimalIconStyle]" :class =" decimalIconClass" :hover-class =" hoverClass"
7
+ class =" u-icon__decimal" >
8
+ </text >
9
+ </text >
5
10
<!-- 这里进行空字符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示 -->
6
11
<text v-if =" label !== ''" class =" u-icon__label" :style =" {
7
12
color: labelColor,
10
15
marginTop: labelPos == 'bottom' ? $u.addUnit(marginTop) : 0,
11
16
marginRight: labelPos == 'left' ? $u.addUnit(marginRight) : 0,
12
17
marginBottom: labelPos == 'top' ? $u.addUnit(marginBottom) : 0,
13
- }" >{{label}}</text >
18
+ }" >{{ label }}
19
+ </text >
14
20
</view >
15
21
</template >
16
22
38
44
* @property {String} width 显示图片小图标时的宽度
39
45
* @property {String} height 显示图片小图标时的高度
40
46
* @property {String} top 图标在垂直方向上的定位
47
+ * @property {String} top 图标在垂直方向上的定位
48
+ * @property {String} top 图标在垂直方向上的定位
49
+ * @property {Boolean} show -decimal-icon 是否为DecimalIcon
50
+ * @property {String} inactive -color 背景颜色,可接受主题色,仅Decimal时有效
51
+ * @property {String | Number} percent 显示的百分比,仅Decimal时有效
41
52
* @event {Function} click 点击图标时触发
42
53
* @example < u- icon name= " photo" color= " #2979ff" size= " 28" >< / u- icon>
43
54
*/
@@ -145,71 +156,120 @@ export default {
145
156
top: {
146
157
type: [String , Number ],
147
158
default: 0
159
+ },
160
+ // 是否为DecimalIcon
161
+ showDecimalIcon: {
162
+ type: Boolean ,
163
+ default: false
164
+ },
165
+ // 背景颜色,可接受主题色,仅Decimal时有效
166
+ inactiveColor: {
167
+ type: String ,
168
+ default: ' #ececec'
169
+ },
170
+ // 显示的百分比,仅Decimal时有效
171
+ percent: {
172
+ type: [Number , String ],
173
+ default: ' 50'
148
174
}
149
175
},
150
176
computed: {
151
177
customClass () {
152
- let classes = [];
153
- classes .push (this .customPrefix + ' -' + this .name );
178
+ let classes = []
179
+ classes .push (this .customPrefix + ' -' + this .name )
154
180
// uView的自定义图标类名为u-iconfont
155
- if (this .customPrefix == ' uicon' ) classes .push (' u-iconfont' );
156
- else classes .push (this .customPrefix );
181
+ if (this .customPrefix == ' uicon' ) {
182
+ classes .push (' u-iconfont' )
183
+ } else {
184
+ classes .push (this .customPrefix )
185
+ }
157
186
// 主题色,通过类配置
158
- if (this .color && this .$u .config .type .includes (this .color )) classes .push (' u-icon__icon--' + this .color );
187
+ if (this .showDecimalIcon && this .inactiveColor && this .$u .config .type .includes (this .inactiveColor )) {
188
+ classes .push (' u-icon__icon--' + this .inactiveColor )
189
+ } else if (this .color && this .$u .config .type .includes (this .color )) classes .push (' u-icon__icon--' + this .color )
159
190
// 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
160
191
// 故需将其拆成一个字符串的形式,通过空格隔开各个类名
161
192
// #ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
162
- classes = classes .join (' ' );
193
+ classes = classes .join (' ' )
163
194
// #endif
164
- return classes;
195
+ return classes
165
196
},
166
197
iconStyle () {
167
- let style = {};
198
+ let style = {}
168
199
style = {
169
200
fontSize: this .size == ' inherit' ? ' inherit' : this .$u .addUnit (this .size ),
170
201
fontWeight: this .bold ? ' bold' : ' normal' ,
171
202
// 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
172
203
top: this .$u .addUnit (this .top )
173
- };
204
+ }
174
205
// 非主题色值时,才当作颜色值
175
- if (this .color && ! this .$u .config .type .includes (this .color )) style .color = this .color ;
176
- return style;
206
+ if (this .showDecimalIcon && this .inactiveColor && ! this .$u .config .type .includes (this .inactiveColor )) {
207
+ style .color = this .inactiveColor
208
+ } else if (this .color && ! this .$u .config .type .includes (this .color )) style .color = this .color
209
+
210
+ return style
177
211
},
178
212
// 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
179
213
isImg () {
180
- return this .name .indexOf (' /' ) !== - 1 ;
214
+ return this .name .indexOf (' /' ) !== - 1
181
215
},
182
216
imgStyle () {
183
- let style = {};
217
+ let style = {}
184
218
// 如果设置width和height属性,则优先使用,否则使用size属性
185
- style .width = this .width ? this .$u .addUnit (this .width ) : this .$u .addUnit (this .size );
186
- style .height = this .height ? this .$u .addUnit (this .height ) : this .$u .addUnit (this .size );
187
- return style;
219
+ style .width = this .width ? this .$u .addUnit (this .width ) : this .$u .addUnit (this .size )
220
+ style .height = this .height ? this .$u .addUnit (this .height ) : this .$u .addUnit (this .size )
221
+ return style
222
+ },
223
+ decimalIconStyle () {
224
+ let style = {}
225
+ style = {
226
+ fontSize: this .size == ' inherit' ? ' inherit' : this .$u .addUnit (this .size ),
227
+ fontWeight: this .bold ? ' bold' : ' normal' ,
228
+ // 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
229
+ top: this .$u .addUnit (this .top ),
230
+ width: this .percent + ' %'
231
+ }
232
+ // 非主题色值时,才当作颜色值
233
+ if (this .color && ! this .$u .config .type .includes (this .color )) style .color = this .color
234
+ return style
235
+ },
236
+ decimalIconClass () {
237
+ let classes = []
238
+ classes .push (this .customPrefix + ' -' + this .name )
239
+ // uView的自定义图标类名为u-iconfont
240
+ if (this .customPrefix == ' uicon' ) {
241
+ classes .push (' u-iconfont' )
242
+ } else {
243
+ classes .push (this .customPrefix )
244
+ }
245
+ // 主题色,通过类配置
246
+ if (this .color && this .$u .config .type .includes (this .color )) classes .push (' u-icon__icon--' + this .color )
247
+ else classes .push (' u-icon__icon--primary' )
248
+ // 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
249
+ // 故需将其拆成一个字符串的形式,通过空格隔开各个类名
250
+ // #ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
251
+ classes = classes .join (' ' )
252
+ // #endif
253
+ return classes
188
254
}
189
255
},
190
256
methods: {
191
257
click () {
192
- this .$emit (' click' , this .index );
258
+ this .$emit (' click' , this .index )
193
259
},
194
260
touchstart () {
195
- this .$emit (' touchstart' , this .index );
261
+ this .$emit (' touchstart' , this .index )
196
262
}
197
263
}
198
- };
264
+ }
199
265
</script >
200
266
201
267
<style scoped lang="scss">
202
268
@import " ../../libs/css/style.components.scss" ;
203
- /* #ifndef APP-NVUE */
204
- // 目前由于nvue对定义字体时的content属性报错,所以nvue先不引入
205
269
@import ' ../../iconfont.css' ;
206
- /* #endif */
207
270
208
271
.u-icon {
209
- /* #ifndef APP-NVUE */
210
272
display : inline-flex ;
211
- /* #endif */
212
- flex-direction : row ;
213
273
align-items : center ;
214
274
215
275
& --left {
@@ -234,7 +294,7 @@ export default {
234
294
235
295
& __icon {
236
296
position : relative ;
237
-
297
+
238
298
& --primary {
239
299
color : $u-type-primary ;
240
300
}
@@ -256,11 +316,17 @@ export default {
256
316
}
257
317
}
258
318
319
+ & __decimal {
320
+ position : absolute ;
321
+ top : 0 ;
322
+ left : 0 ;
323
+ display : inline-block ;
324
+ overflow : hidden ;
325
+ }
326
+
259
327
& __img {
260
- /* #ifndef APP-PLUS */
261
328
height : auto ;
262
329
will-change : transform ;
263
- /* #endif */
264
330
}
265
331
266
332
& __label {
0 commit comments