fix rate
parent
81e7976a16
commit
7708672f59
|
@ -5,14 +5,14 @@
|
||||||
<template v-for="i in count">
|
<template v-for="i in count">
|
||||||
<Star
|
<Star
|
||||||
ref="stars"
|
ref="stars"
|
||||||
:index="i"
|
:index="i - 1"
|
||||||
:disabled="disabled"
|
:disabled="disabled"
|
||||||
:prefix-cls="`${prefixCls}-star`"
|
:prefix-cls="`${prefixCls}-star`"
|
||||||
:allowHalf="allowHalf"
|
:allowHalf="allowHalf"
|
||||||
:value="currentValue"
|
:value="hoverValue === undefined ? stateValue : hoverValue"
|
||||||
@onClick="onClick"
|
@click="onClick"
|
||||||
@onHover="onHover"
|
@hover="onHover"
|
||||||
:key="i">
|
:key="i - 1">
|
||||||
<template slot-scope="props">
|
<template slot-scope="props">
|
||||||
<slot>
|
<slot>
|
||||||
<span>{{character}}</span>
|
<span>{{character}}</span>
|
||||||
|
@ -25,8 +25,8 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import Star from './Star.vue'
|
import Star from './Star.vue'
|
||||||
import Icon from '../icon/index'
|
import Icon from '../icon'
|
||||||
import { getOffsetLeft } from '../util/util'
|
import { getOffsetLeft } from './util'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'Rate',
|
name: 'Rate',
|
||||||
|
@ -44,14 +44,6 @@ export default {
|
||||||
type: Number,
|
type: Number,
|
||||||
default: 0,
|
default: 0,
|
||||||
},
|
},
|
||||||
onChange: {
|
|
||||||
type: Function,
|
|
||||||
default: () => {},
|
|
||||||
},
|
|
||||||
onHoverChange: {
|
|
||||||
type: Function,
|
|
||||||
default: () => {},
|
|
||||||
},
|
|
||||||
allowHalf: {
|
allowHalf: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false,
|
default: false,
|
||||||
|
@ -69,7 +61,7 @@ export default {
|
||||||
const { value, defaultValue } = this
|
const { value, defaultValue } = this
|
||||||
const reValue = value === undefined ? defaultValue : value
|
const reValue = value === undefined ? defaultValue : value
|
||||||
return {
|
return {
|
||||||
currentValue: reValue,
|
hoverValue: reValue,
|
||||||
stateValue: reValue,
|
stateValue: reValue,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -84,46 +76,43 @@ export default {
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
onClick (event, index) {
|
onClick (event, index) {
|
||||||
const clValue = this.getStarValue(index, event.pageX)
|
const value = this.getStarValue(index, event.pageX)
|
||||||
this.stateValue = clValue
|
if (this.value === undefined) {
|
||||||
|
this.stateValue = value
|
||||||
|
}
|
||||||
this.onMouseLeave()
|
this.onMouseLeave()
|
||||||
this.$emit('input', clValue)
|
this.$emit('input', value)
|
||||||
this.onChange(clValue)
|
this.$emit('change', value)
|
||||||
},
|
},
|
||||||
onHover (event, index) {
|
onHover (event, index) {
|
||||||
this.currentValue = this.getStarValue(index, event.pageX)
|
const value = this.getStarValue(index, event.pageX)
|
||||||
this.changeValue(this.currentValue)
|
this.hoverValue = value
|
||||||
this.onHoverChange(this.currentValue)
|
this.$emit('hover-change', value)
|
||||||
},
|
},
|
||||||
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
|
const { allowHalf, getStarDOM } = this
|
||||||
if (this.allowHalf) {
|
let value = index + 1
|
||||||
const leftEdge = getOffsetLeft(this.getStarDOM(0))
|
if (allowHalf) {
|
||||||
const width = getOffsetLeft(this.getStarDOM(1)) - leftEdge
|
const leftEdge = getOffsetLeft(getStarDOM(0))
|
||||||
if ((x - leftEdge - width * (index - 1)) < width / 2) {
|
const width = getOffsetLeft(getStarDOM(1)) - leftEdge
|
||||||
|
if ((x - leftEdge - width * index) < width / 2) {
|
||||||
value -= 0.5
|
value -= 0.5
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
},
|
},
|
||||||
onMouseLeave () {
|
onMouseLeave () {
|
||||||
this.currentValue = undefined
|
if (this.disabled) return
|
||||||
this.changeValue()
|
this.hoverValue = undefined
|
||||||
this.onHoverChange()
|
this.$emit('hover-change')
|
||||||
},
|
|
||||||
changeValue (val) {
|
|
||||||
if (val === undefined) {
|
|
||||||
this.currentValue = this.stateValue
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
value (val = 0) {
|
value (val) {
|
||||||
this.currentValue = this.stateValue = val
|
this.stateValue = val
|
||||||
this.$emit('input', val)
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
<template>
|
<template>
|
||||||
<li
|
<li
|
||||||
:class="getClassName()"
|
:class="getClassName"
|
||||||
@click="onClick"
|
@click="onClick"
|
||||||
@mousemove="onHover">
|
@mousemove="onHover">
|
||||||
<div :class="`${this.prefixCls}-first`"><slot></slot></div>
|
<div :class="`${prefixCls}-first`"><slot></slot></div>
|
||||||
<div :class="`${this.prefixCls}-second`"><slot></slot></div>
|
<div :class="`${prefixCls}-second`"><slot></slot></div>
|
||||||
</li>
|
</li>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
|
@ -17,26 +17,24 @@ export default {
|
||||||
allowHalf: Boolean,
|
allowHalf: Boolean,
|
||||||
value: Number,
|
value: Number,
|
||||||
},
|
},
|
||||||
data () {
|
computed: {
|
||||||
return {
|
|
||||||
}
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
getClassName () {
|
getClassName () {
|
||||||
const { prefixCls, index, value, allowHalf } = this
|
const { prefixCls, index, value, allowHalf } = this
|
||||||
const starValue = index
|
const starValue = index + 1
|
||||||
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`
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
onClick (e) {
|
onClick (e) {
|
||||||
if (this.disabled) return
|
if (this.disabled) return
|
||||||
this.$emit('onClick', e, this.index)
|
this.$emit('click', e, this.index)
|
||||||
},
|
},
|
||||||
onHover (e) {
|
onHover (e) {
|
||||||
if (this.disabled) return
|
if (this.disabled) return
|
||||||
this.$emit('onHover', e, this.index)
|
this.$emit('hover', e, this.index)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,14 +16,14 @@
|
||||||
</br>
|
</br>
|
||||||
回调函数
|
回调函数
|
||||||
<Rate
|
<Rate
|
||||||
:onChange="onChange"
|
@change="onChange"
|
||||||
:onHoverChange="onHoverChange"></Rate>
|
@hover-change="onHoverChange"></Rate>
|
||||||
<span v-if="hoverValue">{{hoverValue}}stars</span>
|
<span v-if="hoverValue">{{hoverValue}}stars</span>
|
||||||
<span v-if="rValue">{{rValue}}stars</span>
|
<span v-if="rValue">{{rValue}}stars</span>
|
||||||
<br/>
|
<br/>
|
||||||
<Rate
|
<Rate
|
||||||
:allowHalf="allowHalf"
|
:allowHalf="allowHalf"
|
||||||
:onHoverChange="onHoverChangeAH"></Rate>
|
@hover-change="onHoverChangeAH"></Rate>
|
||||||
<span v-if="hoverValueAH">{{hoverValueAH}}stars</span>
|
<span v-if="hoverValueAH">{{hoverValueAH}}stars</span>
|
||||||
</br>
|
</br>
|
||||||
自定义
|
自定义
|
||||||
|
|
|
@ -30,7 +30,7 @@ function getClientPosition (elem) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getOffsetLeft = (el) => {
|
export function getOffsetLeft (el) {
|
||||||
const pos = getClientPosition(el)
|
const pos = getClientPosition(el)
|
||||||
const doc = el.ownerDocument
|
const doc = el.ownerDocument
|
||||||
const w = doc.defaultView || doc.parentWindow
|
const w = doc.defaultView || doc.parentWindow
|
Loading…
Reference in New Issue