Commit 31a991ae authored by wlxuqu's avatar wlxuqu

Merge branch 'dev' into yiruiwen

parents ef176027 df1303d6
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<u-rate v-model="value" :count="count" @change="change" <u-rate v-model="value" :count="count" @change="change"
:active-color="activeColor" :inaction-color="inactiveColor" :active-color="activeColor" :inaction-color="inactiveColor"
:active-icon="activeIcon" :inactive-icon="inactiveIcon" :active-icon="activeIcon" :inactive-icon="inactiveIcon"
:disabled="disabled"></u-rate> :disabled="disabled" :colors="colors" :icons="icons"></u-rate>
</view> </view>
</view> </view>
<view class="u-config-wrap"> <view class="u-config-wrap">
...@@ -29,6 +29,10 @@ ...@@ -29,6 +29,10 @@
<view class="u-item-title">自定义图标</view> <view class="u-item-title">自定义图标</view>
<u-subsection vibrateShort current="1" :list="['是', '否']" @change="iconChange"></u-subsection> <u-subsection vibrateShort current="1" :list="['是', '否']" @change="iconChange"></u-subsection>
</view> </view>
<view class="u-config-item">
<view class="u-item-title">是否分层</view>
<u-subsection vibrateShort current="1" :list="['是', '否']" @change="decimalChange"></u-subsection>
</view>
<view class="u-config-item"> <view class="u-config-item">
<view class="u-item-title">是否禁用</view> <view class="u-item-title">是否禁用</view>
<u-subsection vibrateShort current="1" :list="['是', '否']" @change="disabledChange"></u-subsection> <u-subsection vibrateShort current="1" :list="['是', '否']" @change="disabledChange"></u-subsection>
...@@ -53,7 +57,9 @@ ...@@ -53,7 +57,9 @@
count: 5, count: 5,
customIcon: false, customIcon: false,
plain: false, plain: false,
value: 0 value: 0,
colors: [],
icons: []
} }
}, },
watch: { watch: {
...@@ -93,6 +99,15 @@ ...@@ -93,6 +99,15 @@
this.inactiveColor = '#b2b2b2'; this.inactiveColor = '#b2b2b2';
} }
}, },
decimalChange(index) {
if(index == 0) {
this.colors = ['#ffc454', '#ffb409', '#ff9500'];
this.icons = ['thumb-down-fill', 'thumb-down-fill', 'thumb-up-fill', 'thumb-up-fill'];
} else {
this.colors = [];
this.icons = [];
}
},
iconChange(index) { iconChange(index) {
this.customIcon = !index; this.customIcon = !index;
}, },
......
<template> <template>
<view :style="[customStyle]" class="u-icon" @tap="click" :class="['u-icon--' + labelPos]"> <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> <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> <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>
<!-- 这里进行空字符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示 --> <!-- 这里进行空字符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示 -->
<text v-if="label !== ''" class="u-icon__label" :style="{ <text v-if="label !== ''" class="u-icon__label" :style="{
color: labelColor, color: labelColor,
...@@ -10,7 +15,8 @@ ...@@ -10,7 +15,8 @@
marginTop: labelPos == 'bottom' ? $u.addUnit(marginTop) : 0, marginTop: labelPos == 'bottom' ? $u.addUnit(marginTop) : 0,
marginRight: labelPos == 'left' ? $u.addUnit(marginRight) : 0, marginRight: labelPos == 'left' ? $u.addUnit(marginRight) : 0,
marginBottom: labelPos == 'top' ? $u.addUnit(marginBottom) : 0, marginBottom: labelPos == 'top' ? $u.addUnit(marginBottom) : 0,
}">{{label}}</text> }">{{ label }}
</text>
</view> </view>
</template> </template>
...@@ -38,6 +44,11 @@ ...@@ -38,6 +44,11 @@
* @property {String} width 显示图片小图标时的宽度 * @property {String} width 显示图片小图标时的宽度
* @property {String} height 显示图片小图标时的高度 * @property {String} height 显示图片小图标时的高度
* @property {String} top 图标在垂直方向上的定位 * @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 点击图标时触发 * @event {Function} click 点击图标时触发
* @example <u-icon name="photo" color="#2979ff" size="28"></u-icon> * @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
*/ */
...@@ -145,71 +156,120 @@ export default { ...@@ -145,71 +156,120 @@ export default {
top: { top: {
type: [String, Number], type: [String, Number],
default: 0 default: 0
},
// 是否为DecimalIcon
showDecimalIcon: {
type: Boolean,
default: false
},
// 背景颜色,可接受主题色,仅Decimal时有效
inactiveColor: {
type: String,
default: '#ececec'
},
// 显示的百分比,仅Decimal时有效
percent: {
type: [Number, String],
default: '50'
} }
}, },
computed: { computed: {
customClass() { customClass() {
let classes = []; let classes = []
classes.push(this.customPrefix + '-' + this.name); classes.push(this.customPrefix + '-' + this.name)
// uView的自定义图标类名为u-iconfont // uView的自定义图标类名为u-iconfont
if (this.customPrefix == 'uicon') classes.push('u-iconfont'); if (this.customPrefix == 'uicon') {
else classes.push(this.customPrefix); classes.push('u-iconfont')
} else {
classes.push(this.customPrefix)
}
// 主题色,通过类配置 // 主题色,通过类配置
if (this.color && this.$u.config.type.includes(this.color)) classes.push('u-icon__icon--' + this.color); 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]的形式,否则无法识别 // 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
// 故需将其拆成一个字符串的形式,通过空格隔开各个类名 // 故需将其拆成一个字符串的形式,通过空格隔开各个类名
//#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
classes = classes.join(' '); classes = classes.join(' ')
//#endif //#endif
return classes; return classes
}, },
iconStyle() { iconStyle() {
let style = {}; let style = {}
style = { style = {
fontSize: this.size == 'inherit' ? 'inherit' : this.$u.addUnit(this.size), fontSize: this.size == 'inherit' ? 'inherit' : this.$u.addUnit(this.size),
fontWeight: this.bold ? 'bold' : 'normal', fontWeight: this.bold ? 'bold' : 'normal',
// 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中 // 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
top: this.$u.addUnit(this.top) top: this.$u.addUnit(this.top)
}; }
// 非主题色值时,才当作颜色值 // 非主题色值时,才当作颜色值
if (this.color && !this.$u.config.type.includes(this.color)) style.color = this.color; if (this.showDecimalIcon && this.inactiveColor && !this.$u.config.type.includes(this.inactiveColor)) {
return style; style.color = this.inactiveColor
} else if (this.color && !this.$u.config.type.includes(this.color)) style.color = this.color
return style
}, },
// 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式 // 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
isImg() { isImg() {
return this.name.indexOf('/') !== -1; return this.name.indexOf('/') !== -1
}, },
imgStyle() { imgStyle() {
let style = {}; let style = {}
// 如果设置width和height属性,则优先使用,否则使用size属性 // 如果设置width和height属性,则优先使用,否则使用size属性
style.width = this.width ? this.$u.addUnit(this.width) : this.$u.addUnit(this.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); style.height = this.height ? this.$u.addUnit(this.height) : this.$u.addUnit(this.size)
return style; 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 + '%'
}
// 非主题色值时,才当作颜色值
if (this.color && !this.$u.config.type.includes(this.color)) style.color = this.color
return style
},
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)
}
// 主题色,通过类配置
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: { methods: {
click() { click() {
this.$emit('click', this.index); this.$emit('click', this.index)
}, },
touchstart() { touchstart() {
this.$emit('touchstart', this.index); this.$emit('touchstart', this.index)
} }
} }
}; }
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
@import "../../libs/css/style.components.scss"; @import "../../libs/css/style.components.scss";
/* #ifndef APP-NVUE */
// 目前由于nvue对定义字体时的content属性报错,所以nvue先不引入
@import '../../iconfont.css'; @import '../../iconfont.css';
/* #endif */
.u-icon { .u-icon {
/* #ifndef APP-NVUE */
display: inline-flex; display: inline-flex;
/* #endif */
flex-direction: row;
align-items: center; align-items: center;
&--left { &--left {
...@@ -256,11 +316,17 @@ export default { ...@@ -256,11 +316,17 @@ export default {
} }
} }
&__decimal {
position: absolute;
top: 0;
left: 0;
display: inline-block;
overflow: hidden;
}
&__img { &__img {
/* #ifndef APP-PLUS */
height: auto; height: auto;
will-change: transform; will-change: transform;
/* #endif */
} }
&__label { &__label {
......
...@@ -2,20 +2,22 @@ ...@@ -2,20 +2,22 @@
<view class="u-rate" :id="elId" @touchmove.stop.prevent="touchMove"> <view class="u-rate" :id="elId" @touchmove.stop.prevent="touchMove">
<view class="u-star-wrap" v-for="(item, index) in count" :key="index" :class="[elClass]"> <view class="u-star-wrap" v-for="(item, index) in count" :key="index" :class="[elClass]">
<u-icon <u-icon
:name="activeIndex > index ? activeIcon : inactiveIcon" :name="activeIndex > index ? elActiveIcon : inactiveIcon"
@click="click(index + 1, $event)" @click="click(index + 1, $event)"
:color="activeIndex > index ? activeColor : inactiveColor" :color="activeIndex > index ? elActiveColor : inactiveColor"
:custom-style="{ :custom-style="{
fontSize: size + 'rpx', fontSize: size + 'rpx',
padding: `0 ${gutter / 2 + 'rpx'}` padding: `0 ${gutter / 2 + 'rpx'}`
}" }"
:custom-prefix="customPrefix"
:show-decimal-icon="showDecimalIcon(index)"
:percent="decimal"
></u-icon> ></u-icon>
</view> </view>
</view> </view>
</template> </template>
<script> <script>/**
/**
* rate 评分 * rate 评分
* @description 该组件一般用于满意度调查,星型评分的场景 * @description 该组件一般用于满意度调查,星型评分的场景
* @tutorial https://www.uviewui.com/components/rate.html * @tutorial https://www.uviewui.com/components/rate.html
...@@ -33,6 +35,7 @@ ...@@ -33,6 +35,7 @@
* @event {Function} change 选中的星星发生变化时触发 * @event {Function} change 选中的星星发生变化时触发
* @example <u-rate :count="count" :current="2"></u-rate> * @example <u-rate :count="count" :current="2"></u-rate>
*/ */
export default { export default {
name: 'u-rate', name: 'u-rate',
props: { props: {
...@@ -97,6 +100,23 @@ export default { ...@@ -97,6 +100,23 @@ export default {
inactiveIcon: { inactiveIcon: {
type: String, type: String,
default: 'star' default: 'star'
},
// 自定义扩展前缀,方便用户扩展自己的图标库
customPrefix: {
type: String,
default: 'uicon'
},
colors: {
type: Array,
default() {
return []
}
},
icons: {
type: Array,
default() {
return []
}
} }
}, },
data() { data() {
...@@ -109,94 +129,127 @@ export default { ...@@ -109,94 +129,127 @@ export default {
activeIndex: this.value != -1 ? this.value : this.current, activeIndex: this.value != -1 ? this.value : this.current,
starWidth: 0, // 每个星星的宽度 starWidth: 0, // 每个星星的宽度
starWidthArr: [] //每个星星最右边到组件盒子最左边的距离 starWidthArr: [] //每个星星最右边到组件盒子最左边的距离
}; }
}, },
watch: { watch: {
current(val) { current(val) {
this.activeIndex = val; this.activeIndex = val
}, },
value(val) { value(val) {
this.activeIndex = val; this.activeIndex = val
}
},
computed: {
decimal() {
if (this.disabled) {
return this.activeIndex * 100 % 100
} else if (this.allowHalf) {
return 50
}
},
elActiveIcon() {
const len = this.icons.length
if (len && len <= this.count) {
const step = Math.round(this.activeIndex / Math.round(this.count / len))
if (step < 1) return this.icons[0]
if (step > len) return this.icons[len - 1]
return this.icons[step - 1]
}
return this.activeIcon
},
elActiveColor() {
const len = this.colors.length
if (len && len <= this.count) {
const step = Math.round(this.activeIndex / Math.round(this.count / len))
if (step < 1) return this.colors[0]
if (step > len) return this.colors[len - 1]
return this.colors[step - 1]
}
return this.activeColor
} }
}, },
methods: { methods: {
// 获取评分组件盒子的布局信息 // 获取评分组件盒子的布局信息
getElRectById() { getElRectById() {
// uView封装的获取节点的方法,详见文档 // uView封装的获取节点的方法,详见文档
this.$uGetRect('#' + this.elId).then(res => { this.$u.getRect('#' + this.elId).then(res => {
this.starBoxLeft = res.left; this.starBoxLeft = res.left
}) })
}, },
// 获取单个星星的尺寸 // 获取单个星星的尺寸
getElRectByClass() { getElRectByClass() {
// uView封装的获取节点的方法,详见文档 // uView封装的获取节点的方法,详见文档
this.$uGetRect('.' + this.elClass).then(res => { this.$u.getRect('.' + this.elClass).then(res => {
this.starWidth = res.width; this.starWidth = res.width
// 把每个星星右边到组件盒子左边的距离放入数组中 // 把每个星星右边到组件盒子左边的距离放入数组中
for (let i = 0; i < this.count; i++) { for (let i = 0; i < this.count; i++) {
this.starWidthArr[i] = (i + 1) * this.starWidth; this.starWidthArr[i] = (i + 1) * this.starWidth
} }
}) })
}, },
// 手指滑动 // 手指滑动
touchMove(e) { touchMove(e) {
if (this.disabled) { if (this.disabled) {
return; return
} }
if (!e.changedTouches[0]) { if (!e.changedTouches[0]) {
return; return
} }
const movePageX = e.changedTouches[0].pageX; const movePageX = e.changedTouches[0].pageX
// 滑动点相对于评分盒子左边的距离 // 滑动点相对于评分盒子左边的距离
const distance = movePageX - this.starBoxLeft; const distance = movePageX - this.starBoxLeft
// 如果滑动到了评分盒子的左边界,就设置为0星 // 如果滑动到了评分盒子的左边界,就设置为0星
if (distance <= 0) { if (distance <= 0) {
this.activeIndex = 0; this.activeIndex = 0
} }
// 滑动的距离,相当于多少颗星星 // 滑动的距离,相当于多少颗星星
let index = Math.ceil(distance / this.starWidth); let index = Math.ceil(distance / this.starWidth)
this.activeIndex = index > this.count ? this.count : index; this.activeIndex = index > this.count ? this.count : index
// 对最少颗星星的限制 // 对最少颗星星的限制
if (this.activeIndex < this.minCount) this.activeIndex = this.minCount; if (this.activeIndex < this.minCount) this.activeIndex = this.minCount
this.emitEvent(); this.emitEvent()
}, },
// 通过点击,直接选中 // 通过点击,直接选中
click(index, e) { click(index, e) {
if (this.disabled) { if (this.disabled) {
return; return
} }
// 半星选择,尚未实现 // 半星选择,尚未实现
if (this.allowHalf) { if (this.allowHalf) {
} }
// 对第一个星星特殊处理,只有一个的时候,点击可以取消,否则无法作0星评价 // 对第一个星星特殊处理,只有一个的时候,点击可以取消,否则无法作0星评价
if (index == 1) { if (index == 1) {
if (this.activeIndex == 1) this.activeIndex = 0; if (this.activeIndex == 1) {
else this.activeIndex = 1; this.activeIndex = 0
} else {
this.activeIndex = 1
}
} else { } else {
this.activeIndex = index; this.activeIndex = index
} }
// 对最少颗星星的限制 // 对最少颗星星的限制
if (this.activeIndex < this.minCount) this.activeIndex = this.minCount; if (this.activeIndex < this.minCount) this.activeIndex = this.minCount
this.emitEvent(); this.emitEvent()
}, },
// 发出事件 // 发出事件
emitEvent() { emitEvent() {
// 发出change事件 // 发出change事件
this.$emit('change', this.activeIndex); this.$emit('change', this.activeIndex)
// 同时修改双向绑定的value的值 // 同时修改双向绑定的value的值
if(this.value != -1) { if (this.value != -1) {
this.$emit('input', this.activeIndex); this.$emit('input', this.activeIndex)
} }
},
showDecimalIcon(index) {
return this.disabled && parseInt(this.activeIndex) === index
} }
}, },
mounted() { mounted() {
setTimeout(() => { this.getElRectById()
this.getElRectById(); this.getElRectByClass()
this.getElRectByClass();
}, 100)
} }
}; }
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
......
...@@ -9,5 +9,10 @@ module.exports = { ...@@ -9,5 +9,10 @@ module.exports = {
}, },
onShareAppMessage() { onShareAppMessage() {
return this.$u.mpShare return this.$u.mpShare
},
// #ifdef MP-WEIXIN
onShareTimeline() {
return this.$u.mpShare
} }
// #endif
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment