iview/src/components/rate/rate.vue

131 lines
4 KiB
Vue
Raw Normal View History

<template>
<div :class="classes" @mouseleave="handleMouseleave">
2017-01-17 15:30:10 +08:00
<div
v-for="item in count"
:class="starCls(item)"
@mousemove="handleMousemove(item, $event)"
@click="handleClick(item)">
<span :class="[prefixCls + '-star-content']" type="half"></span>
</div>
2017-03-02 19:31:21 +08:00
<div :class="[prefixCls + '-text']" v-if="showText" v-show="currentValue > 0">
2017-07-12 13:51:40 +08:00
<slot><span>{{ currentValue }}</span> <span v-if="currentValue <= 1">{{ t('i.rate.star') }}</span><span v-else>{{ t('i.rate.stars') }}</span></slot>
2017-01-17 15:49:34 +08:00
</div>
</div>
</template>
<script>
2017-01-17 15:49:34 +08:00
import Locale from '../../mixins/locale';
2017-03-02 19:31:21 +08:00
import Emitter from '../../mixins/emitter';
2017-01-17 15:49:34 +08:00
const prefixCls = 'ivu-rate';
export default {
2017-03-02 19:31:21 +08:00
mixins: [ Locale, Emitter ],
props: {
count: {
type: Number,
default: 5
},
value: {
type: Number,
default: 0
},
allowHalf: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
2017-01-17 15:49:34 +08:00
},
showText: {
type: Boolean,
default: false
}
},
data () {
return {
prefixCls: prefixCls,
2017-01-17 15:30:10 +08:00
hoverIndex: -1,
isHover: false,
2017-09-04 15:42:18 +08:00
isHalf: this.allowHalf && this.value.toString().indexOf('.') >= 0,
2017-03-03 11:00:11 +08:00
currentValue: this.value
};
},
computed: {
classes () {
return [
`${prefixCls}`,
{
[`${prefixCls}-disabled`]: this.disabled
}
2017-01-17 12:04:33 +08:00
];
}
},
2017-01-17 15:30:10 +08:00
watch: {
2017-03-03 11:00:11 +08:00
value (val) {
this.currentValue = val;
2017-03-02 19:31:21 +08:00
},
2017-03-03 11:00:11 +08:00
currentValue (val) {
this.setHalf(val);
2017-01-17 15:30:10 +08:00
}
},
methods: {
starCls (value) {
const hoverIndex = this.hoverIndex;
2017-03-02 19:31:21 +08:00
const currentIndex = this.isHover ? hoverIndex : this.currentValue;
2017-01-17 15:30:10 +08:00
let full = false;
2017-01-17 15:30:10 +08:00
let isLast = false;
2017-03-02 19:31:21 +08:00
if (currentIndex >= value) full = true;
2017-01-17 15:30:10 +08:00
if (this.isHover) {
2017-03-02 19:31:21 +08:00
isLast = currentIndex === value;
2017-01-17 15:30:10 +08:00
} else {
2017-03-02 19:31:21 +08:00
isLast = Math.ceil(this.currentValue) === value;
}
return [
`${prefixCls}-star`,
{
2017-01-17 15:30:10 +08:00
[`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf),
[`${prefixCls}-star-half`]: isLast && this.isHalf,
[`${prefixCls}-star-zero`]: !full
}
2017-01-17 12:04:33 +08:00
];
},
2017-01-17 15:30:10 +08:00
handleMousemove(value, event) {
if (this.disabled) return;
2017-01-17 15:30:10 +08:00
this.isHover = true;
if (this.allowHalf) {
2017-01-17 15:30:10 +08:00
const type = event.target.getAttribute('type') || false;
this.isHalf = type === 'half';
} else {
2017-01-17 15:30:10 +08:00
this.isHalf = false;
}
2017-03-02 19:31:21 +08:00
this.hoverIndex = value;
},
handleMouseleave () {
2017-01-17 15:30:10 +08:00
if (this.disabled) return;
this.isHover = false;
2017-03-02 19:31:21 +08:00
this.setHalf(this.currentValue);
this.hoverIndex = -1;
},
2017-01-17 15:30:10 +08:00
setHalf (val) {
2017-09-04 16:16:29 +08:00
this.isHalf = this.allowHalf && val.toString().indexOf('.') >= 0;
2017-01-17 15:30:10 +08:00
},
handleClick (value) {
if (this.disabled) return;
2017-03-03 11:00:11 +08:00
// value++;
2017-01-17 15:30:10 +08:00
if (this.isHalf) value -= 0.5;
2017-03-02 19:31:21 +08:00
this.currentValue = value;
2017-03-03 11:00:11 +08:00
this.$emit('input', value);
this.$emit('on-change', value);
this.dispatch('FormItem', 'on-form-change', value);
}
}
};
2017-08-31 17:39:17 +02:00
</script>