ant-design-vue/components/rate/Rate.vue

124 lines
2.6 KiB
Vue
Raw Normal View History

2017-11-01 09:03:42 +00:00
<template>
<ul
2017-11-02 07:05:31 +00:00
:class="classes"
2017-11-01 09:03:42 +00:00
@mouseleave="onMouseLeave">
<template v-for="i in count">
<Star
ref="stars"
2017-11-09 09:24:00 +00:00
:index="i - 1"
2017-11-01 09:03:42 +00:00
:disabled="disabled"
:prefix-cls="`${prefixCls}-star`"
:allowHalf="allowHalf"
2017-11-09 09:24:00 +00:00
:value="hoverValue === undefined ? stateValue : hoverValue"
@click="onClick"
@hover="onHover"
:key="i - 1">
2017-11-01 09:03:42 +00:00
<template slot-scope="props">
<slot>
2017-11-02 07:05:31 +00:00
<span>{{character}}</span>
2017-11-01 09:03:42 +00:00
</slot>
</template>
</Star>
</template>
</ul>
</template>
<script>
2017-11-02 07:05:31 +00:00
import Star from './Star.vue'
2017-11-09 09:24:00 +00:00
import Icon from '../icon'
import { getOffsetLeft } from './util'
2017-11-01 09:03:42 +00:00
export default {
name: 'Rate',
props: {
2017-11-02 07:05:31 +00:00
prefixCls: {
type: String,
default: 'ant-rate',
},
2017-11-01 09:03:42 +00:00
count: {
type: Number,
default: 5,
},
2017-11-02 07:05:31 +00:00
value: Number,
2017-11-01 09:03:42 +00:00
defaultValue: {
type: Number,
default: 0,
},
allowHalf: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
2017-11-02 02:56:18 +00:00
character: {
type: String,
2017-11-02 07:05:31 +00:00
default: '★',
2017-11-02 02:56:18 +00:00
},
2017-11-01 09:03:42 +00:00
},
2017-11-02 07:05:31 +00:00
data () {
const { value, defaultValue } = this
const reValue = value === undefined ? defaultValue : value
2017-11-01 09:03:42 +00:00
return {
2017-11-09 09:24:00 +00:00
hoverValue: reValue,
2017-11-02 07:05:31 +00:00
stateValue: reValue,
2017-11-01 09:03:42 +00:00
}
},
2017-11-02 07:05:31 +00:00
computed: {
classes () {
const { prefixCls, disabled } = this
return {
[`${prefixCls}`]: true,
[`${prefixCls}-disabled`]: disabled,
2017-11-01 09:03:42 +00:00
}
2017-11-02 02:56:18 +00:00
},
2017-11-01 09:03:42 +00:00
},
methods: {
2017-11-02 07:05:31 +00:00
onClick (event, index) {
2017-11-09 09:24:00 +00:00
const value = this.getStarValue(index, event.pageX)
if (this.value === undefined) {
this.stateValue = value
}
2017-11-02 07:05:31 +00:00
this.onMouseLeave()
2017-11-09 09:24:00 +00:00
this.$emit('input', value)
this.$emit('change', value)
2017-11-01 09:03:42 +00:00
},
2017-11-02 07:05:31 +00:00
onHover (event, index) {
2017-11-09 09:24:00 +00:00
const value = this.getStarValue(index, event.pageX)
this.hoverValue = value
this.$emit('hover-change', value)
2017-11-01 09:03:42 +00:00
},
getStarDOM (index) {
return this.$refs.stars[index].$el
},
getStarValue (index, x) {
2017-11-09 09:24:00 +00:00
const { allowHalf, getStarDOM } = this
let value = index + 1
if (allowHalf) {
const leftEdge = getOffsetLeft(getStarDOM(0))
const width = getOffsetLeft(getStarDOM(1)) - leftEdge
if ((x - leftEdge - width * index) < width / 2) {
2017-11-01 09:03:42 +00:00
value -= 0.5
}
}
return value
},
2017-11-02 07:05:31 +00:00
onMouseLeave () {
2017-11-09 09:24:00 +00:00
if (this.disabled) return
this.hoverValue = undefined
this.$emit('hover-change')
2017-11-02 07:05:31 +00:00
},
},
watch: {
2017-11-09 09:24:00 +00:00
value (val) {
this.stateValue = val
2017-11-01 09:03:42 +00:00
},
},
components: {
Star,
Icon,
2017-11-02 07:05:31 +00:00
},
2017-11-01 09:03:42 +00:00
}
</script>