You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/rate/Rate.vue

134 lines
3.0 KiB

7 years ago
<script>
7 years ago
import Star from './Star.vue'
7 years ago
import Icon from '../icon'
7 years ago
import { getOffsetLeft, deepClone } from './util'
7 years ago
export default {
name: 'Rate',
props: {
7 years ago
prefixCls: {
type: String,
default: 'ant-rate',
},
7 years ago
count: {
type: Number,
default: 5,
},
7 years ago
value: Number,
7 years ago
defaultValue: {
type: Number,
default: 0,
},
allowHalf: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
character: {
type: String,
7 years ago
default: '★',
},
7 years ago
},
7 years ago
data () {
const { value, defaultValue } = this
const reValue = value === undefined ? defaultValue : value
7 years ago
return {
hoverValue: undefined,
7 years ago
stateValue: reValue,
7 years ago
}
},
7 years ago
computed: {
classes () {
const { prefixCls, disabled } = this
return {
[`${prefixCls}`]: true,
[`${prefixCls}-disabled`]: disabled,
7 years ago
}
},
7 years ago
countList () {
return new Array(this.count).fill(1)
},
hasDefaultSlot () {
return !!this.$slots.default
},
7 years ago
},
methods: {
7 years ago
onClick (event, index) {
7 years ago
const value = this.getStarValue(index, event.pageX)
7 years ago
this.stateValue = value
7 years ago
this.onMouseLeave()
7 years ago
this.$emit('input', value)
this.$emit('change', value)
7 years ago
},
7 years ago
onHover (event, index) {
7 years ago
const value = this.getStarValue(index, event.pageX)
this.hoverValue = value
this.$emit('hover-change', value)
7 years ago
},
getStarDOM (index) {
7 years ago
return this.$refs['stars' + index].$el
7 years ago
},
getStarValue (index, x) {
7 years ago
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) {
7 years ago
value -= 0.5
}
}
return value
},
7 years ago
onMouseLeave () {
7 years ago
if (this.disabled) return
this.hoverValue = undefined
this.$emit('hover-change')
7 years ago
},
},
watch: {
7 years ago
value (val) {
this.stateValue = val
7 years ago
},
},
components: {
Star,
Icon,
7 years ago
},
7 years ago
render (createElement, a) {
const self = this
return createElement('ul', {
class: self.classes,
on: {
'mouseleave': self.onMouseLeave,
},
}, [
(
self.countList.map((item, i) => {
return createElement('Star', {
attrs: {
index: i,
disabled: self.disabled,
'prefix-cls': `${self.prefixCls}-star`,
allowHalf: self.allowHalf,
value: self.hoverValue === undefined ? self.stateValue : self.hoverValue,
},
ref: 'stars' + i,
key: i,
on: {
'click': self.onClick,
'hover': self.onHover,
},
}, [
((self.hasDefaultSlot) ? (deepClone(self.$slots.default, createElement)) : this.character),
])
})
),
])
},
7 years ago
}
</script>