From 962c40bd3df53df30f73785beea0509c2e8d0362 Mon Sep 17 00:00:00 2001 From: 梁灏 Date: Tue, 17 Jan 2017 15:30:10 +0800 Subject: [PATCH] update Rate --- src/components/rate/rate.vue | 78 ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------ test/routers/rate.vue | 11 +++++++++-- 2 files changed, 57 insertions(+), 32 deletions(-) diff --git a/src/components/rate/rate.vue b/src/components/rate/rate.vue index 1f0115a..77cdf57 100644 --- a/src/components/rate/rate.vue +++ b/src/components/rate/rate.vue @@ -1,10 +1,11 @@ @@ -33,7 +34,9 @@ data () { return { prefixCls: prefixCls, - hoverIndex: -1 + hoverIndex: -1, + isHover: false, + isHalf: false }; }, computed: { @@ -46,53 +49,68 @@ ]; } }, + watch: { + value: { + immediate: true, + handler (val) { + this.setHalf(val); + } + } + }, methods: { starCls (value) { const hoverIndex = this.hoverIndex; + const currentIndex = this.isHover ? hoverIndex : this.value; + let full = false; + let isLast = false; + + if (currentIndex > value) full = true; - if (hoverIndex >= value) { - full = true; + if (this.isHover) { + isLast = currentIndex === value + 1; + } else { + isLast = Math.ceil(this.value) === value + 1; } return [ `${prefixCls}-star`, { - [`${prefixCls}-star-full`]: full, + [`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf), + [`${prefixCls}-star-half`]: isLast && this.isHalf, [`${prefixCls}-star-zero`]: !full } ]; }, - handleMousemove(value) { + handleMousemove(value, event) { if (this.disabled) return; + this.isHover = true; if (this.allowHalf) { -// let target = event.target; -// if (hasClass(target, 'el-rate__item')) { -// target = target.querySelector('.el-rate__icon'); -// } -// if (hasClass(target, 'el-rate__decimal')) { -// target = target.parentNode; -// } -// this.pointerAtLeftHalf = event.offsetX * 2 <= target.clientWidth; -// this.currentValue = this.pointerAtLeftHalf ? value - 0.5 : value; + const type = event.target.getAttribute('type') || false; + this.isHalf = type === 'half'; } else { - this.currentValue = value; + this.isHalf = false; } - this.hoverIndex = value; + this.hoverIndex = value + 1; }, handleMouseleave () { - if (this.disabled) { - return; - } - if (this.allowHalf) { -// this.pointerAtLeftHalf = this.value !== Math.floor(this.value); - } -// this.currentValue = this.value; + if (this.disabled) return; + + this.isHover = false; + this.setHalf(this.value); this.hoverIndex = -1; }, - handleClick () { - + setHalf (val) { + this.isHalf = val.toString().indexOf('.') >= 0; + }, + handleClick (value) { + if (this.disabled) return; + value++; + if (this.isHalf) value -= 0.5; + this.value = value; + this.$emit('on-change', value); + this.$dispatch('on-form-change', value); } } }; diff --git a/test/routers/rate.vue b/test/routers/rate.vue index 5a7d769..7676a3a 100644 --- a/test/routers/rate.vue +++ b/test/routers/rate.vue @@ -1,13 +1,20 @@