update Rate

update Rate
This commit is contained in:
梁灏 2017-01-17 15:30:10 +08:00
parent d94d98c464
commit 962c40bd3d
2 changed files with 58 additions and 33 deletions

View file

@ -1,10 +1,11 @@
<template> <template>
<div :class="classes" @mouseleave="handleMouseleave"> <div :class="classes" @mouseleave="handleMouseleave">
<div v-for="item in count" :class="starCls(item)"> <div
<span v-for="item in count"
:class="[prefixCls + '-star-content']" :class="starCls(item)"
@mousemove="handleMousemove(item, $event)" @mousemove="handleMousemove(item, $event)"
@click="handleClick(item)"></span> @click="handleClick(item)">
<span :class="[prefixCls + '-star-content']" type="half"></span>
</div> </div>
</div> </div>
</template> </template>
@ -33,7 +34,9 @@
data () { data () {
return { return {
prefixCls: prefixCls, prefixCls: prefixCls,
hoverIndex: -1 hoverIndex: -1,
isHover: false,
isHalf: false
}; };
}, },
computed: { computed: {
@ -46,53 +49,68 @@
]; ];
} }
}, },
watch: {
value: {
immediate: true,
handler (val) {
this.setHalf(val);
}
}
},
methods: { methods: {
starCls (value) { starCls (value) {
const hoverIndex = this.hoverIndex; const hoverIndex = this.hoverIndex;
let full = false; const currentIndex = this.isHover ? hoverIndex : this.value;
if (hoverIndex >= value) { let full = false;
full = true; let isLast = false;
if (currentIndex > value) full = true;
if (this.isHover) {
isLast = currentIndex === value + 1;
} else {
isLast = Math.ceil(this.value) === value + 1;
} }
return [ return [
`${prefixCls}-star`, `${prefixCls}-star`,
{ {
[`${prefixCls}-star-full`]: full, [`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf),
[`${prefixCls}-star-half`]: isLast && this.isHalf,
[`${prefixCls}-star-zero`]: !full [`${prefixCls}-star-zero`]: !full
} }
]; ];
}, },
handleMousemove(value) { handleMousemove(value, event) {
if (this.disabled) return; if (this.disabled) return;
this.isHover = true;
if (this.allowHalf) { if (this.allowHalf) {
// let target = event.target; const type = event.target.getAttribute('type') || false;
// if (hasClass(target, 'el-rate__item')) { this.isHalf = type === 'half';
// 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;
} else { } else {
this.currentValue = value; this.isHalf = false;
} }
this.hoverIndex = value; this.hoverIndex = value + 1;
}, },
handleMouseleave () { handleMouseleave () {
if (this.disabled) { if (this.disabled) return;
return;
} this.isHover = false;
if (this.allowHalf) { this.setHalf(this.value);
// this.pointerAtLeftHalf = this.value !== Math.floor(this.value);
}
// this.currentValue = this.value;
this.hoverIndex = -1; 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);
} }
} }
}; };

View file

@ -1,13 +1,20 @@
<template> <template>
<div style="margin: 100px"> <div style="margin: 100px">
<Rate></Rate> {{value}}
<br><br><br>
<Rate :value.sync="value" :count="5" allow-half></Rate>
<br><br><br>
<i-button @click="value++">add</i-button>
<i-button @click="value--">remove</i-button>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
props: {}, props: {},
data () { data () {
return {}; return {
value: 3.8
};
}, },
computed: {}, computed: {},
methods: {} methods: {}