Skip to content

Commit 279115b

Browse files
committed
新增:图标ICON进度/半星显示,为评分半星做支持
1 parent a25e047 commit 279115b

File tree

1 file changed

+96
-30
lines changed

1 file changed

+96
-30
lines changed

uview-ui/components/u-icon/u-icon.vue

Lines changed: 96 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<template>
22
<view :style="[customStyle]" class="u-icon" @tap="click" :class="['u-icon--' + labelPos]">
33
<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>
510
<!-- 这里进行空字符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示 -->
611
<text v-if="label !== ''" class="u-icon__label" :style="{
712
color: labelColor,
@@ -10,7 +15,8 @@
1015
marginTop: labelPos == 'bottom' ? $u.addUnit(marginTop) : 0,
1116
marginRight: labelPos == 'left' ? $u.addUnit(marginRight) : 0,
1217
marginBottom: labelPos == 'top' ? $u.addUnit(marginBottom) : 0,
13-
}">{{label}}</text>
18+
}">{{ label }}
19+
</text>
1420
</view>
1521
</template>
1622

@@ -38,6 +44,11 @@
3844
* @property {String} width 显示图片小图标时的宽度
3945
* @property {String} height 显示图片小图标时的高度
4046
* @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时有效
4152
* @event {Function} click 点击图标时触发
4253
* @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
4354
*/
@@ -145,71 +156,120 @@ export default {
145156
top: {
146157
type: [String, Number],
147158
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'
148174
}
149175
},
150176
computed: {
151177
customClass() {
152-
let classes = [];
153-
classes.push(this.customPrefix + '-' + this.name);
178+
let classes = []
179+
classes.push(this.customPrefix + '-' + this.name)
154180
// 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+
}
157186
// 主题色,通过类配置
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)
159190
// 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
160191
// 故需将其拆成一个字符串的形式,通过空格隔开各个类名
161192
//#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
162-
classes = classes.join(' ');
193+
classes = classes.join(' ')
163194
//#endif
164-
return classes;
195+
return classes
165196
},
166197
iconStyle() {
167-
let style = {};
198+
let style = {}
168199
style = {
169200
fontSize: this.size == 'inherit' ? 'inherit' : this.$u.addUnit(this.size),
170201
fontWeight: this.bold ? 'bold' : 'normal',
171202
// 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
172203
top: this.$u.addUnit(this.top)
173-
};
204+
}
174205
// 非主题色值时,才当作颜色值
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
177211
},
178212
// 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
179213
isImg() {
180-
return this.name.indexOf('/') !== -1;
214+
return this.name.indexOf('/') !== -1
181215
},
182216
imgStyle() {
183-
let style = {};
217+
let style = {}
184218
// 如果设置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
188254
}
189255
},
190256
methods: {
191257
click() {
192-
this.$emit('click', this.index);
258+
this.$emit('click', this.index)
193259
},
194260
touchstart() {
195-
this.$emit('touchstart', this.index);
261+
this.$emit('touchstart', this.index)
196262
}
197263
}
198-
};
264+
}
199265
</script>
200266

201267
<style scoped lang="scss">
202268
@import "../../libs/css/style.components.scss";
203-
/* #ifndef APP-NVUE */
204-
// 目前由于nvue对定义字体时的content属性报错,所以nvue先不引入
205269
@import '../../iconfont.css';
206-
/* #endif */
207270
208271
.u-icon {
209-
/* #ifndef APP-NVUE */
210272
display: inline-flex;
211-
/* #endif */
212-
flex-direction: row;
213273
align-items: center;
214274
215275
&--left {
@@ -234,7 +294,7 @@ export default {
234294
235295
&__icon {
236296
position: relative;
237-
297+
238298
&--primary {
239299
color: $u-type-primary;
240300
}
@@ -256,11 +316,17 @@ export default {
256316
}
257317
}
258318
319+
&__decimal {
320+
position: absolute;
321+
top: 0;
322+
left: 0;
323+
display: inline-block;
324+
overflow: hidden;
325+
}
326+
259327
&__img {
260-
/* #ifndef APP-PLUS */
261328
height: auto;
262329
will-change: transform;
263-
/* #endif */
264330
}
265331
266332
&__label {

0 commit comments

Comments
 (0)