optimizion

pull/9/head
wangxueliang 2017-11-02 15:05:31 +08:00
parent b7d855ba7b
commit 9d7169bf7b
5 changed files with 81 additions and 80 deletions

View File

@ -1,6 +1,6 @@
<template> <template>
<ul <ul
:class="[prefixCls, disabled ? `${prefixCls}-disabled` : '', className]" :class="classes"
@mouseleave="onMouseLeave"> @mouseleave="onMouseLeave">
<template v-for="i in count"> <template v-for="i in count">
<Star <Star
@ -9,13 +9,13 @@
:disabled="disabled" :disabled="disabled"
:prefix-cls="`${prefixCls}-star`" :prefix-cls="`${prefixCls}-star`"
:allowHalf="allowHalf" :allowHalf="allowHalf"
:value="currentValue" :value="hoverValue"
@onClick="onClick" @onClick="onClick"
@onHover="onHover" @onHover="onHover"
:key="i"> :key="i">
<template slot-scope="props"> <template slot-scope="props">
<slot> <slot>
<span v-html="character"></span> <span>{{character}}</span>
</slot> </slot>
</template> </template>
</Star> </Star>
@ -24,22 +24,22 @@
</template> </template>
<script> <script>
import Vue from 'vue'; import Star from './Star.vue'
import Star from './Star.vue'; import Icon from '../icon/index'
import Icon from '../icon/index'; import { getOffsetLeft } from '../util/util'
import { getOffsetLeft } from '../util/util';
export default { export default {
name: 'Rate', name: 'Rate',
props: { props: {
prefixCls: {
type: String,
default: 'ant-rate',
},
count: { count: {
type: Number, type: Number,
default: 5, default: 5,
}, },
value: { value: Number,
type: Number,
default: 0,
},
defaultValue: { defaultValue: {
type: Number, type: Number,
default: 0, default: 0,
@ -62,67 +62,74 @@ export default {
}, },
character: { character: {
type: String, type: String,
default: '★' default: '★',
}, },
className: String,
}, },
data() { data () {
const reValue = this.value || this.defaultValue; const { value, defaultValue } = this
const reValue = value === undefined ? defaultValue : value
return { return {
prefixCls: 'ant-rate', hoverValue: reValue,
hoverValue: undefined, stateValue: reValue,
currentValue: reValue,
markValue: reValue,
} }
}, },
watch: { computed: {
hoverValue(val) { classes () {
if(val === undefined) { const { prefixCls, disabled } = this
this.currentValue = this.markValue; return {
return; [`${prefixCls}`]: true,
[`${prefixCls}-disabled`]: disabled,
} }
this.currentValue = val;
}, },
value() {
this.currentValue = this.markValue = this.value || this.defaultValue;
},
markValue(val) {
this.$emit('input', val);
}
}, },
methods: { methods: {
onClick(event, index) { onClick (event, index) {
let clValue = this.getStarValue(index, event.pageX); const clValue = this.getStarValue(index, event.pageX)
this.markValue = clValue; this.stateValue = clValue
this.onMouseLeave(); this.onMouseLeave()
this.onChange(clValue); this.$emit('input', clValue)
this.onChange(clValue)
}, },
onHover(event, index) { onHover (event, index) {
this.hoverValue = this.getStarValue(index, event.pageX); this.hoverValue = this.getStarValue(index, event.pageX)
this.onHoverChange(this.hoverValue); this.changeValue(this.hoverValue)
this.onHoverChange(this.hoverValue)
}, },
getStarDOM (index) { getStarDOM (index) {
return this.$refs.stars[index].$el return this.$refs.stars[index].$el
}, },
getStarValue (index, x) { getStarValue (index, x) {
let value = index; let value = index
if (this.allowHalf) { if (this.allowHalf) {
const leftEdge = getOffsetLeft(this.getStarDOM(0)) const leftEdge = getOffsetLeft(this.getStarDOM(0))
const width = getOffsetLeft(this.getStarDOM(1)) - leftEdge const width = getOffsetLeft(this.getStarDOM(1)) - leftEdge
if ((x - leftEdge - width * (index-1)) < width / 2) { if ((x - leftEdge - width * (index - 1)) < width / 2) {
value -= 0.5 value -= 0.5
} }
} }
return value return value
}, },
onMouseLeave() { onMouseLeave () {
this.hoverValue = undefined this.hoverValue = undefined
this.onHoverChange(undefined); this.changeValue()
this.onHoverChange()
},
changeValue (val) {
if (val === undefined) {
this.hoverValue = this.stateValue
return
}
},
},
watch: {
value (val = 0) {
this.hoverValue = this.stateValue = val
this.$emit('input', val)
}, },
}, },
components: { components: {
Star, Star,
Icon, Icon,
} },
} }
</script> </script>

View File

@ -17,36 +17,27 @@ export default {
allowHalf: Boolean, allowHalf: Boolean,
value: Number, value: Number,
}, },
data() { data () {
return { return {
} }
},
mounted() {
},
computed: {
}, },
methods: { methods: {
getClassName() { getClassName () {
const { prefixCls, index, value, allowHalf } = this; const { prefixCls, index, value, allowHalf } = this
const starValue = index; const starValue = index
if (allowHalf && value + 0.5 === starValue) { if (allowHalf && value + 0.5 === starValue) {
return `${prefixCls} ${prefixCls}-half ${prefixCls}-active`; return `${prefixCls} ${prefixCls}-half ${prefixCls}-active`
} }
return starValue <= value ? `${prefixCls} ${prefixCls}-full` : `${prefixCls} ${prefixCls}-zero`; return starValue <= value ? `${prefixCls} ${prefixCls}-full` : `${prefixCls} ${prefixCls}-zero`
}, },
onClick(e) { onClick (e) {
if(this.disabled) return; if (this.disabled) return
this.$emit("onClick", e, this.index); this.$emit('onClick', e, this.index)
}, },
onHover(e) { onHover (e) {
if(this.disabled) return; if (this.disabled) return
this.$emit("onHover", e, this.index); this.$emit('onHover', e, this.index)
}, },
}, },
watch: {
},
components: {
}
} }
</script> </script>

View File

@ -1,3 +1,3 @@
import Rate from './Rate'; import Rate from './Rate'
export default Rate; export default Rate

View File

@ -0,0 +1,3 @@
.icon-test{
font-size: 35px;
}

View File

@ -1,7 +1,7 @@
<template> <template>
<div> <div>
基本 基本
<Rate className="custom" :allowHalf="allowHalf"></Rate> <Rate class="custom"></Rate>
</br> </br>
半星 半星
<Rate :allowHalf="allowHalf"></Rate> <Rate :allowHalf="allowHalf"></Rate>
@ -41,25 +41,25 @@ export default {
hoverValue: undefined, hoverValue: undefined,
rValue: undefined, rValue: undefined,
hoverValueAH: undefined, hoverValueAH: undefined,
character: '好' character: '好',
} }
}, },
methods: { methods: {
onHoverChange(val) { onHoverChange (val) {
this.hoverValue = val; this.hoverValue = val
}, },
onChange(val) { onChange (val) {
this.rValue = val; this.rValue = val
}, },
onHoverChangeAH(val) { onHoverChangeAH (val) {
this.hoverValueAH = val; this.hoverValueAH = val
}, },
changeValue() { changeValue () {
this.initValue = 4; this.initValue = undefined
},
getValue () {
console.log(this.initValue)
}, },
getValue() {
alert(this.initValue)
}
}, },
components: { components: {
Rate, Rate,